Skip to content

Communication channels

Simon Warta edited this page Apr 30, 2019 · 4 revisions

Simple one-time requests

Website -> background script

  • Receiver (background script): Setup listener via chrome.runtime.onMessageExternal.addListener()
  • Sender (website): Call chrome.runtime.sendMessage() with the extension ID as the first argument
  • Limitations: websites must be whitelisted in manifest.json's externally_connectable where at least a second level domain must be specified.

Background script -> extension

  • Receiver (extension): Setup listener via chrome.runtime.onMessage.addListener()
  • Sender (backround script): Call chrome.runtime.sendMessage()

Event based messaging

Content script -> Background script -> extension

Step 1:

Step 2:

  • Receiver (extension): Setup listener via chrome.runtime.onMessage.addListener()
  • Sender (backround script): Call chrome.runtime.sendMessage()

This solution requires explicit matching of request/response by ID since events don't have a back channel. IOV-Core's JsonRpcClient could be used to simplify that.