[英]Chrome not setting local storage
I am creating a Chrome packaged app. 我正在创建一个Chrome打包的应用。 I want to display a EULA the first time the user opens the app.
我想在用户第一次打开应用程序时显示EULA。 I want to us Chrome's local storage to test whether or not the app has been opened.
我想让我们使用Chrome的本地存储来测试该应用是否已打开。
Here is my javascript: 这是我的JavaScript:
function disagree(){
chrome.storage.local.set({eula:'false'}, function(result) {
console.log(result.eula)});
// console.log returns undefined
// Returns: TypeError: Cannot read property 'eula' of undefined
document.getElementById("eulaDisagree").style.display = "block";
}
function agree(){
chrome.storage.local.set({eula:'true'}, function(result) {
console.log("Set eula: " + result.eula);
// console.log returns undefined
// Returns: TypeError: Cannot read property 'eula' of undefined
chrome.storage.local.get("eula"), function(result){
console.log("Get Result: " + result.eula)};
// Returns this error: extensions::uncaught_exception_handler:8 Error in response to
// storage.set: Error: Invocation of form get(string) doesn't match definition
// get(optional string or array or object keys, function callback)
});
document.getElementById("background").style.height = "0%";
document.getElementById("foreground").style.height = "0%";
document.getElementById("foreground").style.border = "none";
}
// Creates event listeners
document.addEventListener('DOMContentLoaded', function(){
document.getElementById("disagree").addEventListener("click", function(){
disagree();
});
document.getElementById("agree").addEventListener("click", function(){
agree();
});
});
The problem is Chrome is not storing the data to local storage. 问题是Chrome无法将数据存储到本地存储中。
You didn't rightly use chrome.storage
api. 您没有正确使用
chrome.storage
api。 Take a look at the definition of StorageArea.get
and StorageArea.set
api. 看一下
StorageArea.get
和StorageArea.set
api的定义。
StorageArea.set(object items, function callback)
. StorageArea.set(object items, function callback)
callback function doesn't have a parameter StorageArea.get(string or array of string or object keys, function callback)
, you should put the callback as a parameter. StorageArea.get(string or array of string or object keys, function callback)
,应将回调作为参数。 So a sample usage looks like: 因此,用法示例如下:
chrome.storage.local.set({ eula: true });
chrome.storage.local.get('eula', function (results) {
console.log(results.eula);
});
I think the issue comes from two small mistakes. 我认为问题出在两个小错误上。
I believe when setting the key value pair 我相信在设置键值对时
chrome.storage.local.set({eula:'false'}, function() {});
eula should be a string {'eula': 'false'} this could be the reason it is not being set in the first place. eula应该是一个字符串{'eula':'false'},这可能是未首先设置它的原因。
the reason it is console.logging undefined is because set does not send a result parameter to the callback function that only happens with the get method. 它是console.logging未定义的原因是因为set不会将结果参数发送到仅通过get方法发生的回调函数。
If setting the eula to a string will set the storage object then your get method should stop throwing an error. 如果将eula设置为字符串将设置存储对象,则您的get方法应停止引发错误。
I appears that I had the syntax for the get
wrong. 我看来,我有语法的
get
错误。 I changed my javascript to the following: 我将JavaScript更改为以下内容:
function disagree(){
chrome.storage.local.set({'eula':'false'}, function() {
console.log("False")});
document.getElementById("eulaDisagree").style.display = "block";
}
function agree(){
chrome.storage.local.set({'eula':'true'}, function() {
var eulaV;
console.log("True");
chrome.storage.local.get('eula', function(result){
eulaV = result.eula;
console.log(eulaV);
});
});
document.getElementById("background").style.height = "0%";
document.getElementById("foreground").style.height = "0%";
document.getElementById("foreground").style.border = "none";
}
I am no longer getting any errors. 我不再遇到任何错误。 Everything works fine.
一切正常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.