2017-08-04 32 views
-1

我打电话从上下文菜单下面的脚本作为测试,以更好地理解消息传递:如何从chrome.tabs.query()返回信息?

console.log(chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {return tabs[0].id})) 

但是,调用此方法返回undefined后检查控制台,我不能从控制台页面,因为这在运行此返回一个错误。

此外,运行下面也返回一个undefined

console.log(chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {return "1"})) 

任何援助表示赞赏。

+3

铬API回调是异步的,因此该方法本身不返回任何东西。在回调中使用检索的值和console.log。 – wOxxOm

回答

0

这可以通过使用回调如下内检索值修正:

chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {console.log(tabs[0].id)})