0

I need to handle changes of url (not url hash).

A page uses window.history.pushState and window.history.replaceState to change url. Unfortunately window.onpopstate doesn't fire when url changes by JavaScript.

Usually, people override pushState and replaceState, including a call of window.onpopstate in them, but I can't use it because content scripts in Chrome extensions can't access to window object.

Maybe there is some API in Chrome extension for that?

Also I can check location.href by timer(it's very bad idea) or use DOM 3 Mutation events and observers, but it's not very good idea in this case too.

(Sorry for my poor English)

UPD: My question isn't duplicate of "Chrome extension Content Script not loaded until page is refreshed".

user64675
  • 482
  • 7
  • 25
  • Besides that, you can [inject code into the page itself](http://stackoverflow.com/questions/9515704/building-a-chrome-extension-inject-code-in-a-page-using-a-content-script) to override the `pushState`. – Xan Oct 12 '15 at 20:40
  • Yes I can, but injected code can't call handler, which placed in the content script. So I need to inject ALL my code :/ – user64675 Oct 12 '15 at 20:41
  • Nope, [you don't have to](http://stackoverflow.com/questions/33059366/chrome-storage-sync-get-from-web-page). – Xan Oct 12 '15 at 20:42
  • Ok, I think this is a good idea. – user64675 Oct 12 '15 at 20:45
  • I will agree it's not a good duplicate. – Xan Oct 12 '15 at 20:57
  • **push**State and **replace**State should not raise on**pop**state event. I'd use [chrome.webNavigation.onHistoryStateUpdated](https://developer.chrome.com/extensions/webNavigation#event-onHistoryStateUpdated) or would find an event used by the page that fires on hash-change like [spfdone](http://stackoverflow.com/a/32277150/3959875) on youtube.com – wOxxOm Oct 12 '15 at 23:42

0 Answers0