Chrome扩展程序设置

时间:2015-01-18 03:02:24

标签: javascript jquery html google-chrome google-chrome-extension

我需要创建一个只运行一次的javascript函数,这样它就会设置一堆google chrome变量,有人知道怎么做吗?

这就是我现在所拥有的,但我认为它不起作用:

window.onLoad = check();

function check() {
    chrome.storage.local.get('defaultLoad', function (x) {
        if (x.defaultLoad == undefined) {
            chrome.storage.local.set({'defaultLoad': '0'}, function() {
                console.log('SearchPanel default');
                defaultLoad();
            });
        }
    });
}

function defaultLoad() {
    chrome.storage.local.set({'searchPanelOption': 'open'}, function() {
            console.log('SearchPanel default');
    });
    chrome.storage.local.set({'barBackgroundOption': '#000000'}, function() {
            console.log('barBackground default');
    });
    chrome.storage.local.set({'goBackgroundOption': '#000000'}, function() {
            console.log('goBackground default');
    });
    chrome.storage.local.set({'barColorOption': '#ffffff'}, function() {
            console.log('barColor default');
    });
    chrome.storage.local.set({'placeColorOption': '#999999'}, function() {
            console.log('placeColor default');
    });
    chrome.storage.local.set({'searchEngineOption': 'google'}, function() {
            console.log('searchEngine default');
    });
}

该脚本在页面顶部调用,它应该只运行一次,但我认为它不起作用,因为我没有得到任何console.log输出。

3 个答案:

答案 0 :(得分:2)

所以你想设置默认值。

在分隔文件中定义默认值。

// Define default values.
var options = {
    notifyTimeout: 5,
    pageInspect: true,
    linkInspect: true,
    pushItem: false,
    notifyMode: 'margin',
};

// a method similar to jquery.ready
function initOptions(callback) {
    // get data from remote
    chrome.storage.sync.get(null, function(data) {
        // merge remote data into local global `options`
        $.extend(options, data);
        // push them back to remote
        // maybe the extension updated and added new default values.
        chrome.storage.sync.set(options);
        // callback method to use updated options.
        callback && callback();
    });
}

// listen to remote data changes, if soem value changed
// update the global variable `options`, for example,
// you changed an option in options page and push changes to remote
// in contentscript, this got the changes and update to new value.
// you can use the updated value without a page reload.
chrome.storage.onChanged.addListener(function(changes) {
    for (var name in changes) {
        var change = changes[name];
        options[name] = change.newValue;
    }
});

在您要使用选项的页面中,注入此脚本并随意使用全局变量options,它也适用于内容脚本。

initOptions(function() {
   doSomethingWith(options); 
});

我写了一篇关于此的文章,但不是用英文写的。

http://www.g2w.me/2014/08/sync-chrome-extension-options/

答案 1 :(得分:1)

只有在初始安装时才会运行事件:

chrome.runtime.onInstalled.addListener(function(details) {
  if(details.reason == "install") {
    // This code runs once
  }
});

但是,方法presented by greatghoul比设置默认选项更可取。它将覆盖任何未使用默认选项设置的选项,并保留已设置的任何选项的值。这样做更好,因为之后添加更多选项与向默认选项添加新密钥一样简单。

答案 2 :(得分:0)

没关系,这段代码确实有效,你只需要将函数调用defaultLoad()移出chrome.local.storage.set调用。

window.onLoad = check();

function check() {
    chrome.storage.local.get('defaultLoad', function (x) {
        if (x.defaultLoad == undefined) {
            chrome.storage.local.set({'defaultLoad': '0'}, function() {
                console.log('SearchPanel default');
            });
            defaultLoad();
        }
    });
}

function defaultLoad() {
    chrome.storage.local.set({'searchPanelOption': 'open'}, function() {
            console.log('SearchPanel default');
    });
    chrome.storage.local.set({'barBackgroundOption': '#000000'}, function() {
            console.log('barBackground default');
    });
    chrome.storage.local.set({'goBackgroundOption': '#000000'}, function() {
            console.log('goBackground default');
    });
    chrome.storage.local.set({'barColorOption': '#ffffff'}, function() {
            console.log('barColor default');
    });
    chrome.storage.local.set({'placeColorOption': '#999999'}, function() {
            console.log('placeColor default');
    });
    chrome.storage.local.set({'searchEngineOption': 'google'}, function() {
            console.log('searchEngine default');
    });
}