YouTube does not launch history.pushState

I am trying to detect a page change on YouTube using history.pushState , but it never works. Ultimately, I want this work to work with the Tampermonkey / Greasemonkey script, and for this I understand that you need to enter the script into the actual page, which I did 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 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 it didn’t seem either. Does anyone have an idea of ​​what is going on here?

+2
source share
1 answer

YouTube uses spfjs to control PushState. However, it captures the function before you can neutralize it. That is why you are faced with a problem.

Either move the monkey patch script up to the page before loading spf.js, or you can search for spf events , such as spfdone, to detect the change.

+4
source

Source: https://habr.com/ru/post/1014588/


All Articles