[英]localStorage in a Firefox extension
我試圖從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擴展中使用。 現在我很困惑......
在Firefox擴展中,您可以使用window.content.localStorage訪問localStorage對象,例如:
var ls = window.content.localStorage;
ls.setItem("myvariable", "myvalue");
var item = ls.getItem("myvariable");
其他任何東西都會給你一個“組件不可用”的錯誤。
順便說一句,globalStorage不會這樣工作。 您根本無法使用擴展名,因為該對象僅在從服務器運行時才可用。
使用NsIDOMStorageManager xpcom接口,您可以獲取本地存儲信息。
https://developer.mozilla.org/en/XPCOM_Interface_Reference/NsIDOMStorageManager
使用content.localStorage.wrappedJSObject.myVariable
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.