Access chrome local storage used in both content script and popup?

人盡茶涼 提交于 2020-01-07 01:22:50

问题


I'm designing my first chrome extension and have run into a problem.

On my popups.js users can enter words and definitions that are saved as an array in chrome storage:

function save()
{
     chrome.storage.local.set({'words': words});
    chrome.storage.local.set({'definitions': definitions});
}

I would like to then load these words and definitions in my content script js like so:

function load()
{
    //load words from local storage
    chrome.storage.local.get('words', function (result) {
    if (result.words != "null") words = result.words;
    });

    //load definitions
    chrome.storage.local.get('definitions', function (result) {
        if (result.definitions != "null") definitions = result.definitions;
    });
}

However it does not load the data at all. I have made sure the data is saved before the content script is run.

I have looked into messaging but don't really understand it / know if that's even the right way to do it.

Help appreciated!


回答1:


SOLVED!

I now use sync instead of local: chrome.storage.sync

also I wasn't waiting for the storage to be loaded in, after I put my "start()" inside:

function load()
{
    //load words from local storage
    chrome.storage.local.get('words', function (result) {
    if (result.words != "null") words = result.words;
    });

    //load definitions
    chrome.storage.local.get('definitions', function (result) {
        if (result.definitions != "null") definitions = result.definitions;

start()
    });
}

it worked.



来源:https://stackoverflow.com/questions/28793173/access-chrome-local-storage-used-in-both-content-script-and-popup

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