Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

359
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda