我试图通过content_script在扩展中使用chrome存储,但我一直在失败
Uncaught TypeError: Cannot read property 'sync' of undefined
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
testChromeStorage();
function testChromeStorage() {
console.log("Saving");
chrome.storage.sync.set({'value': theValue}, function() {
message('Settings saved');
});
chrome.storage.sync.get("value", function (retVal) {
console.log("Got it? " + retVal.value);
});
}
Run Code Online (Sandbox Code Playgroud) 我正在开发Chrome扩展程序,我需要存储一些数据,然后在某些时候获取它.我对可用的 storage
s 进行了调查,并且遇到了以下问题:window.localStorage
和chrome.storage.local
.
所以我的问题是,哪一个是在Chrome扩展程序中使用的正确选择:
window.localStorage
或者chrome.storage.local
?
PS我使用browser action
加载本地HTML
在IFRAME
.所以我没有使用popup.js
.
javascript local-storage google-chrome-extension google-chrome-storage
我可以使用Chrome扩展程序API获取存储密钥的价值:
chrome.storage.sync.get("someKey", function() {});
Run Code Online (Sandbox Code Playgroud)
如何获取Chrome存储中存在的所有密钥名称?
在过去的两天里,我一直在使用chrome异步存储.如果你有一个功能,它的工作"很好".(如下):
chrome.storage.sync.get({"disableautoplay": true}, function(e){
console.log(e.disableautoplay);
});
Run Code Online (Sandbox Code Playgroud)
我的问题是我无法使用我正在做的功能.我想把它归还,就像LocalStorage一样.就像是:
var a = chrome.storage.sync.get({"disableautoplay": true});
Run Code Online (Sandbox Code Playgroud)
要么
var a = chrome.storage.sync.get({"disableautoplay": true}, function(e){
return e.disableautoplay;
});
Run Code Online (Sandbox Code Playgroud)
我已经尝试了一百万种组合,甚至设置一个公共变量并设置:
var a;
window.onload = function(){
chrome.storage.sync.get({"disableautoplay": true}, function(e){
a = e.disableautoplay;
});
}
Run Code Online (Sandbox Code Playgroud)
什么都行不通.它都返回undefined,除非引用它的代码在get函数内部,这对我来说没用.我只是希望能够将值作为变量返回.
这甚至可能吗?
编辑:这个问题不重复,请允许我解释原因:
1:没有其他帖子专门询问这个问题(我花了两天时间先查看,以防万一).
2:我的问题仍未得到解答.是的,Chrome Storage是异步的,是的,它不会返回值.那就是问题所在.我将在下面详细说明......
我需要能够在chrome.storage.sync.get函数之外获取存储值.我无法使用localStorage,因为它是特定于URL的,并且无法从chrome扩展的browser_action页面和background.js访问相同的值.我无法使用一个脚本存储值并使用另一个脚本访问它.他们分开对待.
所以我唯一的解决方案是使用Chrome存储.必须有某种方法来获取存储项的值并在get函数之外引用它.我需要在if语句中检查它.
就像localStorage可以做的那样
if(localStorage.getItem("disableautoplay") == true);
Run Code Online (Sandbox Code Playgroud)
必须有一些方法可以做一些事情
if(chrome.storage.sync.get("disableautoplay") == true);
Run Code Online (Sandbox Code Playgroud)
我意识到这不会那么简单,但这是我解释它的最佳方式.
我看到的每个帖子都说这样做:
chrome.storage.sync.get({"disableautoplay": true, function(i){
console.log(i.disableautoplay);
//But the info is worthless to me inside this function.
});
//I need it outside this function.
Run Code Online (Sandbox Code Playgroud) javascript function return-value google-chrome-extension google-chrome-storage
我写了一个Chrome扩展程序.我无法使用
localStorage.setItem
和localStorage.getItem
存储和检索,因为后台和浏览器操作在不同的环境中运行[ 如此处所示 ].
所以我决定使用Chrome存储API:
var storage = chrome.storage.local;
var myTestVar = 'somevar';
var obj = {};
obj[myTestVar] = $("#somevar").val();
storage.set(obj);
Run Code Online (Sandbox Code Playgroud)
这产生了以下错误:
未捕获的TypeError:无法读取未定义的属性"local"
我究竟做错了什么?
javascript local-storage google-chrome-extension google-chrome-storage
我有4件我想要的东西,但我不确定如何分开钥匙.使用逗号会出错.以下是我的用法示例:
chrome.storage.sync.get({
'customImage',
'customColor',
'customRandColor',
'customRandImage'
}, function(backgroundCheckedOptions) {
document.getElementById('optionsCustomImage').checked = backgroundCheckedOptions.customImage;
document.getElementById('optionsBackgroundColor').checked = backgroundCheckedOptions.customColor;
document.getElementById('optionsRandomColor').checked = backgroundCheckedOptions.customRandColor;
document.getElementById('optionsRandomImage').checked = backgroundCheckedOptions.customRandImage;
});
Run Code Online (Sandbox Code Playgroud)
我原以为他们会用逗号分开,但我猜不是.
是否可以将数组存储在chrome存储同步中并检索它们?
var uarray = [abc,def,ghi];
Run Code Online (Sandbox Code Playgroud)
是否可以更新存储中存储的数组?
var tobeadded = jkl;
uarray.push(tobeadded);
Run Code Online (Sandbox Code Playgroud)
这是文档中的语法
chrome.storage.sync.set({'value': theValue}, function() {
// Notify that we saved.
message('Settings saved');
});
Run Code Online (Sandbox Code Playgroud)
我的书签扩展,需要存储书签的ID并检索它们以进行内部搜索和基于它的填充.书签需要定期更新存储同步中的ID.
谢谢!!
我有一个与LocalStorage一起使用的小片段,但我无法使其适用于Chrome存储计划.
当我的应用程序启动时,我检查localStorage中的变量
var bookNarration=parseInt(localStorage.getItem("narration"));
Run Code Online (Sandbox Code Playgroud)
如果该变量未定义,则表示我的应用程序第一次打开,并且在使用"默认"声明处理交换机中的bookLanguage之后.
switch(window.bookNarration)
{
case 2:
window.narrationShift = window.spanishShift;
break;
case 3:
window.narrationShift = window.frenchShift;
break;
case 10:
window.narrationShift = window.neutralShift;
break;
default:
window.narrationShift = 0; }
Run Code Online (Sandbox Code Playgroud)
为了使其适用于Chrome存储,我以这种方式更改了我的代码:
var bookNarration=parseInt(chrome.storage.local.get("narration"));
Run Code Online (Sandbox Code Playgroud)
但我立刻得到了这个错误:
调用表单get(string)与定义get(可选字符串或数组或对象键,函数回调)不匹配
我一直在寻找寻找解决方案的许多小时,但我无法使其发挥作用.我只需要检查是否已经定义了值,所以如果不是,我可以使用set()方法来存储我的默认值.
javascript local-storage google-chrome-extension chrome-web-store google-chrome-storage
所有.我已经开始使用Chrome的各种API开发小扩展程序了,尽管工作得很好,但我仍然对一些事情感到好奇.
两个问题,如果你们都不介意帮助我:
1.有人能告诉我是什么限制都chrome.storage.local的持久性?我自己尝试了各种测试,比如用chrome.storage.local.set()存储一些东西,清除所有浏览器历史记录,cookie等等,然后看看是否还有一切.它经常会在那里,但有时我会稍后再回来看看它会消失.总的来说,我无法明确地标记我正在做的偶尔清除.local数据的内容.
2.到目前为止,我一直主要使用chrome.storage.sync,而不是使用Chrome的"登录Chrome"功能登录.我在API页面上看到,在Chrome离线时使用.sync(以及未登录,我假设,这是我的情况),数据存储在本地,然后再进行同步.我主要担心的是,这是否意味着我使用chrome.storage.sync存储的数据可能会被删除,因为我使用chrome.storage.local存储的数据过去了吗?无论如何,我一直在使用.sync的主要原因之一是因为我从未有过使用它擦除数据的经验,而我使用.local(正如我在#1中描述的那样).
非常感谢!请帮帮我!
编辑:我很确定.local清除没有发生,因为我错误地删除了扩展,然后重新添加它.我知道这将清除.local数据(但保留.sync).
javascript google-chrome google-chrome-extension google-chrome-storage
我想存储我的扩展程序的设置 - 被弹出窗口更改:
chrome.storage.local.set({'extension-status': 'on'}, function() {
console.log('extension on status stored');
}
Run Code Online (Sandbox Code Playgroud)
并在更改此设置时在我的 bakground 页面中接收更新:
chrome.storage.onChanged.addListener(function(changes, namespace) {
console.log("change recived!");
});
Run Code Online (Sandbox Code Playgroud)
我在弹出窗口的日志中看到消息“存储状态的扩展”,但在后台页面的日志中没有看到“收到更改”。
我如何对这个存储对象的变化做出反应?