Greasemonkey脚本关闭烦人的事情

cap*_*apn 5 javascript greasemonkey

有什么方法可以使用Greasemonkey选择性删除网站上的脚本吗?

免责声明:我了解JS,但是对GM没有太多直接的经验(阅读:没有)。

我只需要停止加载外部脚本即可(不能使用NoScript,因为我想加载其他不那么烦人的脚本)。

我尝试这样做:

// ==UserScript==
// @name           turn_shit_off
// @namespace      http://www.google.com
// @include        http://www.xyz.com/*
// ==/UserScript==

window.onload = function() {
    var d = document;   // shorthand
    var scripts = d.getElementsByTagName('script');
    for(var i = 0; i < scripts.count; i++) {
        if(scripts[i].src.indexOf('foobar.js') != -1) {
            scripts[i].src = '';
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

但这对我不起作用。

Bro*_*ams 3

是的,Adblock Plus是最好的选择(如果适用)。

\n\n

GM 代码可能无法及时触发来阻止所有损害,但是——咯咯笑——代码的工作版本将如下所示:

\n\n
// ==UserScript==\n// @name           turn_shit_off\n// @namespace      http://www.google.com\n// @include        http://www.xyz.com/*\n// ==/UserScript==\n\nvar scripts = document.getElementsByTagName(\'script\');\n\nfor (var J = scripts.length-1;  J >=0;  --J)\n{\n    if (/foobar\\.js/i.test (scripts[J].src) )\n    {\n        console.log ("Killed", scripts[J].src);\n        scripts[J].parentNode.removeChild (scripts[J]);        \n    }\n}\n\n/*--- Now you have to unload any unwanted event handlers and/or timers \n    that were set before the above code could fire.\n    This is highly page-specific and may not be possible if anonymous\n    functions were used.\n*/\n
Run Code Online (Sandbox Code Playgroud)\n\n


\n您将看到它实际上删除了脚本元素。
\n但是,唉,更改或删除script元素本身在大多数情况下不会产生任何效果。\xc2\xa0 除了,也许,在延迟加载/运行代​​码中(触发onload或设置了deferasync属性的东西)。

\n\n

在显式地对抗伪造的 JS 设置的处理程序和计时器之前,您不会产生任何效果——这是高度特定于页面的,并且并不总是可能的。

\n\n

运行这段代码来亲自看看。

\n