Greasemonkey 脚本来关闭烦人的事情

发布于 2024-10-08 18:32:46 字数 623 浏览 0 评论 0原文

有没有办法使用 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 = '';
        }
    }
}

但这对我不起作用。

Is there a way I can use Greasemonkey to selectively remove scripts on a site?

Disclaimer: I know JS but don't have much (read: none) direct experience with GM.

I just need to stop external scripts from loading (can't use NoScript because I want to load other less annoying scripts).

I tried doing this:

// ==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 = '';
        }
    }
}

But it doesn't work for me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

剩一世无双 2024-10-15 18:32:46

是的,Adblock Plus 是最好的方法(如果适用)。

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

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

var scripts = document.getElementsByTagName('script');

for (var J = scripts.length-1;  J >=0;  --J)
{
    if (/foobar\.js/i.test (scripts[J].src) )
    {
        console.log ("Killed", scripts[J].src);
        scripts[J].parentNode.removeChild (scripts[J]);        
    }
}

/*--- Now you have to unload any unwanted event handlers and/or timers 
    that were set before the above code could fire.
    This is highly page-specific and may not be possible if anonymous
    functions were used.
*/

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

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

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

Yes, Adblock Plus is the best way to go, if applicable.

The GM code may not fire in time to stop all the damage, but -- for giggles -- a working version of your code would be something like so:

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

var scripts = document.getElementsByTagName('script');

for (var J = scripts.length-1;  J >=0;  --J)
{
    if (/foobar\.js/i.test (scripts[J].src) )
    {
        console.log ("Killed", scripts[J].src);
        scripts[J].parentNode.removeChild (scripts[J]);        
    }
}

/*--- Now you have to unload any unwanted event handlers and/or timers 
    that were set before the above code could fire.
    This is highly page-specific and may not be possible if anonymous
    functions were used.
*/

You will see that it actually removes the script elements.
But, alas, altering or removing the script elements, by itself, will have no effect most of the time.   Except, maybe, in delayed-load/run code (things that fire onload or that have the defer or async attributes set).

You won't have any effect until explicitly countering the handlers and timers that the bogus JS sets -- which is highly page-specific and not always possible.

Run this code to see for yourself.

_失温 2024-10-15 18:32:46

这是来自 Greasemonkey 论坛上马特·萨金特 (Matt Sargent) 的更短的一篇:

var badWidget = document.getElementById('challengeWinnersWidget');
badWidget.parentNode.removeChild(badWidget);

简单、优雅、有效!

Here is an even shorter one from Matt Sargent on Greasemonkey forum: 

var badWidget = document.getElementById('challengeWinnersWidget');
badWidget.parentNode.removeChild(badWidget);

Simple, elegant, effective!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文