gpt4 book ai didi

javascript - YouTube 未触发 history.pushState

转载 作者:行者123 更新时间:2023-11-29 18:07:31 25 4
gpt4 key购买 nike

我正在尝试使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com