谷歌浏览器扩展程序 - chrome.storage.sync.get无效 [英] Google Chrome Extension -- chrome.storage.sync.get not working
问题描述
只是想知道是否有人可以看到我的代码有什么问题,保存数据的工作正常,但是当我尝试加载它时,它找不到保存的数据:
代码:
$('#SaveSet')。click(function(){
var theValue = $('#col')。val();
if(!theValue){
alert('Error:No value specified');
return;
}
chrome.storage.sset.set({$ b $'ToSave':theValue
},function(){
alert('Settings saved'++ theValue);
});
});
'('#Get')。click(function(){
alert('working');
chrome.storage.sync.get(ToSave,function(data){
alert(data+ data);
});
});
当我在获取数据后得到警报时,它读取data [object Object]
我相信 chrome.storage.sync.get
返回给你一个对象,即使你只是要求单个键的值。
这意味着,而不是你的数据参数只是一个值,它实际上是一个对象,只有一个名为'ToSave'的键。
试试你的<$ c
$ b $ $ $ $('#Get')。click(function($($ ){
alert('working');
chrome.storage.sync.get(ToSave,function(data){
alert(data:+ data.ToSave);
});
});
Just wondering if anyone can see anything wrong with my code, the saving of the data works fine, however when I try to load it, it cannot find the saved data:
Code:
$('#SaveSet').click(function() {
var theValue = $('#col').val();
if (!theValue) {
alert('Error: No value specified');
return;
}
chrome.storage.sync.set({
'ToSave': theValue
}, function() {
alert('Settings saved' + " " + theValue);
});
});
$('#Get').click(function() {
alert('working');
chrome.storage.sync.get("ToSave", function(data) {
alert("data" + data);
});
});
When I get the alert after trying to get the data, it reads "data[object Object]"
I believe that chrome.storage.sync.get
will always return to you an object, even if you're only asking for the value of a single key.
That would mean that instead of your data
parameter being just the value, it is actually an object, with a single key named 'ToSave'.
Try this for your #Get
handler:
$('#Get').click(function() {
alert('working');
chrome.storage.sync.get("ToSave", function(data) {
alert("data: " + data.ToSave);
});
});
这篇关于谷歌浏览器扩展程序 - chrome.storage.sync.get无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!