Sharlie Sharlie - 5 months ago 35
Javascript Question

Chrome not setting local storage

I am creating a Chrome packaged app. I want to display a EULA the first time the user opens the app. I want to us Chrome's local storage to test whether or not the app has been opened.

Here is my 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.

Answer

I appears that I had the syntax for the get wrong. I changed my javascript to the following:

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.

Comments