如何覆盖警报功能? [英] How to override alert function?

查看:182
本文介绍了如何覆盖警报功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现场有类似代码(其网站上的网站)

On site there is code like that (its site on LAN)

<script language="JavaScript" type="text/javascript">         
    alert("ble");
</script>

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

I try to disable that alert using GM. I was trying to do this

unsafeWindow.alert=function() {};

但我看到警报并收到此错误

but I see the alert and get this error

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这是 NOT Javascript 问题,但 greasemonkey 问题。

P.S. this is NOT Javascript question, but greasemonkey question.

编辑:

其公司的网站,所以我无法粘贴真实代码

Its company's website, so I can't paste the real code

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

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

There are also some scripts and css declarations in header.

推荐答案

更新:对于Greasemonkey的现代版本:

您可以拦截 alert()在大多数情况下使用 @ run-at document-start 。例如,加载此脚本,然后访问测试页

Update: For modern versions of Greasemonkey:
You can intercept alert() in most cases by using @run-at document-start. For example, load this script and then visit the test page:

// ==UserScript==
// @name            Overwrite Alert
// @description     Overwrites alert()
// @include         http://output.jsbin.com/*
// @grant           none
// @run-at          document-start
// ==/UserScript==

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

如果您的脚本需要GM_功能,则必须设置 @grant 除了没有。在这种情况下,使用 exportFunction(),如下所示:

If your script requires GM_ functions, it must set @grant other than none. In that case use exportFunction() like so:

// ==UserScript==
// @name            Overwrite Alert
// @description     Overwrites alert()
// @include         http://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(){}; 工作正常在某些情况下。



Old answer, for Greasemonkey before August 2011:

unsafeWindow.alert=function() {}; works fine in select situations.

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

But, if that really is the code on the page, then you will not be able to stop that alert using Greasemonkey.

这是因为该警报将在页面加载期间和 DOMContentLoaded 事件之前触发 - 这是Greasemonkey被触发的时候。

This is because that alert will fire during the page load and before the DOMContentLoaded event -- which is when Greasemonkey is fired.



加载此GM脚本:


Load this GM script:

// ==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只能停止在加载(通常)时触发的警报。

Inspecting the code (http://jsbin.com/ajeqe4/6/edit), You will see 3 alerts.   Greasemonkey is only able to stop the alerts that fire on load (usually).

其他因素可能会阻止GM的能力停止警报...页面加载太快或关闭,或许。

Other factors can block GM's ability to stop the alert... The page loads too fast or closures, perhaps.

粘贴该页面的来源,如果可能的话,未经编辑,在pastebin.com。您可以做其他事情。也许通过adblock阻止脚本?

Paste the source of that page, unedited if at all possible, at pastebin.com. There may be something else you can do.   Maybe block the script via adblock?

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

Otherwise, you'll have to write an extension/add-on.

这篇关于如何覆盖警报功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆