更新时间:2023-12-05 19:25:10
正如你已经说 chrome.tabs.query
函数是异步的。正因为如此,你不能依靠收益
,相反,你必须使用回调。对于Chrome扩展的文档解释了它相当不错: http://developer.chrome.com /extensions/overview.html#sync-example
As you already said the chrome.tabs.query
function is asynchronous. Because of this, you cannot rely on return
, instead you have to use callbacks. The documentation for Chrome extension explains it quite well: http://developer.chrome.com/extensions/overview.html#sync-example
所以你的情况,这样的事情可能工作(取决于你想以后做什么用)。
So in your case, something like this might work (depends on what you want to later with it).
var url, tab;
function init(){
chrome.tabs.query({currentWindow: true, active: true},function(tabs){
url = tabs[0].url;
tab = tabs[0];
//Now that we have the data we can proceed and do something with it
processTab();
});
}
function processTab(){
// Use url & tab as you like
console.log(url);
console.log(tab);
}