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 = '';
}
}
}
但这对我不起作用。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,Adblock Plus 是最好的方法(如果适用)。
GM 代码可能无法及时触发以阻止所有损害,但是 - 咯咯笑 - 你的代码的工作版本将如下所示:
您将看到它实际上删除了脚本元素。
但是,遗憾的是,更改或删除
script
元素本身在大多数情况下不会产生任何效果。 除了,也许,在延迟加载/运行代码中(触发onload
或具有defer
或async 属性设置)。
在显式地对抗伪造的 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:
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 fireonload
or that have thedefer
orasync
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.
这是来自 Greasemonkey 论坛上马特·萨金特 (Matt Sargent) 的更短的一篇:
简单、优雅、有效!
Here is an even shorter one from Matt Sargent on Greasemonkey forum:
Simple, elegant, effective!