Javascript Firefox扩展无法访问本地存储
我有一个Chrome扩展,我正在尝试适应FF。当我尝试访问本地存储时,代码失败。控制台中没有JS错误。我是FF新手,所以问题可能是基本的。为什么在Javascript Firefox扩展无法访问本地存储,javascript,local-storage,firefox-addon-webextensions,Javascript,Local Storage,Firefox Addon Webextensions,我有一个Chrome扩展,我正在尝试适应FF。当我尝试访问本地存储时,代码失败。控制台中没有JS错误。我是FF新手,所以问题可能是基本的。为什么在var gettingItem=browser.local.storage.get()上停止执行 此外,当我在FF中加载扩展时,我不会像在Chrome中那样看到选项菜单。如何打开“选项”窗格 我的舱单: { "manifest_version": 2, "name": "My name's mod", "short_name":
var gettingItem=browser.local.storage.get()上停止执行代码>
此外,当我在FF中加载扩展时,我不会像在Chrome中那样看到选项菜单。如何打开“选项”窗格
我的舱单:
{
"manifest_version": 2,
"name": "My name's mod",
"short_name": "Mod",
"version": "1.2.0",
"description": "Make the ",
"web_accessible_resources": [
"base-theme.css",
"light-theme.css",
"wider-theme.css",
"nobg-theme.css"
],
"permissions": [
"storage",
"cookies",
"*://forums.site.com/*"
],
"content_scripts": [{
"js": [
"cg-mod.js",
"cg-forum-class.js",
"cg-forum-writer.js",
"cg-onload.js"
],
"matches": ["https://forums.site.com/*", "http://forums.site.com/*" ]
}],
"icons": {
"16": "icon16.png",
"48": "icon48.png",
"128": "icon128.png" },
"options_ui": {
"page": "options.html",
"chrome_style": true
}
}
这是第一个被调用的文件:
//https://stackoverflow.com/questions/2400935/browser-detection-in-javascript
function IsChrome(){
var isOpera = !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0;
var isChrome = !!window.chrome && !isOpera; // Chrome 1+
return isChrome;
}
function IsFirefox()
{
var isFirefox = typeof InstallTrigger !== 'undefined'; // Firefox 1.0+
return isFirefox;
}
function GetStorage( callback ){
var defaultOptions = {
Theme: 'wider',
ShowSignatures: true,
TheNewlyArrived: false,
Kickstarter: false,
Recruitment: false,
Off: false
};
if( IsChrome() )
{
chrome.storage.sync.get( defaultOptions, function(items) {
console.log( "Get Storage for Chrome");
console.log( items );
callback( items );
});
}else if( IsFirefox() ) {
console.log("is firefox: "+ IsFirefox());
//browser.storage.sync.get
var gettingItem = browser.local.storage.get();
console.log(gettingItem);
console.log("THE PROBLEM IS HERE ^^");
/* gettingItem.then((items) => {
console.log( "Get Storage for Firefox");
callback( items );
}); */
}
}
然后在我的onload文件中,我有以下内容:
/*
Run these commands on startup.
*/
// link the custom CSS as a stylesheet to give it higher priority
function Onload( storage )
{
console.log( "storage: ");
console.log( storage );
// If off is true, they don't want to load the extension...
if( storage.Off )
return;
var link = document.createElement('link');
if( IsChrome() ){
link.href = chrome.extension.getURL( 'base-theme.css' );
} else if( IsFirefox() ) {
link.href = browser.extension.getURL( 'base-theme.css' );
}
link.type = 'text/css';
link.rel = 'stylesheet';
document.getElementsByTagName("head")[0].appendChild(link);
var link = document.createElement('link');
if( IsChrome() ){
link.href = chrome.extension.getURL( storage.Theme +'-theme.css');
} else if( IsFirefox() ) {
link.href = browser.extension.getURL( storage.Theme +'-theme.css');
}
link.type = 'text/css';
link.rel = 'stylesheet';
document.getElementsByTagName("head")[0].appendChild(link);
if( ! storage.ShowSignatures )
ForumWriter.ToggleVisibility( 'Signature' );
}
console.log("get storage pre");
// Call the Google values through a callback
GetStorage( Onload );
console.log("get storage post");
控制台显示:
Name says hello! (proves the files are loading in the right oder)
get storage pre
is firefox: true
问题来自这一行:
browser.local.storage.get()
你把它搞混了,本地是存储的属性,而存储是浏览器的属性
browser.storage.local.get()
但是看看你的chrome实现,你似乎想访问同步存储而不是
browser.storage.sync.get()
还有。我真的想指出,您正在将代码库翻倍,这应该避免,以便于将来的维护。我建议您使用以下代码片段:
var client;
if( IsChrome() ){
client= chrome;
} else if( IsFirefox() ) {
client = browser;
}
这样,您就可以替换所有如下所示的代码段
if( IsChrome() ){
link.href = chrome.extension.getURL( 'base-theme.css' );
} else if( IsFirefox() ) {
link.href = browser.extension.getURL( 'base-theme.css' );
}
与
控制台显示:
-那么,它应该显示什么?Hi@JaromandaX我添加了一个澄清的编辑。它应该是问题在这里^ ^
你读过。。。乍一看,您似乎应该使用相同的API调用,或者可能是browser.storage.sync.get
那么,如果您实际将该console.log添加到扩展中,它是否实际将其记录到控制台?browser.local.storage.get
应该是browser.storage.local.get
。。。我知道只有一个关键词,但我想不出来。我知道浏览器不能在Chrome上运行!我来试一试。谢谢,奇怪的是,您应该看到一个错误,如“无法访问未定义的属性存储”谢谢。客户端不能在Chrome上工作,这也是我的浏览器问题。我可能也应该映射一个变量。我将在以后重构。而且,我现在得到了{:“待定”}的承诺。我现在就去做。是的,客户不应该马上工作。您需要在之前定义它。我建议你现在就开始使用它,即使你以后重构。。。如果你这样做了,你就不会有这个问题。@Salketer,当使用一个变量来保存browser
与chrome
相比,使用完全相同的代码更好,除了名称空间,每次都被切换,不需要它。Firefox已经实现了带有回调的chrome.*
名称空间,就像chrome一样。因此,不需要执行任何切换或分配客户端
。只需在chrome和Firefox中使用chrome.*
。Firefox在很大程度上与Chrome代码兼容(对于Firefox实现的那些东西)。显然,存在一些差异,但这些差异通常存在于不太常见的领域。
link.href = client.extension.getURL( 'base-theme.css' );