开发者

Can local storage databases be cross-accessed between separate Chrome extensions?

Question I thi开发者_高级运维nk is self explanatory, but if you need more, here it is:

Chrome Extension A saves an email address in localstorage. Chrome Extension B wants to see that email address.

Is this permitted? (This might be more of an HTML5 thing than a Chrome-specific thing, but my knowledge is limited so I'll frame it within the context of my desire to know the answer).


If you own the two extensions, for instance, your the one maintaining both extensions. You can definitely use cross extension message communication to pass that email or even localStorage to the other extension.

For example, take a look at my extension here: https://github.com/mohamedmansour/reload-all-tabs-extension/tree/v2

One extension is the core, and the other one is just the browser action (right now they are merged as of v3) but v2 lets them both communicate to each other. The browser action sends a "ping" event, and the core extension listens on such event and returns a "pong". The browser action extension is an "Add-On" to the core extension. When you open up "Options", it uses the options from the core one.

Back to your questions ... To access localStorage cross extensions, you can do something like this:

main core extension:

localStorage['foo'] = 'bar';
var secondary_extension_id = 'pecaecnbopekjflcoeeiogjaogdjdpoe';
chrome.extension.onRequestExternal.addListener(
  function(request, sender, response) {
    // Verify the request is coming from the Add-On.
    if (sender.id != secondary_extension_id)
      return;

    // Handle the request.
    if (request.getLocalStorage) {
      response({result: localStorage});
    } else {
      response({}); // Snub them.
    }
  }
);

secondary extension:

var main_extension_id = 'gighmmpiobklfepjocnamgkkbiglidom'
chrome.extension.sendRequest(main_extension_id, {getLocalStorage: 1},
  function (response) {
    var storage = response.result;
    alert(storage['foo']); // This should print out 'bar'.
  }
);

BTW, I really didn't test this extension. I just copied and pasted from the reload all tabs extension that did something similar.


Not directly, but you can send messages between extensions. So if an extension that stores emails is expecting a request from some external extension, it could read the required data and send it back. More about it here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜