最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Tabs cannot be queried right now (user may be dragging a tab) - Stack Overflow

programmeradmin2浏览0评论

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?

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?

Share Improve this question asked Jun 3, 2021 at 13:55 NicholasNicholas 7557 silver badges24 bronze badges 3
  • The solution you've linked is the only one and it works but you may need to increase the timeout. – woxxom Commented Jun 3, 2021 at 17:06
  • 1 I've experienced the same thing. Everything was working fine for months before and now suddenly, this strange, unhelpful error message. I'm assuming the api has changed – I0_ol Commented Jun 5, 2021 at 7:27
  • 1 Ok so yeah, Google just updated the api and released manifest version 3. This shouldn't have broken anything for current extensions but, apparently it did. – I0_ol Commented Jun 5, 2021 at 8:02
Add a ment  | 

4 Answers 4

Reset to default 2

I work on a fairly plex 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');
    }
  });
}

I believe you have built your extension with manifest v2. Try to avoid this error by chrome.runtime.lastError. If you have used chrome.tabs.onUpdated.addListener(...) or other listener method in your code then update it like below so that it wont throw this error.

chrome.tabs.onUpdated.addListener( (tabId, statusInfo, tabInfo) => {
   //this will prevent it from throwing the error
   if (chrome.runtime.lastError) {}
   //rest of your code 
})

Also anywhere in the rest of your code or inside chrome.tabs.onUpdated.addListener(...) listener if you are using chrome.tabs.query({ active: true }, (result) => { ... }) then also update it like below:

chrome.tabs.query({active: true}, result => {
   //this will prevent it from throwing the error
   if (chrome.runtime.lastError) {}
   //rest of your code 
})

I found that the real reason is: when you click the mouse to switch tabs, when chrome.tabs.query is executed, the left mouse button has not been raised yet, and there may be a very small displacement, which leads chrome to think that you are dragging tabs instead of clicking them. You can try to do this: raise the mouse, and then quickly click a tab to switch, at this time everything is OK. Using the method of setTimeout provided by netizens can really solve the problem, because after 200ms, your mouse has probably finished clicking, getting rid of the suspicion of dragging. Anyway, I think this update to Chrome is a failed attempt.

This code works for me:

chrome.tabs.query({ url: 'YOUR_URL_HERE' }, function (tabs) {
    // This one prevents the "Tabs cannot be queried right now (user may be dragging a tab)."
    if (!chrome.runtime.lastError) {}
    if (tabs) {
        tabs.forEach(tab => {
            chrome.tabs.sendMessage(tab.id, { mand, data }, function (response) {
                // This one prevents the Tabs undefined errors...
                if (!chrome.runtime.lastError) {}
            });
        });
    }
});
发布评论

评论列表(0)

  1. 暂无评论