Chrome: get URL of active content script tab

11,546

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

Share:
11,546
otoomey
Author by

otoomey

Updated on June 03, 2022

Comments

  • otoomey
    otoomey about 2 years

    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.

  • Badrush
    Badrush over 2 years
    Sometimes the best answer is the simplest :)
  • letterphile
    letterphile over 2 years
    document.location.href would be more precise and direct to use!! right ?