Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/426.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Chrome:runtime.onMessage的事件处理程序中出错_Javascript_Google Chrome_Google Chrome Extension - Fatal编程技术网

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

Javascript Chrome:runtime.onMessage的事件处理程序中出错,javascript,google-chrome,google-chrome-extension,Javascript,Google Chrome,Google Chrome Extension,我试图返回Chrome扩展中的所有窗口实例,但我得到: runtime.onMessage的事件处理程序中出错:错误:窗体windows.getAll()的调用与定义windows.getAll不匹配(可选对象getInfo,函数回调) 我已经将我的应用程序精简到最低限度,下面是我收到错误消息时的实际设置。这样做的目的是,当我按下空格键时,chrome.windows.getAll()会被调用,并且后台控制台中会显示一些内容。在发生错误之前,我确实在控制台中收到了“messagereceived

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

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

我已经将我的应用程序精简到最低限度,下面是我收到错误消息时的实际设置。这样做的目的是,当我按下空格键时,
chrome.windows.getAll()
会被调用,并且后台控制台中会显示一些内容。在发生错误之前,我确实在控制台中收到了“messagereceived”

你知道我做错了什么,甚至错误是什么意思吗?我在底部留下了确切的错误信息

我的Chrome版本是:
33.0.1750.152


manifest.json js/bg.js
完整错误消息 runtime.onMessage的事件处理程序中出现错误:错误:窗体windows.getAll()的调用与定义windows.getAll不匹配(可选对象getInfo,函数回调) 在Object.normalizeArgumentsAndValidate(扩展::schemaUtils:113:11) 反对。(扩展:绑定:307:30) 在铬-extension://aijclfleiopkccfielfjknhgpajnclah/js/bg.js:2:32 at Function.target.(匿名函数)(扩展::安全内置:19:14) 在Event.dispatchToListener(扩展::事件绑定:394:22) 在Event.dispatch(扩展::Event_绑定:378:27) 在Event.dispatch(扩展::Event_绑定:400:17) at messageListener(扩展::消息传递:192:31) at Function.target.(匿名函数)(扩展::安全内置:19:14) 在Event.dispatchToListener(扩展::Event_绑定:394:22)扩展::Event_绑定:382 Event.dispatch_u扩展::事件绑定:382 Event.dispatch扩展::事件绑定:400 messageListener扩展::消息传递:192 Event.dispatchToListener扩展::事件绑定:394 Event.dispatch_u扩展::Event_绑定:378 Event.dispatch扩展::事件绑定:400 DispatchOnMessageExtensions::消息传递:307
如果要获取所有选项卡信息,需要定义回调

试试这个:

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

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

完全做到了!有一点让我感到不舒服的是,它说它是可选的,所以我甚至没有想到去尝试
document.addEventListener('keydown', function(e){
    if(e.keyCode == 32)
        chrome.runtime.sendMessage({command: 'switch-window'})
})
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
chrome.windows.getAll({populate:true},function(wins){
  wins.forEach(function(win){
    win.tabs.forEach(function(tab){
      console.log(tab.url);
    });
  });
});