18

I have a content script that is run when the user connects to "*://www.youtube.com/*". I need to know when the URL changes, as YouTube does not open a new 'page', so-to-speak, when you click on a link; rather it changes the URL and page contents (I think). Because of this, my content script doesn't realize that anything has changed. (I know this because it DOES work when I reload the page)

So how do I find out when the URL of the tab that my script is running in has changed?

[Edit]: My question concerns specifically getting the URL of the tab that the content script is running in, rather than getting the url of the active tab.

otoomey
  • 939
  • 1
  • 14
  • 31
  • Possible duplicate of [Get current URL for Chrome extension](http://stackoverflow.com/questions/21078989/get-current-url-for-chrome-extension) – Fohlen Nov 29 '16 at 20:07
  • @Fohlen, This question is not a duplicate of that question. That question is specifically regarding getting the URL of the tab from within a `chrome.contextMenus` (which its primary answer reflects). Looking at the code from that answer it is clear that, despite there being better solutions (as answered), that question is actually a duplicate of the question listed in comments as a possible duplicate. – Makyen Nov 29 '16 at 21:29
  • How did you manage to actually get this working? Specifically for the youtube case, how did you manage to detect that the page had changed? – pingOfDoom Jun 26 '20 at 02:13
  • @pingOfDoom I periodically checked the location.href, every half/full second or so. – otoomey Jun 26 '20 at 22:07

1 Answers1

42

location.href is a simple way to get the active URL https://developer.mozilla.org/en-US/docs/Web/API/Window/location

user2535381
  • 588
  • 1
  • 7
  • 10