[英]localStorage between panel content script and page content script in firefox addon
I'm creating a firefox addon with addon builder, and I want to save a value from page content, and then read it from panel content script. 我正在使用插件构建器创建一个firefox插件,我想从页面内容中保存一个值,然后从面板内容脚本中读取它。 I want to use localStorage, but I tried and I cannot share the same localStorage between page and panel script.
我想使用localStorage,但我尝试过,我无法在页面和面板脚本之间共享相同的localStorage。 Also I tried with self.port.emit , and this method is working perfectly I can send the value with port.emit and port.on, but this is not what i want because I need to refresh this value every second, and if I use that method then browser is overloaded with port requests, and that's why I want to use localStorage or something which can be stored in a variable and easily accessed from panel script.
我也试过self.port.emit,这个方法工作得很好我可以用port.emit和port.on发送值,但这不是我想要的,因为我需要每秒刷新一次这个值,如果我使用该方法然后浏览器重载端口请求,这就是为什么我想使用localStorage或可以存储在变量中并可以从面板脚本轻松访问的东西。
here I'm attaching panel script, which will read the value saved with page script. 这里我附加了面板脚本,它将读取用页面脚本保存的值。
var panel = require("panel").Panel({
width: 100,
height: 100,
contentScriptFile: data.url("panel_script.js")
});
here I'm attaching page script which will save a value from page content and panel script will read it. 这里我附加了页面脚本,它将从页面内容中保存一个值,面板脚本将读取它。
var pageMod = require("page-mod");
pageMod.PageMod({
include: "*",
contentScriptFile: self.data.url("page_script.js"),
contentScriptWhen: 'ready'
});
I believe the port
system is the recommended route for interacting with a Panel page. 我相信
port
系统是与Panel页面交互的推荐路线。 However here's how you'd solve the problem you've posted of accessing localStorage
for a page via a Firefox add-on. 但是,这就是你如何解决你通过Firefox插件访问
localStorage
页面的问题。
var chrome = require('chrome');
var data = require('sdk/self').data;
var ios = chrome.Cc["@mozilla.org/network/io-service;1"]
.getService(chrome.Ci.nsIIOService);
var ssm = chrome.Cc["@mozilla.org/scriptsecuritymanager;1"]
.getService(chrome.Ci.nsIScriptSecurityManager);
var dsm = chrome.Cc["@mozilla.org/dom/storagemanager;1"]
.getService(chrome.Ci.nsIDOMStorageManager);
var uri = ios.newURI(data.url('index.html'), "", null);
var principal = ssm.getCodebasePrincipal(uri);
var storage = dsm.getLocalStorageForPrincipal(principal, data.url('index.html'));
With that you'll be able to access the page's localStorage
using the storage
object as you normally would in a web page. 有了它,您将能够像在网页中一样使用
storage
对象访问页面的localStorage
。 eg storage.setItem('key', 'value')
and storage.getItem('key')
. 例如
storage.setItem('key', 'value')
和storage.getItem('key')
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.