Hello. I decided to understand the writing of browser extensions, searched the Internet, but did not understand how to work with the content script. As far as I understand, this script is executed as if it is in the web page itself. But for some reason, for example, the simplest alert (); not performed. Where is the mistake?

Code: manifest.json: enter image description here

popup.html: enter image description here

popup.js: enter image description here

content_script.js: enter image description here

  • and without a handler executed (without chrome.runtime.onMessage ..)? - Misha Saidov
  • Put the code as text please - Deliaz

1 answer 1

To send a message from popup or background you need to use the chrome.tabs.sendMessage method. It is he who is responsible for delivering messages to the content script running on the page.
For this method, you must specify the tab ID. You can get it, for example, using the chrome.tabs.query method.

Also note that the view code:

 <button OnClick="start()">Button</button> 

.. will not work, due to the fact that in the extension is prohibited execution of inline javascript . You need to place the JS code in a separate file and connect it to popup.html via the script tag.