I'm trying to get the active tab's url when the user goes to a new website. The chrome web store has suggested that I remove the tabs permission and use activeTab instead. I don't have a content script and only need the active URL.
I currently get the active tab and save it to a global variable with the tabs permission:
let ACTIVE_URL = '';
chrome.tabs.onActivated.addListener((_activeInfo)=> {
chrome.tabs.query({active: true}, (tabs) => {
ACTIVE_URL = tabs[0].url || tabs[0].pendingUrl || ACTIVE_URL;
});
});
chrome.tabs.onUpdated.addListener((_tabID, _changeInfo, tab) => {
ACTIVE_URL = tab.url || ACTIVE_URL;
});
this question, and woxxom's comments seem to indicate that it's not possible. But according to activeTab documentation, activeTab allows you to
Intercept network requests in the tab to the tab's main frame origin using the webRequest API. The extension temporarily gets host permissions for the tab's main frame origin.
So it seems like this is possible (in place of placeholder chrome.getActiveUrl();).
chrome.webRequest.onBeforeRequest.addListener(
(webRequest) => {
const activeURL = ACTIVE_URL // chrome.getActiveUrl();
if (activeURL === url.im.looking.for) {
return {cancel: true};
}
return {cancel: false};
},
{
urls: ["http://*/*", "https://*/*"],
types: ['main_frame']
},
['blocking']
);
this question says it's not possible to use an async function inside of the onBeforeRequest listener.
How do you get the URL with activeTab inside of a webRequest callback fn?