chrome扩展名chrome.storage.local默认设置

时间:2019-02-18 19:26:54

标签: google-chrome google-chrome-extension google-chrome-storage

我正在为自己的工作场所开发Chrome扩展程序,并具有许多默认设置,这些默认设置是在安装时使用事件chrome.runtime.onInstalled在chrome.storage.local中创建的。

但是,我敢肯定,这意味着每当我推送该扩展程序的新版本时,用户的设置都会被默认设置覆盖。我找到了看起来可以解决此问题的Object.assign方法,但是我无法弄清楚如何在不诉诸实际更新扩展名的情况下测试代码。

卸载该扩展包的解压缩版本似乎会导致其chrome.storage.local密钥被破坏(对此我可能是错的),因此我无法在重新安装时验证它是否会保留我之前更改的任何设置

我当前的“ onInstalled”代码如下所示:

chrome.runtime.onInstalled.addListener(function() {
    chrome.storage.local.get(["options"], function(r){

        let defaults = {
            option1: 2000,
            option2: true,
            option3: false,
            option4: ""
        },
        current = r.options,
        newSettings = Object.assign(defaults,current);

        chrome.storage.local.set({
            options: newSettings
        });
    });
});

一个后续问题:是否有必要为我的选项命名,还是设置了chrome.storage.local以便不同扩展名中的同名设置不会互相干扰?

0 个答案:

没有答案