How to override the alert function?

The site has such a code (its site on the local network)

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

I am trying to disable this alert using GM. I tried to do it

 unsafeWindow.alert=function() {}; 

but I see a warning 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 this alert?

PS This is NOT Javascript , but greasemonkey .

EDIT:

Company website, so I can’t insert the real code

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

The header also has some css scripts and declarations.

+4
source share
2 answers

Update:. For modern versions of Greasemonkey:
You can intercept alert() in most cases using @run-at document-start . For example, download 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); }; 

If your script requires GM_ functions , it must set @grant , which is different from none. In this case, use exportFunction() as follows:

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


Old answer, for Greasemonkey until August 2011:

unsafeWindow.alert=function() {}; works great in situations of choice.

But if this is really the code on the page, you cannot stop this warning with Greasemonkey.

This is because this warning will be triggered during page load and before the DOMContentLoaded event that occurs when Greasemonkey is launched.


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

Checking the code ( http://jsbin.com/ajeqe4/6/edit ), you will see 3 warnings. Greasemonkey can stop warnings that fire on load (usually).

Other factors may block GM’s ability to stop the warning ... The page loads too fast or closes, perhaps.


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

Otherwise, you will need to write an extension / addition.

+4
source

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 script here .

+2
source

Source: https://habr.com/ru/post/1335064/


All Articles