Google Chrome中的动态上下文菜单。运行时错误?

时间:2015-05-24 18:48:18

标签: javascript google-chrome google-chrome-extension runtime-error contextmenu

下面给出的代码适用于2个选项但在后续选择时崩溃。

Content.js -

document.addEventListener('selectionchange', function(event){
            var selected = window.getSelection().toString().trim();
            chrome.extension.sendMessage({
                   'message': 'updateContextMenu', 
                   'selection': selected});
}, true);

BackgroundScript.js -

function SelectionType(str) {
  if (str.match("^[0-9]+$"))
      return "number";
  else if (str.match("^[a-z]+$"))
      return "string";
  else
      return "other";
}
var optionOne = {'enabled': true, 
        'title': "Some Title One '%s'", 
        "contexts": ["selection"],
        'onclick': functionOne
        };
var optionTwo = {'enabled': true, 
        'title': "Some Title Two '%s'", 
        "contexts": ["selection"],
        'onclick': functionTwo
        };
var cmid = null;
// a message listener that will modify the context menu
chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
    if (request.message == 'updateContextMenu') {
        if (request.selection !='') {
          var type = SelectionType(request.selection);
          if (type == "number") 
                if(cmid != null) {
                    chrome.contextMenus.update(cmid, optionOne); 
                }
                else {
                    cmid = chrome.contextMenus.create(optionOne);
                }
          else if (type == "string")
                if (cmid != null) {
                    chrome.contextMenus.update(cmid, optionTwo);
                }
                else {
                    cmid = chrome.contextMenus.create(optionTwo);   
                }
        } else if (cmid != null) {
            chrome.contextMenus.remove(cmid);
            cmid = null;
        }
    } else {
        sendResponse({});
    }
});
function functionOne(info, tab) {
    console.log("Function One");

    chrome.tabs.create({ 
        url: "some url" + info.selectionText,
    })
}

function functionTwo(info, tab) {
    console.log("Function Two");

    chrome.tabs.create({ 
        url: "some url" + info.selectionText,
    })  
}

创建上下文菜单时出现以下错误。我不知道为什么会崩溃。

  

runtime.onMessage的事件处理程序出错:错误:参数1的值无效。属性'generatedId':意外的属性。       在chrome-extension://acpffpbkehpfofhgilcophibgbkhmmba/script.js:46:42handler @ extensions :: uncaught_exception_handler:8exports.handle @ extensions :: uncaught_exception_handler:100EventImpl.dispatch_ @ extensions :: event_bindings:384EventImpl.dispatch @ extensions :: event_bindings:401target 。(匿名函数)@ extensions :: SafeBuiltins:19publicClass。(匿名函数)@ extensions :: utils:94messageListener @ extensions :: messaging:188target。(匿名函数)@ extensions :: SafeBuiltins:19EventImpl.dispatchToListener @ extensions :: event_bindings :395target。(匿名函数)@ extensions :: SafeBuiltins:19publicClass。(匿名函数)@ extensions :: utils:94EventImpl.dispatch_ @ extensions :: event_bindings:379EventImpl.dispatch @ extensions :: event_bindings:401target。(匿名函数)@ extensions :: SafeBuiltins:19publicClass。(匿名函数)@ extensions :: utils:94dispatchOnMessage @ extensions :: messaging:316

0 个答案:

没有答案