问题
I am trying to set, and later request the data stored locally in an extension
The way I am doing this is sending a request from the content script as follows
chrome.runtime.sendMessage({method: "fetchData"}, doSomething)
On the background page, the message is received and parsed by:
chrome.runtime.onMessage.addListener(getData);
where getData is a function as follows:
var getData = function(message, sender, sendResponse) {
if (message.method == "fetchData"){
chrome.storage.local.get('myinfo',function(items){
console.log(items);
if (items.myinfo){
sendResponse({status: items.myinfo});
} else {
sendResponse({status: items.myinfo});
}
}); //storage.local.get
}
return true;
}
Now the first time this is run, storage is empty, so items is an empty object {}
Therefore, items.myinfo is undefined
This is indeed the case when I test on my chrome installation on Linux.
However, when running the extension on chrome on a Mac ( OS X 10.9.2 header below)...
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.131 Safari/537.36 module=default version=1
...the background page spits up the following error:
Error in response to storage.get: TypeError: Cannot read property 'myinfo' of undefined
It turns out that in this instance items is undefined, not an empty object.
I am trying to figure out if checking for an undefined items, and then populating chrome.storage.local with some data...
storage.set({'myinfo': message.key}, function() {
});
... helps resolve the issue, or if it continues to persist.
Why is this behavior of chrome.local.storage.get different on different OS's. Am I missing something obvious?
来源:https://stackoverflow.com/questions/23405121/bug-in-chrome-storage-local-that-seems-to-affect-extensions-only-on-mac-os-x