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

126
Views
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
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!