admin管理员组文章数量:1403438
I'm writing a Chrome extension for my own use. I'm querying the current tab's index from an injected script like this:
[injected script]
chrome.runtime.sendMessage({whatIsMyIndex:1}, function(response){
var myIndex = response.index;
});
[background script]
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if (request.whatIsMyIndex){
sendResponse({index: sender.tab.index});
}
}
);
Now this is all good and fine, but then I need to return the url of the first tab as well:
[injected script 2]
chrome.runtime.sendMessage({whatIsMyIndex:1}, function(response){
var myIndex = response.index;
var url = response.url;
});
[background script 2]
var url;
//this uses sendResponse when the requested values arrive
function respond(sendResponse, index){
if(typeof(url)!="undefined"){
sendResponse({index:index, url:url});
} else {
setTimeout(respond, 15, sendResponse, index);
}
}
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if (request.whatIsMyIndex){
chrome.tabs.query({index:0, currentWindow:true}, function(tabs){
url=tabs[0].url;
}
setTimeout(respond, 15, sendResponse, sender.tab.index);
return true; //so i can use sendResponse later
}
}
);
Now, this code works just fine. My question is, is there any way to acplish this without the need for setTimeout? Adding a fixed 15ms delay doesn't seem right.
If only I could get chrome.tabs.query's callback to take my sendResponse() as a parameter...
(Storing sendResponse() in a global variable is not possible, since there will be ~20 tabs doing this at the same time, and all of them need their own responses.)
I don't need any working (or not working) code here, only some ideas/guidelines.
I'm writing a Chrome extension for my own use. I'm querying the current tab's index from an injected script like this:
[injected script]
chrome.runtime.sendMessage({whatIsMyIndex:1}, function(response){
var myIndex = response.index;
});
[background script]
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if (request.whatIsMyIndex){
sendResponse({index: sender.tab.index});
}
}
);
Now this is all good and fine, but then I need to return the url of the first tab as well:
[injected script 2]
chrome.runtime.sendMessage({whatIsMyIndex:1}, function(response){
var myIndex = response.index;
var url = response.url;
});
[background script 2]
var url;
//this uses sendResponse when the requested values arrive
function respond(sendResponse, index){
if(typeof(url)!="undefined"){
sendResponse({index:index, url:url});
} else {
setTimeout(respond, 15, sendResponse, index);
}
}
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if (request.whatIsMyIndex){
chrome.tabs.query({index:0, currentWindow:true}, function(tabs){
url=tabs[0].url;
}
setTimeout(respond, 15, sendResponse, sender.tab.index);
return true; //so i can use sendResponse later
}
}
);
Now, this code works just fine. My question is, is there any way to acplish this without the need for setTimeout? Adding a fixed 15ms delay doesn't seem right.
If only I could get chrome.tabs.query's callback to take my sendResponse() as a parameter...
(Storing sendResponse() in a global variable is not possible, since there will be ~20 tabs doing this at the same time, and all of them need their own responses.)
I don't need any working (or not working) code here, only some ideas/guidelines.
Share Improve this question asked Sep 3, 2013 at 12:46 proto-nproto-n 6257 silver badges21 bronze badges1 Answer
Reset to default 8Just call sendResponse
inside the chrome.tabs.query
callback, instead of setting the variables:
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if (request.whatIsMyIndex){
chrome.tabs.query({index:0, currentWindow:true}, function(tabs){
sendResponse({index:sender.tab.index, url:tabs[0].url});
});
return true; //so i can use sendResponse later
}
}
);
Each invocation of the onMessage
callback function creates a new closure, so the sendResponse
inside the tabs.query
callback function will be bound to the right function.
本文标签: javascriptBest way to wait for a chrometabsquery callbackStack Overflow
版权声明:本文标题:javascript - Best way to wait for a chrome.tabs.query callback? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744350615a2602027.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论