使用存储数据定义子菜单项



我的扩展应该使用用户的选项在主扩展上下文菜单项下构建子菜单。选项存储在一个表中,每一行都定义了一个子菜单。整个表以json字符串的形式存储在chrome.local.storage中,关键字为jsondata

清单是:

   "background": {
  "persistent": true,
  "scripts": [ "js/storage.js", "js/backgroundlib.js", "js/background.js" ]
},
...
"permissions": [ "storage", "contextMenus", "http://*/*", "https://*/*",   "tabs", "clipboardRead", "clipboardWrite" ],
...

在后台脚本中,我试图使用获取数据

window.addEventListener('load', function () {
var key = 'jsondata';
 storage.area.get(key, function (items){ 
     console.log(items[key]);
     build_submenu(items[key]);}); 
 }); 
function build_submenu(json) {
     console.log("build_submenu: " + json);
 }

并且CCD_ 3应当调用多个CCD_。目前,我无法调用build_submenu。我是在尝试做一些不可能的事情,还是只是错过了一些显而易见的事情?

谢谢,F.

storage.area.get替换为chrome.storage.local.get

另一个建议是删除外部window.onload侦听器,因为您使用的是后台脚本,而window.onload没有任何意义。

好吧,我终于得到了这个,它起作用了:manifest.json

   "background": {
  "persistent": false,
  "scripts": [ "js/storage.js", "js/backgroundlib.js", "js/background.js" ]
  },

在background.js中,上下文菜单是在从存储读取时在回调函数中构建的。当onInstalled被激发时,会调用此读取。我使用保存在Suspend上的全局变量,并在Startup上再次读取。并且将子菜单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);
}

谢谢你告诉我什么是多余的或缺少的。

相关内容

  • 没有找到相关文章

最新更新