本地存储数据库可以在不同的 Chrome 扩展程序之间交叉访问吗?

发布于 2024-10-18 06:51:34 字数 179 浏览 4 评论 0原文

我认为问题是不言自明的,但如果您需要更多,这里是:

Chrome 扩展 A 在本地存储中保存电子邮件地址。 Chrome 扩展程序 B 希望查看该电子邮件地址。

这是允许的吗? (这可能更多的是 HTML5 的东西,而不是 Chrome 特有的东西,但我的知识有限,所以我将在我想要知道答案的背景下构建它)。

Question I think 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).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜空下最亮的亮点 2024-10-25 06:51:34

例如,如果您拥有这两个扩展,则您将负责维护这两个扩展。您绝对可以使用跨扩展消息通信将该电子邮件甚至本地存储传递给其他扩展。

例如,看看我的扩展:
https://github.com/mohamedmansour/reload-all-tabs-extension /tree/v2

一个扩展是核心,另一个扩展只是浏览器操作(现在它们从 v3 开始合并),但 v2 允许它们相互通信。浏览器操作发送“ping”事件,核心扩展侦听此类事件并返回“pong”。浏览器操作扩展是核心扩展的“附加组件”。当您打开“选项”时,它会使用核心选项中的选项。

回到你的问题...要访问 localStorage 交叉扩展,你可以执行以下操作:

主要核心扩展:

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.
    }
  }
);

辅助扩展:

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'.
  }
);

顺便说一句,我真的没有测试这个扩大。我刚刚从重新加载所有选项卡扩展中复制并粘贴了类似的操作。

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.

拔了角的鹿 2024-10-25 06:51:34

不能直接发送消息,但您可以在分机之间发送消息。因此,如果存储电子邮件的扩展程序正在等待某个外部扩展程序的请求,它可以读取所需的数据并将其发回。有关详细信息,请访问此处

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文