Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tabs cannot be queried right now (user may be dragging a tab)

I have a chrome extension that accesses the active tab via this code:

    chrome.tabs.query({ active: true }, (result) => { ... })

This has worked super well until a recent update, where I am no longer able to query the tab, with the following error printed in the console:

Tabs cannot be queried right now (user may be dragging a tab).

I tried this but it does not work. Any suggestions?

like image 303
Nicholas Avatar asked Jun 03 '21 13:06

Nicholas


People also ask

How do I drag a tab in Chrome?

Press Alt + Tab to move between windows. Right-click on a tab and select Move tab to another window.

How do I restore Chrome tabs?

Chrome keeps the most recently closed tab just one click away. Right-click a blank space on the tab bar at the top of the window and choose Reopen closed tab. You can also use a keyboard shortcut to accomplish this: CTRL + Shift + T on a PC or Command + Shift + T on a Mac.

Should the tab become the active tab in the window?

Whether the tab should become the active tab in the window. Does not affect whether the window is focused (see windows.update ). Defaults to true. The position the tab should take in the window. The provided value is clamped to between zero and the number of tabs in the window. The ID of the tab that opened this tab.

Why doesn't my Tab have a session ID?

Under some circumstances a tab may not be assigned an ID; for example, when querying foreign tabs using the sessions API, in which case a session ID may be present. Tab ID can also be set to chrome.tabs.TAB_ID_NONE for apps and devtools windows.

What happens when the selected tab in a window changes?

Fires when the selected tab in a window changes. Note that the tab's URL may not be set at the time this event fired, but you can listen to tabs.onUpdated events so as to be notified when a URL is set. The ID of the window the selected tab changed inside of.

Why doesn't the tabs tab return a promise?

The tabs.Tab object does not contain url, pendingUrl, title, and favIconUrl if the "tabs" permission has not been requested. This only returns a Promise when the callback parameter is not specified, and with MV3+.


1 Answers

I work on a fairly complex extension that does a lot of tab queries, and adding an arbitrary timeout wasn't helping, especially when users were actually dragging tabs.

Re-running the function after a short delay when we encounter browser.runtime.lastError seems to work:

function doStuffWithTabs() {
  browser.tabs.query({ active: true, currentWindow: true }, (tab) => {
    if (browser.runtime.lastError) {
      console.log('fail');
      window.setTimeout(() => doStuffWithTabs(), 100);
    } else {
      console.log('win');
    }
  });
}
like image 147
Kent Brewster Avatar answered Nov 09 '22 08:11

Kent Brewster