7

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

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

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]

How to disable that alert?

P.S. this is NOT a javascript question, but a Greasemonkey question.

EDIT:

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>

There are also some scripts and CSS declarations in header.

Brock Adams
  • 90,639
  • 22
  • 233
  • 295
IAdapter
  • 62,595
  • 73
  • 179
  • 242
  • @01, That code `...` is placed in the body on a page, which you would like to abort using Greasemonkey? – Anders Jan 11 '11 at 13:05

2 Answers2

8

Update: For modern versions of Tampermonkey, Violentmonkey, Greasemonkey (but strongly recommended to avoid GM 4+):
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
// @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);
};

Note that if you are running Tampermonkey, you can block alerts more effectively by switching to Inject Mode: Instant:
Tampermonkey Settings => Config mode: Advanced => Experimental => Inject Mode: Instant.


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

// ==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);


Old answer, for Greasemonkey before August 2011:

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

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

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


Load this GM script:

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

unsafeWindow.alert=function() {};


Then visit: http://jsbin.com/ajeqe4/6 .

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).

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


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.

Brock Adams
  • 90,639
  • 22
  • 233
  • 295
  • Yupp, this is correct, just did not know if that was what he really meant. – Anders Jan 11 '11 at 14:04
  • I have pasted the code. I will look at adblock, thx for suggestion. if you have any other ideas, please share them. – IAdapter Jan 11 '11 at 14:31
  • 1
    @01, Yes, because it's executed before GM. – Anders Jan 11 '11 at 14:53
  • 1
    @01: Since your alert is coded into the body and fired inline, GM cannot block it.   Since it is not loaded from a file, adblock cannot block it.   Pretty much your only alternative is a browser add-on that rewrites/filters the HTML during page download.   **Realistically**, since this is a LAN and extremely poor usability, register complaints with managers and IT weenies. You can probably find ammunition at [Jakob Nielsen's excellent site](http://www.useit.com/). – Brock Adams Jan 11 '11 at 20:47
  • thx a lot, my company doesn't care about usability, but I don't use that page that often, so its ok. if I get bored I might try to write add-on. thx a lot for your help, now I know better how GM works. – IAdapter Jan 11 '11 at 22:11
3

If you use Scriptish then the following will always work:

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

unsafeWindow.alert=function() {};

You can get the user script here.

erikvold
  • 15,988
  • 11
  • 54
  • 98