ОК, я, наконец, получил это, что работает: manifest.json
"background": {
"persistent": false,
"scripts": [ "js/storage.js", "js/backgroundlib.js", "js/background.js" ]
},
в background.js, контекстное меню встроена функция обратного вызова при чтении из хранилища. Это чтение вызывается, когда onInstalled уволен. Я использую глобальный var, который сохраняется вSuspend и снова читается onStartup. и которые связывают подменю id и соответствующую строку с опцией пользователя. Тест onClick-слушателя, если определена глобальная переменная. Если он не читается снова из хранилища.
var regex = new Object();
chrome.runtime.onInstalled.addListener(function() {
console.log("onInstalled called");
var key = 'jsondata';
storage.area.get(key, function (items){ get_jsondata(items[key]);});
function get_jsondata(value){
var data = JSON.parse(value);
var fcb =[ {fcb_context: "fcb_copy", title:"Copy filtered", context: ["selection", "link"]}, {fcb_context:"fcb_paste", context:["editable"], title:"Paste filtered"}];
for (var i=0; i<fcb.length; i++) {
var menu = fcb[i];
chrome.contextMenus.create({
//title: "Look up: %s",
title: menu.title,
id: menu.fcb_context,
contexts: menu.context,
});
var last = data.length;
//var sel = info.selectionText;
for (var j=0; j<last; j++){
chrome.contextMenus.create({
title: data[j].name,
contexts: menu.context,
id: menu.fcb_context + "_" + j,
parentId: menu.fcb_context,
//onclick: function(info, tab){ run_cmd(data[j].regex, info, menu.fcb_context); }
});
regex[ menu.fcb_context + "_" + j] = data[j];
//console.log(regex[menu.fcb_context + "_" + j]);
}// for j
} // for i
}//get_jsondata
}); //add listener
chrome.contextMenus.onClicked.addListener(function(info, tabs){
var id = info.menuItemId;
if (typeof regex === "undefined"){
storage.area.get("regex", function(items){
regex = JSON.parse(items["regex"]);
console.log("get " + items["regex"] + " from storage");
run_cmd(regex, info);
});
} else {
console.log("regex was defined... " + JSON.stringify(regex));
run_cmd(regex, info);
}
});
chrome.runtime.onSuspend.addListener(function() {
// Do some simple clean-up tasks.
console.log("onSuspend called saving " + JSON.stringify(regex));
storage.area.set({ "regex" : JSON.stringify(regex)}, function(){console.log("regex saved");});
});
chrome.runtime.onStartup.addListener(function() {
console.log("onStartup called");
storage.area.get("regex", function(items){
regex = JSON.parse(items["regex"]);
console.log("get " + items["regex"] + " from storage");
});
});
function getSelectedText(info){
var sel = info.selectionText;
chrome.tabs.executeScript(null, {file:"js/script.js"});
}
function pasteFilteredText(info){
chrome.tabs.executeScript(null, {file:"js/script.js"});
}
function run_cmd(regex, info){
var id = info.menuItemId;
var data = regex[id];
var sel = info.selectionText;
var fcb_context = info.parentMenuItemId;
//console.log("run_cmd regex " + data.regex + " sel " + (sel ? sel : ""));
alert("run_cmd regex " + data.regex + " sel " + (sel ? sel : "") + " fcb_context: " + fcb_context);
}
Спасибо, что указали мне, что лишнее или отсутствует.
'storage.area' определяется в js/storage.js на' chrome.storage.local', поэтому я бы сказал, что здесь нет проблемы – fraz
@fraz, что у вас есть? Был ли 'console.log (items [key])' выводить что-нибудь? –