I want to make an extension to block dialog boxes, made through manifest.json

{ "content_scripts": [ { "matches": ["http://*/*", "https://*/*"], "js": ["content.js"], "run_at": "document_start", "all_frames": true } ], "permissions": [ "https://*/*", "http://*/*", "tabs" ], "web_accessible_resources": ["js.js"] 

content.js standard:

 var s = document.createElement('script'); s.src = chrome.extension.getURL('js.js'); (document.head||document.documentElement).appendChild(s); s.onload = function() { s.parentNode.removeChild(s); }; 

but js.js:

 window.alert = function alert(data) { console.log('Alert ' + data); }; Object.defineProperty(window, 'alert', { enumerable: false, configurable: false, writable: false, }); window.confirm = function confirm(data) { console.log('Confirm ' + data); }; Object.defineProperty(window, 'confirm', { enumerable: false, configurable: false, writable: false, }); window.prompt = function prompt(data) { console.log('prompt' + data); }; Object.defineProperty(window, 'prompt', { enumerable: false, configurable: false, writable: false, }); 

for example, on the site https://regex101.com anyway, a dialog box appears that I don’t know yet about browser JS?

1 answer 1

The code does not work because it is executed in the context of content_scripts and does not have access to variables, functions defined on the page.

This is what the documentation says about it:

Isolated scripts. There are no variables or functions created by the page. It is running on. It is true that the script is in reverse: javascript

In order for the code to start working in the context of the page, you need to define the corresponding key in manifest.json , namely web_accessible_resources .

An example of using this key:

 { "web_accessible_resources": ["js/script.js"] } 

where script.js is a script that should be executed in the context of a web page.

If you embed a script without specifying the corresponding key, then an error will be received in the web page console:

Denying load of chrome-extension: //aomgbbmcobgkafcfkedmilgaohcapael/js.js . Resources must be listed in the web_accessible_resources .

Execution environment | Web Accessible Resources

UPD :

the problem is that I quietly intercept all alert / confirm / prompt of the bargain JS, but it is on regex101.com that the window is displayed all the same, as I understand it of a different type

This is the usual confirm window.

Appears as a result of the beforeunload event, that is, before closing the page, but with the condition that the property returnValue will have a non-empty value.

This is a non-void value. This screen is displayed in this dialog.

It cannot be customized, due to security policy. Some browser versions ( Firefox up to 44.0, Google Chrome up to 51.0, etc) allow you to change the message text. In newer versions, support for custom messages is removed , replacing them with messages that the browser defines.


In this case, when trying to leave the site, the above event is triggered:

catch beforeunload

And here is the handler function code (part of the source code is not copied):

 window.addEventListener("beforeunload", function (e) { var t = { regex: $("#regex").val(), options: getOptions(), regexText: $("#regex_string").val(), isSub: sub_enabled(), sub: $("#sub").val(), delimiter: $("#delimiter_selector").text(), flavor: getFlavor(), permaid: $("#permalink_menu").attr("data-permalink"), unitTests: V(!1) }, a = JSON.stringify(t); if (a !== X) { var i = "Your most recent changes have not been saved. "; return i += "If you leave before saving, your changes will be lost.", (e || window.event).returnValue = i, i } }); 

There is no way to remove the handler, since for this you need a reference to it, but it does not exist, since an anonymous function is used.

Calling addEventListener creates an listener each time. Calling removeEventListener to an anonymous function has no effect. It can call one. It can be used to always be able to make it.

WindowEventHandlers.onbeforeunload | Event.returnValue | EventTarget.removeEventListener

Unloading documents

  • The script is registered in the web_accessible_resources, without it it doesn’t work at all, the problem is that I calmly intercept all alert / confirm / prompt of the bargain JS, but it is on regex101.com that the window is still displayed, as I understand it of a different type, but how to intercept such windows I yet I did not understand. - pnp2000
  • @ vnn198, it was necessary to indicate this immediately, it turns out the current question is not correct. Reformulate. What window are we talking about? I could not reproduce - UserName
  • If you write something on regex101.com and try to close the tab, then the "Leave this page" window will pop up - the changes may not be saved, and without writing to the "web_accessible_resources" script will not work at all and the console will have an error that this extension is not loaded because which is not spelled out in web_accessible_resources, so I didn’t write about it because it’s so obvious - pnp2000
  • @ vnn198, check the updated answer. - UserName
  • I'll check, just on vacation now, with the Internet hard - pnp2000