如何使用用户脚本覆盖警报函数?

7
在现场有类似这样的代码(它在局域网上的站点)
<script language="JavaScript" type="text/javascript">         
    alert("ble");
</script>

我试图使用GM禁用该警报。我一直试图这样做。

unsafeWindow.alert=function() {};

但是我看到警告并收到了这个错误

Error: uncaught exception: [Exception... "Component is not available"  nsresult: "0x80040111 (NS_ERROR_NOT_AVAILABLE)"  location: "JS frame :: file:///C:/Documents%20and%20Settings/arokitnicki/Dane%20aplikacji/Mozilla/Firefox/Profiles/sm4bsods.default/extensions/%7Be4a8a97b-f2ed-450b-b12d-ee082ba24781%7D/components/greasemonkey.js :: anonymous :: line 377"  data: no]

如何禁用该警告?

附注:这不是一个关于JavaScript的问题,而是一个关于Greasemonkey的问题。

编辑:

这是公司的网站,所以我不能粘贴真实代码。

<head>
    <script>    
        dojo.require("dojo.back");
        dojo.back.init(); 
    </script>
</head>
<body onload="someMethod()">
    <iframe></iframe>
    <script>         
        alert("bla");
    </script>
</body>
中还有一些脚本和 CSS 声明。

@01,那段代码<alert>...被放置在一个页面的body中,您想使用Greasemonkey来终止它? - Anders
2个回答

9

更新:对于现代版本的Tampermonkey、Violentmonkey和Greasemonkey(但强烈建议避免使用GM 4+)
您可以通过使用@run-at document-start来拦截大多数情况下的alert()。例如,加载此脚本,然后访问测试页面

// ==UserScript==
// @name    _Overwrite Alert
// @match   *://output.jsbin.com/*
// @grant   none
// @run-at  document-start
// ==/UserScript==

var alrtScope;
if (typeof unsafeWindow === "undefined") {
    alrtScope = window;
} else {
    alrtScope = unsafeWindow;
}

alrtScope.alert = function (str) {
    console.log ("Greasemonkey intercepted alert: ", str);
};

请注意,如果您正在运行Tampermonkey,则可以通过切换到“注入模式:即时”来更有效地阻止警报:
=> <配置模式> Advanced => <实验性> => <注入模式> Instant
如果您的脚本需要GM_函数,则必须设置@grant而不是none。在这种情况下,请使用exportFunction(),如下所示:
// ==UserScript==
// @name            _Overwrite Alert
// @match           *://output.jsbin.com/*
// @grant           GM_addStyle
// @run-at          document-start
// ==/UserScript==

function myAlert (str) {
    console.log ("Greasemonkey intercepted alert: ", str);
}
unsafeWindow.alert   = exportFunction (myAlert, unsafeWindow);

旧答案,适用于2011年8月之前的Greasemonkey:

unsafeWindow.alert=function() {}; 在某些情况下可以正常工作。

但是,如果页面上确实是这个代码,那么你将无法使用Greasemonkey停止该警报。

这是因为该警报将在页面加载期间和DOMContentLoaded事件之前触发,而Greasemonkey则是在DOMContentLoaded事件时启动。


请加载此GM脚本:

// ==UserScript==
// @name            Overwrite Alert
// @description     Overwrites alert()
// @include         http://jsbin.com/*
// ==/UserScript==

unsafeWindow.alert=function() {};

然后访问:http://jsbin.com/ajeqe4/6。 检查代码(http://jsbin.com/ajeqe4/6/edit),您将看到3个警报。Greasemonkey只能停止在load时触发的警报(通常情况下)。 其他因素可能会阻止GM停止警报...页面加载太快或者可能是闭包。 将该页面的源代码(如果可能,不要编辑)粘贴到pastebin.com上。也许还有其他方法可以做到这一点。也许通过adblock屏蔽脚本? 否则,您将不得不编写一个扩展程序/插件。

是的,这是正确的,只是不知道他真正的意思。 - Anders
我已经粘贴了代码。我会看一下广告拦截器,谢谢你的建议。如果你有其他想法,请分享。 - IAdapter
1
@01,是的,因为它在GM之前执行。 - Anders
1
@01: 由于您的警报已编码到主体中并在内联触发,因此GM无法阻止它。由于它不是从文件加载的,adblock也无法阻止它。基本上,您唯一的选择是浏览器插件,在页面下载期间重写/过滤HTML。实际上,由于这是一个局域网和极其糟糕的可用性,请向经理和IT专家提出投诉。您可能可以在Jakob Nielsen的优秀网站找到弹药。 - Brock Adams
非常感谢,我的公司不关心可用性,但我并不经常使用那个页面,所以没关系。如果我感到无聊,我可能会尝试编写附加组件。非常感谢您的帮助,现在我更了解GM的工作原理了。 - IAdapter

3
如果您使用Scriptish,则以下内容将始终有效:
// ==UserScript==
// @id              alert-killer-test@erikvold.com
// @name            Overwrite Alert
// @description     Overwrites alert()
// @include         *
// @run-at          document-start
// ==/UserScript==

unsafeWindow.alert=function() {};

您可以在此处获取用户脚本


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接