我正在尝试使用 history.pushState
检测 YouTube 上的页面更改,但它似乎从未触发过。我最终想通过 Tampermonkey/Greasemonkey 脚本实现此功能,为此我知道您需要将脚本注入(inject)实际页面,我已经这样做了,但无济于事:
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);
我还尝试从调试控制台运行相同的代码:
var oldState = history.pushState;
history.pushState = function() {
alert('url changed');
return oldState.apply(this);
};
但这似乎也没有做到。有人知道这里发生了什么吗?
最佳答案
YouTube 使用 spfjs操纵 pushState。然而,它通常在你可以猴子修补它之前捕获这个功能。这就是您遇到此问题的原因。
要么在加载 spf.js 之前在页面中向上移动您的猴子补丁脚本,要么您可以查找 spf events像 spfdone 来检测变化。
关于javascript - YouTube 未触发 history.pushState,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30199242/