0

Below is manifest.json.

{
    "manifest_version": 3,
    ...
    "content_scripts": [
        {
            ...
            "world": "MAIN",
            "all_frames": true
        }
    ],
}

I set "word": "MAIN" and then due to a change in the runtime environment, chrome.runtime is undefined and I can't use the chrome.runtime.onMessage API to accept data sent from the background page.

Now when I want to send data from the background page to content_scripts what do I do?

Another question is, in the case of 'world' : 'MAIN', how do I send data from content_scripts to the background page?

The key thing to note about this is that content_scripts is already 'world: "MAIN" will not be able to access any of chrome's apis, which is a feature of setting this option. This is not a problem with normal content_scripts communicating with background pages.

Autumn
  • 161
  • 6
  • Declare a second content script with `world` and use CustomEvent, [example](https://stackoverflow.com/a/19312198), then use chrome.runtime messaging in that second script – wOxxOm Aug 30 '23 at 10:30
  • @wOxxOm That seems to be a different problem from this one – Autumn Aug 30 '23 at 10:40
  • My comment describes a solution for this very problem. It consists of two parts: 1) communicating between the two scripts, an example of which I linked, and 2) using chrome.runtime messaging in the standard content script. – wOxxOm Aug 30 '23 at 11:31
  • @wOxxOm The question you should answer is how to communicate between content_scripts and injected scripts, My problem is that content_scripts is already set to MAIN mode (setting this means content_scripts won't be able to access chrome's API), I want content_scripts to communicate with background-servers_worker. These are two problems. I don't see the connection between these two problems. Am I missing something? – Autumn Aug 30 '23 at 15:15
  • I've explained it twice, I don't know how I can do it better, but I'll try. So you need to declare a second content script without `world`. It'll be able to communicate with the background script (service worker). The two content scripts will communicate via CustomEvent as shown in the example I've linked. It means your MAIN world script sends a CustomEvent, your normal content script receives it and sends the data to the service worker. – wOxxOm Aug 30 '23 at 15:18
  • @wOxxOm Thank you, I see what you mean, this method is too roundabout, I may wish to find a better method, this method should be my last alternative. – Autumn Aug 30 '23 at 15:27
  • There are only 2 alternatives: via an iframe (last example [here](/a/68689866)) and via externally_connectable (only supported for specific domains, not for all sites). – wOxxOm Aug 30 '23 at 16:30

0 Answers0