1

I'm writing an extension for this site. In my injected script(injected using chrome.tabs.executeScript) which runs at "document_end" everything seems to be working fine but I want to remove site's "alert" prompts.

To do I've placed this code in the injected script but it does not work:

window.alert = function(msg){
    console.log("bypassed");
}

Also tried this from the injected script:

$('<script>window.alert = function (msg){}</' + 'script>').appendTo(document.body);

I've even tried this in the background page but this too doesn't:

chrome.tabs.executeScript({
                code: 'window.alert=function(msg){}' //while alert("hello") works
            });

What am I missing? I've written another extension for another site and this method does override the alert function.

UPDATE: I'm unable to call native functions in the downloaded web page from the injected script which will happen when called from content script. But my injected script is loaded using chrome.tabs.executeScript

user5858
  • 1,082
  • 4
  • 39
  • 79
  • when you inspect the page with the extension on, is your script successfully appended? – Noam Hacker Apr 15 '16 at 14:00
  • @NoamHacker Yes it is for sure. – user5858 Apr 15 '16 at 14:04
  • have you tried the proxy pattern that [this answer](http://stackoverflow.com/a/1729684/4926817) uses? – Noam Hacker Apr 15 '16 at 14:24
  • [this answer](http://stackoverflow.com/a/12096099/4926817) recommends to run at document_start, rather than document_end, "so that the overwrite takes place before any of the page's functions are loaded". I will write up a more detailed answer – Noam Hacker Apr 15 '16 at 14:25
  • Ways 1 and 3 won't work because you'll be overriding the alert of the isolated world, not of the original page. I don't see why way 2 wouldn't work. – Teepeemm Apr 15 '16 at 14:30

1 Answers1

0

I am guessing that your issue is with your manifest.json file, where you have specified to "run_at": "document_end".

From the documentation:

In the case of "document_start", the files are injected after any files from css, but before any other DOM is constructed or any other script is run.

but,

In the case of "document_end", the files are injected immediately after the DOM is complete, but before subresources like images and frames have loaded.

Referencing this answer, in the case of document_end, the overwrite cannot take place as the functions are already loaded, therefore you should have the javascript be injected at document_start.

Community
  • 1
  • 1
Noam Hacker
  • 4,671
  • 7
  • 34
  • 55