问题
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