2

When you browse all games on Twitch and you click on a game, it gets the content dynamically without reloading the page. The url also changes from https://www.twitch.tv/directory to https://www.twitch.tv/directory/game/GameNameHere. How can I listen to this url change?

I tried using window.onpopstate in the following code (in a TamperMonkey script) but the event doesn't fire.

$(document).ready(function() {
    console.log("Running"); // works

    window.onpopstate = function(e) {
        console.log("onpopstate triggered");
    };
});

Edit: Upon further investigation, it seems that for TamperMonkey, I had to use unsafeWindow instead of window to get onpopstate to work. However, onpopstate only works for when the browser goes back or forward. Please refer to the accepted answer for the other cases.

Rickkwa
  • 2,197
  • 4
  • 23
  • 34

1 Answers1

1

See here: How to get notified about changes of the history via history.pushState?

Basically, you'll want to intercept calls to pushState and replaceState. If you can't do that, you can (ugly) use a setInterval to monitor the URL for changes.

Community
  • 1
  • 1
Todd Christensen
  • 1,297
  • 8
  • 11