Chrome:runtime.onMessage的事件处理程序出错

时间:2014-03-21 03:26:04

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

我试图在Chrome扩展程序中返回所有窗口实例,但我得到:

  

runtime.onMessage的事件处理程序出错:错误:调用窗体windows.getAll()并不匹配定义windows.getAll(可选对象getInfo,函数回调)

我已将我的应用程序剥离到最低限度,以下是我收到错误消息时的实际设置。目的是当我按空格键时,调用chrome.windows.getAll()并在后台控制台中显示某些内容。我收到"收到消息"在错误之前的控制台中。

任何想法我做错了什么,甚至错误意味着什么?我在底部留下了确切的错误消息

我的Chrome版本为:33.0.1750.152


的manifest.json

{
    "manifest_version": 2,

    "name":         "Alt Tilde",
    "version":      "0.0.1",

    "background": {
        "scripts":  ["js/bg.js"]
    },

    "content_scripts": [{
        "matches":  ["<all_urls>"],
        "js":       ["js/content.js"]
    }]
}

JS / content.js

document.addEventListener('keydown', function(e){
    if(e.keyCode == 32)
        chrome.runtime.sendMessage({command: 'switch-window'})
})

JS / bg.js

chrome.runtime.onMessage.addListener(function(){
    console.log('Message received')
    console.log(chrome.windows.getAll())
})

完整错误消息

Error in event handler for runtime.onMessage: Error: Invocation of form windows.getAll() doesn't match definition windows.getAll(optional object getInfo, function callback)
    at Object.normalizeArgumentsAndValidate (extensions::schemaUtils:113:11)
    at Object.<anonymous> (extensions::binding:307:30)
    at chrome-extension://aijclfleiopkccfielfjknhgpajnclah/js/bg.js:2:32
    at Function.target.(anonymous function) (extensions::SafeBuiltins:19:14)
    at Event.dispatchToListener (extensions::event_bindings:394:22)
    at Event.dispatch_ (extensions::event_bindings:378:27)
    at Event.dispatch (extensions::event_bindings:400:17)
    at messageListener (extensions::messaging:192:31)
    at Function.target.(anonymous function) (extensions::SafeBuiltins:19:14)
    at Event.dispatchToListener (extensions::event_bindings:394:22) extensions::event_bindings:382
Event.dispatch_ extensions::event_bindings:382
Event.dispatch extensions::event_bindings:400
messageListener extensions::messaging:192
Event.dispatchToListener extensions::event_bindings:394
Event.dispatch_ extensions::event_bindings:378
Event.dispatch extensions::event_bindings:400
dispatchOnMessage extensions::messaging:307

1 个答案:

答案 0 :(得分:0)

如果您想获取所有标签信息,则需要定义回调。

试试这个:

chrome.windows.getAll({populate:true},function(wins){
  wins.forEach(function(win){
    win.tabs.forEach(function(tab){
      console.log(tab.url);
    });
  });
});

如果您想获取标签信息,请记住在清单中添加“标签”权限。