YouTube 未触发 history.pushState [英] YouTube not triggering history.pushState

查看:21
本文介绍了YouTube 未触发 history.pushState的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 history.pushState 检测 YouTube 上的页面更改,但它似乎从未触发.我最终希望通过 Tampermonkey/Greasemonkey 脚本来实现这一点,为此我知道您需要将脚本注入到实际页面中,我已经这样做了,但无济于事:

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?

推荐答案

YouTube 使用 spfjs 来操作 pushState.但是,它通常会在您对它进行猴子修补之前先获取该功能.这就是您遇到此问题的原因.

YouTube uses spfjs to manipulate pushState. However it grabs the function generally before you can monkey patch it. This is why you are experiencing the issue.

要么在加载 spf.js 之前在页面中向上移动您的猴子补丁脚本,要么您可以查找 spf 事件 像 spfdone 来检测变化.

Either move your monkey patch script up in the page before spf.js is loaded, or you can look for the spf events like spfdone to detect the change.

这篇关于YouTube 未触发 history.pushState的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆