1

I need to turn alert notification off by my extension. The alert function is a javascript built-in function and i override it like below in content script but it still works.

content script:

window.alert = function(){}

It does not turn it off. The problem is realy simple but it does not work and i am going crazy :)

manifest.json:

"content_scripts": [
    {
      "js": [ "assets/js/jquery-1.12.4.min.js", "assets/js/common.js", "assets/js/handlers.js" ],
      "matches": [ "http://*/*", "https://*/*" ],
      "run_at": "document_start"
    }
  ],

handler.js:

window.alert = function (msg) {
    debugger;
    console.log(msg);
}
RockOnGom
  • 3,893
  • 6
  • 35
  • 53
  • 1
    You could probably look at the source of an extension that already does this, like https://chrome.google.com/webstore/detail/alert-control/ofjjanaennfbgpccfpbghnmblpdblbef?hl=en – ceejayoz Jun 03 '16 at 11:57
  • 1
    Possible duplicate of [Can I prevent an alert() with a Google Chrome Extension](http://stackoverflow.com/questions/2963677/can-i-prevent-an-alert-with-a-google-chrome-extension) (note the accepted answer's top comment - it appears this can no longer be done) – ceejayoz Jun 03 '16 at 11:57
  • It does not work for me. I have already tried it. – RockOnGom Jun 03 '16 at 13:20
  • Dmitriy Khudorozhkov's answer is correct. I just wanted to stress that the code needs to be injected into the page. Running it in the sandboxed environment of the content script is not enough. (There will be 2 levels of injection: extension injects content script, content script then injects code into the page.) – Petr Srníček Jun 03 '16 at 13:38

3 Answers3

4

You can't switch off notifications globally in Chrome (at least, not that way).

By doing

window.alert = function(){}

you simply switch off notifications for your own extension (= your own window context). You can't do this globally for security reasons (think what malicious extension could do if this was possible).

Edit: you actually can inject your code into the content pages; but this is still not a "global" change that you seek. If you still want this, then:

manifest.js of your extension:

{
    "name": "MyExtension",
    ...
    "permissions": ["tabs", "http://*/*"],
    "content_scripts" : [{
        "js" : ["script.js"]
    }]
}

And your script.js:

window.alert = function(){}
Community
  • 1
  • 1
Dmitriy Khudorozhkov
  • 1,624
  • 12
  • 24
1

As Dmitriy mention it must be injected to the page. It works great please vote for Dimitry i just added the answer for sharing my code. contend script:

 function override_alert() {
        window.alert = function alert(msg) {
            console.log('Hidden Alert ' + msg);
        };
    }
 if (!document.xmlVersion) {
        var script = document.createElement('script');
        script.appendChild(document.createTextNode('(' + override_alert + ')();'));
        document.documentElement.appendChild(script);
 }
RockOnGom
  • 3,893
  • 6
  • 35
  • 53
  • I used your code , but it didn't work, I tested with https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_alert , can you help here ? https://github.com/redstoneleo/AlertRedirect – iMath Aug 30 '18 at 07:43
  • https://developer.chrome.com/extensions/content_scripts#run_time you might need to indicate document_end, and override_alert actually works:https://jsfiddle.net/n76Ltzdf/1/ – RockOnGom Aug 30 '18 at 07:52
  • Thanks ! but still didn't work in a Chrome extension, as you know , extension's content script runs in the sandboxed environment which is different from the example you provide ! – iMath Aug 30 '18 at 13:55
-2

Could you try something like this?

$(document).ready( function() { window.alert = function(){}; });

FriendsKenny
  • 150
  • 3
  • 11