Google chrome extension 选项卡信息在chrome.tabs.on激活的事件回调中不可用
当我试图从Google chrome extension 选项卡信息在chrome.tabs.on激活的事件回调中不可用,google-chrome-extension,microsoft-edge-extension,Google Chrome Extension,Microsoft Edge Extension,当我试图从chrome.tabs.on中获取选项卡信息时,当通过鼠标单击切换选项卡时,我会得到undefined结果和错误消息 未选中的runtime.lastError:现在无法编辑选项卡(用户可能会 正在拖动选项卡) 使用100ms延迟或使用CTRL+tab切换tab,或者如果在窗口未处于焦点时单击选项卡,则工作正常: background.js chrome.tabs.onActivated.addListener( info => { chrome.tabs.get(info.
chrome.tabs.on中获取选项卡信息时,当通过鼠标单击切换选项卡时,我会得到undefined
结果和错误消息
未选中的runtime.lastError:现在无法编辑选项卡(用户可能会
正在拖动选项卡)
使用100ms延迟或使用CTRL+tab切换tab,或者如果在窗口未处于焦点时单击选项卡,则工作正常:
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"
}
这是虫子还是什么
在Chrome 91.0.4472.77 64位和MS Edge 91.0.864.41 64位中进行测试是的,这是一个错误:⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
解决方法是捕获runtimeError
和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);
}