Skip to content Skip to sidebar Skip to footer

Messages Intended For One Script In The Background Context Are Received By All

I have a WebExtension with the following structure: webextension [directory] - background.js - page1.html - page1.js - page2.html - page2.js The background.js liste

Solution 1:

Messages sent by runtime.sendMessage() are received in all scripts in the background context which have a runtime.onMessage listener registered, except the script from which the message was sent. There is no way for you to restrict the recipients of such messages.

Thus, you have to create some methodology to determine if the message is intended for each script which receives it. This can be done in a wide variety of ways, but all of them are based on either:

  1. The contents of the message and/or
  2. The sender

Both of these are provided as arguments to the runtime.onMessage() listener.

Using message

To use the message you have to choose to impose some structure on the message. The message can be any JSON-ifiable data you choose to send. Imposing some structure allows you to more easily use messages more complex and more reliably communicate information between your scripts. There is nothing that is predefined. You can use whatever you desire. However, being consistent in your choices usually makes it easier to program and almost always makes code easier to maintain.

Personally, I'm usually sending messages for different reasons. For each extension, I'll usually choose to always send an Object with a particular format. That format may be different for each extension, but it usually looks something like:

var message = {
    type: 'requestFoo',
    //subType: 'Used if the type needs to be further split',data: dataNeededForRequest
    //You can add whatever other properties you want here.
};

If the extension has multiple possible recipients, then either A) only one recipient would understand how to handle a requestFootype message, and all others would ignore such messagetypes, or if there were multiple background context scripts which could handle requestFoo types, then I would add a recipient or destination property. Thus, the message would look like:

var message = {
    type: 'requestFoo',
    //subType: 'Used if the type needs to be further split',recipient: 'page2.js',
    data: dataNeededForRequest
    //You can add whatever other properties you want here.
};

When each script received the message they would check both that the recipient matched the script which had received the message and that the code understood how to handle the type of message.

Keep in mind that the structure above is just what I happen to use. You can define whatever structure you desire which also fulfills your needs.

Using sender

If a script is never to act on messages received from a specific sender(s), or if it is to only act on messages from specific senders, then that script can check the senderruntime.MessageSender Object to see if the parameters match ones from a sender for which it is to act on the message. If you use this methodology, you will most commonly be checking the sender.url.

Basing a choice to act on a message based solely on the sender is significantly more limited than being based on the contents of the message. However, it can be quite useful when used in addition to information provided in the message. It also provides a way to know the sender of the message which can not be spoofed. In addition, it means that you don't need to communicate information as to which scope was the sender of the message, unless, of course, you have more than one possible sender in the scope (i.e. in the URL/page).


Post a Comment for "Messages Intended For One Script In The Background Context Are Received By All"