Alex
Alex

Reputation: 1055

Communicating between background scripts

Been developing google chrome extensions for awhile now and i've always been curious as to how two background scripts would communicate between eachother (recently came across and instance where this would be helpful). They cannot use chrome.runtime to send messages, as that method is used for sending messages to content scripts and the other background pages will not catch those messages.

So say I have 2 scripts running as background scripts, in one:

chrome.runtime.sendMessage({type:"hi"});

Then in the second background script:

chrome.runtime.onMessage.addListener(function (message, sender, sendResponse){
    if (message.type == "hi"){
        console.log("SAID HI");
    }
});

The second script won't catch the message, and i'm guessing because it's as if the 2 scripts are combined and the combined script is communicating with itself (which is silly and obviously wont work).

So if I didnt want to have all of my code in a single file, which would be an easy fix, what would be a good way to have the scripts communicate? Or am I possibly doing something wrong here?

Upvotes: 1

Views: 359

Answers (1)

Alex
Alex

Reputation: 1055

Global variables and functions defined in either script can be called/accessed in either script- as if they were a single script.

Source: comments in the original question, trial and error just now

Upvotes: 1

Related Questions