Sync Call in Google Chrome Extension

I am working on a Google Chrome extension that should block / redirect some outgoing requests. For this purpose I use a chrome.webRequest.onBeforeRequest listener. To decide whether to block the request or not, I need some information about the tab request. I can get it using chrome.tabs.get(integer tabId, function callback) , but the callback is asynchronous, which means that it can be called after the value is returned from the onBeforeRequest listener.

 chrome.webRequest.onBeforeRequest.addListener(function(details){ chrome.tabs.get(details.tabId, function(tab){ // get info from tab }); // based on info from tab return redirect or not }), { urls: ["<all_urls>"], types: ["main_frame"] }, ["blocking"]); 

Is there a way to sync a call? Or maybe another option.

+7
source share
1 answer

Another answer to Stack Overflow recommends tracking tabs outside your listening function, which avoids this problem completely.

Code example:

 /* * -------------------------------------------------- * Keep list of tabs outside of request callback * -------------------------------------------------- */ var tabs = {}; // Get all existing tabs chrome.tabs.query({}, function(results) { results.forEach(function(tab) { tabs[tab.id] = tab; }); }); // Create tab event listeners function onUpdatedListener(tabId, changeInfo, tab) { tabs[tab.id] = tab; } function onRemovedListener(tabId) { delete tabs[tabId]; } // Subscribe to tab events chrome.tabs.onUpdated.addListener(onUpdatedListener); chrome.tabs.onRemoved.addListener(onRemovedListener); /* * -------------------------------------------------- * Request callback * -------------------------------------------------- */ // Create request event listener function onBeforeRequestListener(details) { // *** Remember that tabId can be set to -1 *** var tab = tabs[details.tabId]; // Respond to tab information } // Subscribe to request event chrome.webRequest.onBeforeRequest.addListener(onBeforeRequestListener, { urls: ["<all_urls>"], types: ["main_frame"] }, ["blocking"]); 
+11
source

All Articles