1

When I'm trying get tab information from within chrome.tabs.onActivated event callback when a tab switched via mouse click I get undefined result and error message

Unchecked runtime.lastError: Tabs cannot be edited right now (user may be dragging a tab).

With 100ms delay or switch tab with CTRL+TAB or if a tab clicked while window was not in focus it works fine:

background.js

chrome.tabs.onActivated.addListener( info =>
{
  chrome.tabs.get(info.tabId, tab => console.log("get", tab));
  chrome.tabs.query({active: true, windowId: info.windowId}, tab => console.log("query", tab));
  chrome.tabs.getSelected(tab => console.log("getSelected", tab));
  setTimeout(()=>
  {
    chrome.tabs.get(info.tabId, tab => console.log("setTimeout get", tab));
    chrome.tabs.query({active: true, windowId: info.windowId}, tab => console.log("setTimeout query", tab));
    chrome.tabs.getSelected(tab => console.log("setTimeout getSelected", tab));
  }, 100);
});

manifest.json

{
   "background": {
      "persistent": true,
      "scripts": [ "background.js" ]
   },
   "description": "tab info within chrome.tabs.onActivated event",
   "manifest_version": 2,
   "name": "test",
   "permissions": [ "tabs" ],
   "version": "0.0.1"
}

Is this a bug or something?

Testing in Chrome 91.0.4472.77 64bit and MS Edge 91.0.864.41 64bit

vanowm
  • 9,466
  • 2
  • 21
  • 37

1 Answers1

1

Yes, it's a bug:⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀

https://bugs.chromium.org/p/chromium/issues/detail?id=1213925

The work around is catch runtimeError together with setTimeout

function tabsGet(id, callback)
{
  const cb = tab => (chrome.runtime.lastError) ? setTimeout(e => chrome.tabs.get(id, cb)) : callback(tab);
  chrome.tabs.get(id, cb);
}
vanowm
  • 9,466
  • 2
  • 21
  • 37