I need to prepare an object when the user navigates to a new page in WKWebView and refresh UI. I run a WKUserScript on behalf of the user to do this.
I'm using the readystatechange event listener to detect initial page load. I'm also listening to the visibilitychange event to refresh UI if the DOM is already loaded on a "Go back" or "Go Forward" action.
document.addEventListener('visibilitychange', () => {
//do whatever if the DOM is already loaded and if we are visible.
});
document.addEventListener('readystatechange', (event) =>
{
//do whatever when page is loaded
});
On certain websites however I noticed that neither of these events are being fired when I navigate -goForward: and -goBack:
I'm not sure why. My WKUserScript is in WKContentWorld.defaultClientWorld so it shouldn't be interfering with the javascript of the page. Is there another JS event I'm missing?
I could pick up the changed URL on the native code side but it would be nice to keep this all contained within the WKUserScript as this UI is for the "webview" and the native code shouldn't have to be bothered with having to patch in the additional glue. Is there another DOM event I need to listen to? I tried listening to window's pageshow but that didn't work. Also experimented a bit with the window's popstate event but no luck.