这个问题在这里已有答案:
当我使用Tampermonkey中的用户脚本或作为Chrome扩展程序为youtube制作镀铬扩展程序时,扩展程序无法识别扩展程序应该像往常一样运行的页面,因为最近已经实现了history.push函数。例如:
// @include http://www.youtube.com/feed/subscriptions
我这样做是因为它应该只在我订阅时运行,但是当我点击视频时它会继续加载扩展名。当我把它变成镀铬扩展时,问题仍然存在。
如何查看某个youtube页面是否在运行我的代码?
问题是由history.pushState()修改URL的页面引起的。它只修改显示URL和内容而不重新加载页面。
因此,当您在Tampermonkey上运行脚本时,您需要强制重新加载该操作。
注意:如果您只想在离开订阅页面时不运行脚本,那么只需将@match语句更改为@include即可。
// ==UserScript==
// @name YT pushState
// @namespace http://tampermonkey.net/
// @version 0.1
// @match *://*.youtube.com/*
// @grant none
// ==/UserScript==
window.history.__proto__.pushState = function(a, b, url) {
window.location.href = url;
}
if (window.location.href.match(/https?:\/\/www\.youtube\.com\/feed\/.*/)) {
console.log("Put main function here!");
} else {
console.log("Ignore this page");
}