【问题标题】:Access chrome local storage used in both content script and popup?访问内容脚本和弹出窗口中使用的 chrome 本地存储?
【发布时间】:2015-03-01 20:16:47
【问题描述】:

我正在设计我的第一个 chrome 扩展程序,但遇到了问题。

在我的 popups.js 上,用户可以输入以数组形式保存在 chrome 存储中的单词和定义:

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

然后我想在我的内容脚本 js 中加载这些单词和定义,如下所示:

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;
    });
}

但是它根本不加载数据。我已确保在运行内容脚本之前保存数据。

我已经研究过消息传递,但并不真正理解它/不知道这是否是正确的方法。

帮助表示赞赏!

【问题讨论】:

    标签: google-chrome local-storage content-script


    【解决方案1】:

    解决了!

    我现在使用同步而不是本地:chrome.storage.sync

    在我将“start()”放入其中之后,我也没有等待加载存储:

    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()
        });
    }
    

    成功了。

    【讨论】:

    • 您应该将解决方案代码放入您的答案中。您的代码清单具有本地性。
    猜你喜欢
    • 2016-12-21
    • 1970-01-01
    • 2016-04-05
    • 1970-01-01
    • 2011-08-31
    • 2017-02-15
    • 2017-12-24
    • 2013-10-12
    • 2017-03-31
    相关资源
    最近更新 更多