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

128
Visualizações
Detecting active tab in Chrome background script

I want to detect in a Chrome extension background script what is the active browser tab with focus if any.

I have the code below that works in almost all cases. However, if I restore a minimized app (eg. Outlook) that hides the browser window then the onFocusChange event doesn't fire.

chrome.windows.onFocusChanged.addListener((windowId) => {
    console.log('On focus change: ' + windowId);
    handleActiveTab();
});

chrome.tabs.onActivated.addListener((e) => {
    console.log('On activated', e);
    handleActiveTab();
});
chrome.tabs.onUpdated.addListener((e) => {
    console.log('On updated', e);
    handleActiveTab();
});

const handleActiveTab = () => {
    chrome.tabs.query({ active: true, currentWindow: true, lastFocusedWindow: true }, function (tabs) {
        if (tabs[0]) {
            chrome.windows.get(tabs[0].windowId, (w) => {
                if (w && w.focused) {
                    console.log('ACTIVE TAB -> ' + tabs[0].url);
                } else {
                    console.log('ACTIVE TAB WITHOUT FOCUS -> ' + tabs[0].url);
                }
            });
        } else {
            console.log('NO ACTIVE TAB');
        }
    });
};

If I call my handleActiveTab with a timer then it detects, that the browser tab lost the focus. The window.focused property changes. But why the onFocusChanged event is not fired?

As a workaround, I can use the solution with a timer, but I would like to avoid it if possible.

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