admin管理员组文章数量:1390686
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
4 Answers
Reset to default 2I 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) {}
});
});
}
});
本文标签: javascriptTabs cannot be queried right now (user may be dragging a tab)Stack Overflow
版权声明:本文标题:javascript - Tabs cannot be queried right now (user may be dragging a tab) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744645111a2617354.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论