admin管理员组文章数量:1323342
I have two variables being set to the "return value" of functions, these functions are to get the URL of a tab, and a reference to the actual tab object, and store them in variables. I have some code:
function init(){
var url = getUrl();
var tab = getTab();
}
function getUrl(){
var tablink;
chrome.tabs.query({currentWindow: true, active: true},function(tabs){
tablink = tabs[0].url;
return tablink;
});
}
function getTab(){
var tab;
chrome.tabs.query({currentWindow: true, active: true},function(tabs){
tab = tabs[0];
});
return tab;
}
Why is it that, URL is undefined, though I'm returning the URL from within the callback function, yet when I return tab from outside of the callback, it returns fine, as though this were a synchronous call. I have a screenshot of this phenomenon in the debugger. I'm trying my best to learn how to deal with the asynchronous methods in chrome, but this is very confusing. Can someone explain this behavior to me?
I have two variables being set to the "return value" of functions, these functions are to get the URL of a tab, and a reference to the actual tab object, and store them in variables. I have some code:
function init(){
var url = getUrl();
var tab = getTab();
}
function getUrl(){
var tablink;
chrome.tabs.query({currentWindow: true, active: true},function(tabs){
tablink = tabs[0].url;
return tablink;
});
}
function getTab(){
var tab;
chrome.tabs.query({currentWindow: true, active: true},function(tabs){
tab = tabs[0];
});
return tab;
}
Why is it that, URL is undefined, though I'm returning the URL from within the callback function, yet when I return tab from outside of the callback, it returns fine, as though this were a synchronous call. I have a screenshot of this phenomenon in the debugger. I'm trying my best to learn how to deal with the asynchronous methods in chrome, but this is very confusing. Can someone explain this behavior to me?
Share Improve this question edited Feb 15, 2014 at 11:20 BenMorel 36.6k51 gold badges205 silver badges336 bronze badges asked Nov 17, 2013 at 8:57 aclave1aclave1 1,71218 silver badges29 bronze badges 1- 1 possible duplicate of After calling chrome.tabs.query, the results are not available – Rob W Commented Nov 17, 2013 at 9:50
1 Answer
Reset to default 7As 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./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);
}
本文标签: javascriptChrome Extension query tabs asyncStack Overflow
版权声明:本文标题:javascript - Chrome Extension: query tabs async - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742135543a2422351.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论