使用 Firefox 扩展存储

use*_*858 2 firefox-addon firefox-addon-webextensions

我能够以某种方式运行这个扩展:

https://github.com/mdn/webextensions-examples/tree/master/beastify

popup/choose_beast.js单击时会调用浏览器图标。

有人可以告诉我为什么这段代码(放置在顶部popup/choose_beast.js)会生成异常:

try{
var ss = require("sdk/simple-storage");
ss.storage.myArray = [1, 1, 2, 3, 5, 8, 13];
}catch(e){
    alert('exception');
    console.log(e);
}
Run Code Online (Sandbox Code Playgroud)

这是 中的相关条目manifest.json

 "browser_action": {
    "default_icon": "icons/beasts-32.png",
    "default_title": "Beastify",
    "default_popup": "popup/choose_beast.html"
}
Run Code Online (Sandbox Code Playgroud)

我可以通过什么方式将数据存储在此弹出 html 中,以便我可以随时在内容脚本中检索?

另外,这个页面choose_beast.html是在什么上下文中运行的?背景、页面脚本还是内容脚本?

Kas*_*hif 5

应使用网络扩展storage API来实现此目的。

是一个用法示例。

  • 您不会在网络扩展中使用“简单存储”。事实上,您不会在 Web 扩展中使用任何 sdk 模块。Web 扩展具有新的 api,提供与 addon-sdk 相同的功能。由于“simple-storage” api 旨在允许扩展以键值格式保存数据,因此“storage API”提供了相同的功能,但您可能会发现语法不同 (2认同)
  • 检查[链接的扩展](https://github.com/mdn/webextensions-examples/blob/master/favourite-colour/)源代码如何使用“存储 API”保存和检索数据 (2认同)