Firefox 扩展中的 localStorage
我正在尝试从 Firefox 扩展访问页面的 localStorage。我的理解是 content
给出了对当前页面的 window
的引用。当我尝试使用 content.localStorage
访问页面的 localStorage 时,我想我正在获取对它的引用。但是,当我尝试 content.localStorage.length
时,我什么也没得到。
附件是有问题的代码。
var myExtension = {
init: function() {
var appcontent = document.getElementById("appcontent"); // browser
if(appcontent)
appcontent.addEventListener("DOMContentLoaded", myExtension.onPageLoad, true);
},
onPageLoad: function(aEvent) {
var doc = aEvent.originalTarget;
alert(content.localStorage) // alerts "[object XPCNativeWrapper [object Storage]]"
alert(content.localStorage.length) // alerts nothing
}
window.addEventListener("load", function() { myExtension.init(); }, false);
编辑#1:更多信息。
try{
alert(content.localStorage.getItem('todoData'))
alert(content.localStorage.length)
} catch (e){
alert(e)
}
长度抛出异常“[Exception...“组件不可用”nsresult:“0x80040111 (NS_ERROR_NOT_AVAILABLE)”
localStorage.length
当我将它放在 Firefox 的标准网页上时可以工作,但是content.localStorage.length
在 Firefox 扩展中不起作用,现在我很困惑......
I am trying to access the localStorage of a page from a Firefox extension. My understanding is that content
gives a reference to the window
of the current page. When I try and access the localStorage for the page with content.localStorage
, I think I am getting a reference to it. However, when I try content.localStorage.length
, I get nothing.
Attached is the code in question.
var myExtension = {
init: function() {
var appcontent = document.getElementById("appcontent"); // browser
if(appcontent)
appcontent.addEventListener("DOMContentLoaded", myExtension.onPageLoad, true);
},
onPageLoad: function(aEvent) {
var doc = aEvent.originalTarget;
alert(content.localStorage) // alerts "[object XPCNativeWrapper [object Storage]]"
alert(content.localStorage.length) // alerts nothing
}
window.addEventListener("load", function() { myExtension.init(); }, false);
EDIT#1: More information.
try{
alert(content.localStorage.getItem('todoData'))
alert(content.localStorage.length)
} catch (e){
alert(e)
}
The length is throwing the exception "[Exception... "Component is not available" nsresult: "0x80040111 (NS_ERROR_NOT_AVAILABLE)"
localStorage.length
works when I have it on a standard web page in Firefox, but content.localStorage.length
dose not work from the Firefox extension. Now I'm confused...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 Firefox 扩展中,您可以使用 window.content.localStorage 访问 localStorage 对象,例如:
任何其他操作都会给您带来“组件不可用”错误。
顺便说一句,globalStorage 并不是这样工作的。您根本无法将其与扩展一起使用,因为该对象仅在从服务器运行时才可用。
From a Firefox extension, you can access the localStorage object with window.content.localStorage, for example:
Anything else will give you a "Component not available" error.
As an aside, globalStorage doesn't work this way. You can't use it at all with an extension because the object is only available if it's run from a server.
使用 NsIDOMStorageManager xpcom 接口您可以获取本地存储信息。
https://developer.mozilla.org/en/XPCOM_Interface_Reference/NsIDOMStorageManager
Using NsIDOMStorageManager xpcom interface you can get your local storage information.
https://developer.mozilla.org/en/XPCOM_Interface_Reference/NsIDOMStorageManager
使用 content.localStorage.wrappedJSObject.myVariable
use content.localStorage.wrappedJSObject.myVariable