Chrome tabs query returning empty tab array

Sounds like you’re running this code while your active window is devtools, which is a known bug.

Another problem takes place in your code: it always accesses the focused (active) tab even though the request may have occurred in a backgrounded (inactive) tab.

Solution:

Use the tab id provided inside the listener function parameter as tabId property.
In your case it’s requestDetails.tabId

If for whatever reason you really want the active tab, make sure a real window is active while this code runs or use the following workaround that succeeds even if devtools window is focused:

chrome.windows.getCurrent(w => {
  chrome.tabs.query({active: true, windowId: w.id}, tabs => {
    const tabId = tabs[0].id;
    // use tabId here...
  });
});

Leave a Comment