Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

358
Views
How do you get the URL with activeTab inside of a webRequest callback fn?

Objective

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.

Current Code

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;
});

Docs say it's possible?

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']
);

Async alternatives

this question says it's not possible to use an async function inside of the onBeforeRequest listener.

Question

How do you get the URL with activeTab inside of a webRequest callback fn?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!