I am working on a chrome plugin project and got stuck in a javascript problem(I am new to js). here is the scenario:
chrome.webRequest.onBeforeRequest.addListener(
function(details) {
var currentTab = null;
// this is callback and async
chrome.tabs.query(
{highlighted: true},
function(tab){
currentTab = tab[0].url;
}
)
// this is not working, currentTab won't be set before used here.
if(details.url == currentTab){
return {cancel: false};
}else{
return {cancel: true};
}
}
)
so... it doesn't work obviously. but due to the fact that I can't change how those API functions were designed, I can't re-construct them in the way that it would work easily.
both chrome.webRequest.onBeforeRequest.addListener
and chrome.tabs.query
don't return a value, but only accept callbacks.
and chrome.webRequest.onBeforeRequest.addListener
will behave based on the return of the callback {cancel: true/false}
I tried spinning lock to create a wait barrier, but it blocks the thread and ends up been freezing the whole thing:
while(currentTab == null) {} // blocks the thread, chrome.tabs.query can't even be executed, infinite loop here...
if(details.url == currentTab){
return {cancel: false};
}else{
return {cancel: true};
}
anyone knows how to hold up the return of a function based on a value set inside another function(callback) without blocking the thread?