Firefox扩展中的localStorage [英] localStorage in a Firefox extension

查看:185
本文介绍了Firefox扩展中的localStorage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从Firefox扩展中访问一个页面的localStorage。我的理解是, content 给出了对当前页面的窗口的引用。当我尝试访问localStorage与 content.localStorage 的页面,我想我得到了一个参考。但是,当我尝试 content.localStorage.length 时,我什么也得不到。

附件是有问题的代码。

  var myExtension = {
init:function(){
var appcontent = document.getElementById appcontent); //浏览器
if(appcontent)
appcontent.addEventListener(DOMContentLoaded,myExtension.onPageLoad,true);
},

onPageLoad:function(aEvent){
var doc = aEvent.originalTarget;
alert(content.localStorage)//警报[object XPCNativeWrapper [object Storage]]
alert(content.localStorage.length)//不提示
}
窗口。 addEventListener(load,function(){myExtension.init();},false);

编辑#1:更多资讯

<$ ($)b $ b $ alert(content.localStorage.getItem('todoData'))
alert(content.localStorage.length)
} catch(e) {
alert(e)
}

长度抛出异常 异常...组件不可用nsresult:0x80040111(NS_ERROR_NOT_AVAILABLE)
$ b

localStorage.length 当我在Firefox的标准网页上使用它,但 content.localStorage.length 不适用于Firefox扩展。现在我很困惑... 从Firefox的扩展中,你可以通过 window.content.localStorage 来访问localStorage对象,例如:

  var ls = window.content.localStorage; 
ls.setItem(myvariable,myvalue) ;
var item = ls.getItem(myvariable);

其他wi会给你一个组件不可用的错误。

另外,globalStorage不能这样工作。你完全不能使用扩展名,因为这个对象只有从服务器运行才可用。


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...

解决方案

From a Firefox extension, you can access the localStorage object with window.content.localStorage, for example:

var ls = window.content.localStorage;
ls.setItem("myvariable", "myvalue");
var item = ls.getItem("myvariable");

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.

这篇关于Firefox扩展中的localStorage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆