bug in chrome.storage.local that seems to affect extensions only on Mac OS X?

徘徊边缘 提交于 2020-01-16 01:47:35

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!