我有一个Chrome扩展程序,通过下面的代码访问活动选项卡:
chrome.tabs.query({ active: true }, (result) => { ... })
这一直非常有效,直到最近更新,现在我无法查询该选项卡,并且控制台打印出以下错误:
Tabs cannot be queried right now (user may be dragging a tab).
我尝试了这个方法,但它没有起作用。有什么建议吗?
我有一个Chrome扩展程序,通过下面的代码访问活动选项卡:
chrome.tabs.query({ active: true }, (result) => { ... })
这一直非常有效,直到最近更新,现在我无法查询该选项卡,并且控制台打印出以下错误:
Tabs cannot be queried right now (user may be dragging a tab).
我尝试了这个方法,但它没有起作用。有什么建议吗?
browser.runtime.lastError
错误时,在短暂延迟后重新运行该函数似乎是有效的。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');
}
});
}
我相信你已经使用了manifest v2构建了你的扩展程序。 尝试通过chrome.runtime.lastError避免这个错误。如果你在代码中使用了chrome.tabs.onUpdated.addListener(...)或其他监听器方法,那么请按照以下方式更新它,以便不会抛出此错误。
chrome.tabs.onUpdated.addListener( (tabId, statusInfo, tabInfo) => {
//this will prevent it from throwing the error
if (chrome.runtime.lastError) {}
//rest of your code
})
chrome.tabs.query({active: true}, result => {
//this will prevent it from throwing the error
if (chrome.runtime.lastError) {}
//rest of your code
})
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, { command, data }, function (response) {
// This one prevents the Tabs undefined errors...
if (!chrome.runtime.lastError) {}
});
});
}
});