0

我的扩展应该使用用户的选项在主扩展上下文菜单条目下构建子菜单。选项存储在一个表中,其中每一行定义一个子菜单。整个表存储为chrome.local.storage带有 key的 json 字符串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);
 }

然后build_submenu应该调用多个chrome.contextMenus.create({... })来添加子菜单。目前,我无法接到build_submenu电话。我是在尝试做一些不可能的事情,还是我只是错过了一些明显的事情?

谢谢,F。

4

3 回答 3

0

好的,我终于明白了,它有效:manifest.json

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

在 background.js 中,上下文菜单是在从存储读取时的回调函数中构建的。触发 onInstalled 时会调用此读数。我使用保存在 onSuspend 上的全局变量并在 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);

}

感谢您指出什么是多余的或缺少的。

于 2016-04-08T10:26:52.303 回答
0

取得了一些进展:在 background.js 中,我访问了存储中的 json 字符串

var key = 'jsondata';
var data;
storage.area.get(key, function (items){ get_jsondata(items[key]);}); 

回调函数构建菜单项和子菜单。

function get_jsondata(value){
  console.log("get_jsondata " + value);
  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: menu.title,
    id: menu.fcb_context,
    contexts: menu.context,
    });
    var last = data.length;
    for (var j=0; j<last; j++){
        chrome.contextMenus.create({ 
          title: data[j].name, 
          contexts: menu.context, 
          parentId: menu.fcb_context,
          onclick:  function(info, tab){ run_cmd( data[j].regex, info, menu.fcb_context ); }
        });
    }// for j
  } // for i
}//get_jsondata

function run_cmd(regex, info, fcb_context){
    var sel = info.selectionText;
    console.log("run_cmd regex " + regex + " sel " + (sel ? sel : ""));
}

现在我遇到的问题是,数据虽然定义为顶级变量,但在单击其中一个子菜单时似乎未定义。

我收到错误消息“无法读取未定义的属性正则表达式”

于 2016-04-07T12:07:30.440 回答
0

替换storage.area.getchrome.storage.local.get

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

于 2016-04-07T02:04:07.983 回答