在现场有这样的代码(其网站上的网站)

<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]

如何禁用该警报?

PS这是 不是JavaScript 问题,但是 Greasemonkey 问题。

编辑:

它的公司的网站,所以我不能粘贴真正的代码

<head>
    <script>    
        dojo.require("dojo.back");
        dojo.back.init(); 
    </script>
</head>
<body onload="someMethod()">
    <iframe></iframe>
    <script>         
        alert("bla");
    </script>
</body>

标题中还有一些脚本和CSS声明。

有帮助吗?

解决方案

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

// ==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, , 你可以 通过切换到 Inject Mode: Instant:
Tampermonkey设置 => 配置模式: Advanced => 实验 => 注入模式: Instant.


如果您的脚本需要GM_功能, ,必须设置 @grant 除了没有。在这种情况下使用 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被解雇的时候。


加载此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 (通常)。

其他因素可能会阻止通用汽车停止警报的能力...页面加载太快或关闭。


在Pastebin.com上粘贴该页面的来源,如果可能的话,请未进行编辑。您可能还有其他事情可以做。也许通过Adblock阻止脚本?

否则,您必须编写一个扩展名/附加组件。

其他提示

如果您使用 Scriptish 那么以下将始终起作用:

// ==UserScript==
// @id              alert-killer-test@erikvold.com
// @name            Overwrite Alert
// @description     Overwrites alert()
// @include         *
// @run-at          document-start
// ==/UserScript==

unsafeWindow.alert=function() {};

你可以 在这里获取用户脚本.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top