I am programming a chrome extension. It works good but i have a problem getting the value.
On installation it should set a default value. But here comes the problem: I cant get the value. logValue() should give the value to the console for testing, which it takes from getValue(). In the getValue() function, it works but if I return the value, it is undefined. (Details in the screenshot below.
What is my fault and how can I do it, that the value and type in the logValue() function is the same as in the getValue()?
And I am sorry for my bad english:)
Here you see, in the getValue() function it has the right value (1st time null and 2nd time the string of the object. But if I return it to the logValue() function which replaces a function to work with it, the value is only undefined.
And here is my code:
function logValue() {
var x = getValue();
y = JSON.stringify(x);
console.log("Type: " + typeof (x) + "\nvalue: " + x);
}
function getValue() {
debugger;
chrome.storage.sync.get(['testObject'], function (result) {
var value = result.testObject;
console.log("Type: " + typeof (value) + "\nvalue: " + value);
return value;
});
}
function setDefaultValue(data) {
debugger;
data = JSON.stringify(data);
chrome.storage.sync.set({
'testObject': data
}, function () {
console.log('Value is set to ' + data);
});
}
chrome.runtime.onInstalled.addListener(() => {
debugger;
chrome.storage.sync.set({
'testObject': null
}, function () {});
data = {
"data": [{
"name": "Test1",
"value": "Value1"
},
{
"name": "Test2",
"value": "Value2"
}
]
};
logValue();
setDefaultValue(data);
logValue();
});