Firefox 扩展中的 localStorage

发布于 2024-10-11 03:28:36 字数 1228 浏览 7 评论 0原文

我正在尝试从 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 技术交流群。

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

发布评论

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

评论(3

绝情姑娘 2024-10-18 03:28:36

在 Firefox 扩展中,您可以使用 window.content.localStorage 访问 localStorage 对象,例如:

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

任何其他操作都会给您带来“组件不可用”错误。

顺便说一句,globalStorage 并不是这样工作的。您根本无法将其与扩展一起使用,因为该对象仅在从服务器运行时才可用。

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.

岁月静好 2024-10-18 03:28:36

使用 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

情未る 2024-10-18 03:28:36

使用 content.localStorage.wrappedJSObject.myVariable

use content.localStorage.wrappedJSObject.myVariable

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