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.