I'm trying to detect a page change on YouTube by using history.pushState
, but it never seems to trigger. I ultimately want to get this working from a Tampermonkey/Greasemonkey script, and for that I understand you need to inject the script into the actual page, which I've done like so, but to no avail:
html =
"var oldState = history.pushState;"+
"history.pushState = function() {" +
"alert('url changed');" +
"return oldState.apply(this);" +
"}";
var head = document.getElementsByTagName("body")[0];
var script = document.createElement('script');
script.type = 'text/javascript';
script.innerHTML = html;
head.appendChild(script);
I've also tried running the same code from the debug console:
var oldState = history.pushState;
history.pushState = function() {
alert('url changed');
return oldState.apply(this);
};
But that didn't seem to do it either. Anyone have an idea of what's going on here?
history.pushState
would detect a change? That methods adds an entry to the history. Detecting a change requires to listen for thepopstate
event. – Blakeblakeleeonpopstate
only seems to trigger when the back and forward buttons are pressed (from what I've tested). See https://mcmap.net/q/65755/-how-to-get-notified-about-changes-of-the-history-via-history-pushstate – Murageconsole.log
statement in there, and see if that generates the expected output … – Blakeblakeleeconsole.log(history.pushState)
(before overwriting it) yields what? – Blakeblakeleefunction () { [native code] }
– Murage