Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Could you create an extension, hub, that is used to register other extensions and has a messaging hub.</p> <p>All of the extensions that wanted to communicate with the public DB could then do it via the hub. Upon initialization from the background page, each extension could register with the hub their ID and which events they want to subscribe to.</p> <h2>Register action from each extension</h2> <pre><code>chrome.tabs.sendRequest("hub", { action: "register", key: "somePrivKey", id: "extId", subscribeTo: ["createFoo", "deleteFoo"] }); </code></pre> <h2>Then, each action performed would be communicated to the hub:</h2> <pre><code>chrome.tabs.sendRequest("hub", { action: "createFoo", key: "somePrivKey", context: 1 }); </code></pre> <p>The hub extension would then listen to events. For "register" actions the hub would register the extension as an endpoint for the "subscribeTo" actions. For other actions ("createFoo" or "deleteFoo") the hub would iterate over the list of registered extensions for the event and perform a sendRequest that sends the "action" name and an optional "context".</p> <p>A shared "key" could be known between the hub and all the extensions that want to communicate to prevent the hub from listening to events not from a known source.</p> <h2>Hub extension background.js:</h2> <pre><code>var actionToExtMap = {}; chrome.extension.onRequestExternal.addListener(function(request, sender, sendResponse) { if (request.key === "somePrivKey") { if (request.action === "register") { for (i = 0; i &lt; request.subscribeTo.length; i++) { var action = request.subscribeTo[i]; var extsionsForAction = actionToExtMap[action] || []; extsionsForAction.push(request.id) } } else if (request.action) { var extensionsToSendAction = actionToExtMap[request.action]; for (i = 0; i &lt; extensionsToSendAction.length; i++) { chrome.extension.sendRequest(extensionsToSendAction[i], { action: request.action, context: request.context //pass an option context object } } } } }); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload