本文介绍了Chrome扩展程序错误:“运行browserAction.setIcon时未检查的runtime.lastError:没有标签,ID为”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我为我的Google Chrome浏览器扩展程序编写代码,其中我从后台脚本中设置应用程序的图标,如下所示:
try
{
objIcon = {
19:images / icon19.png,
38:images / icon38.png
};
chrome.browserAction.setIcon({
path:objIcon,
tabId:nTabID
});
catch(e)
{
}
不过,有时我会在控制台日志中收到以下消息:
很难调试此错误,因为它似乎只在关闭或重新加载Chrome标签时才会出现,它没有行号或任何信息供我跟踪,再加上它不容易通过调试器(即我不能在发生错误时设置断点,但是如果我在 chrome.browserAction.setIcon()$>上盲目设置断点
所以我很好奇,如果任何人都可以建议如何解决这个错误? line,我没有看到日志中的消息。 / p>
编辑:只是发布更新。我仍然无法解决这个问题。下面@abraham提出的建议提供了一种有效的方法,但它是不是安全的。例如,在选项卡关闭的情况下,如果选项卡尚未关闭,我可以调用他建议的 chrome.browserAction.setIcon(),但在它的回调函数标签可能最终会关闭,因此任何连续调用某个需要相同标签ID的API(比如 setBadgeBackgroundColor())仍可能会给我相同的没有包含id 异常的选项卡。换句话说,对于那些了解 native programming 的人来说,这是一个典型的竞争条件情况。我不确定这是否是Chrome中的一个错误,因为显然JS不提供任何线程同步方法......
我几次目睹这种行为做我的测试。它并不经常发生,因为我们正在谈论非常精确的时间情况,但它确实发生。所以,如果有人找到解决方案,请将其发布到下面。 解决方案
包含回调并检查。
objIcon = {
19:images / icon19.png,
38:images / icon38.png
};
函数callback(){
if(chrome.runtime.lastError){
console.log(chrome.runtime.lastError.message);
} else {
//标签存在
}
}
chrome.browserAction.setIcon({
path:objIcon,
tabId:nTabID
},回调);
I'm coding my Google Chrome Extension where I set the app's icon from the background script as such:
try
{
objIcon = {
"19": "images/icon19.png",
"38": "images/icon38.png"
};
chrome.browserAction.setIcon({
path: objIcon,
tabId: nTabID
});
}
catch(e)
{
}
Note that I wrapped the call in try/catch block.
Still, sometimes I'm getting the following message in the console log:
It's hard to debug this error because it seems to come up only when I close or reload the Chrome tab, it doesn't have a line number or any info for me to track, plus it's not easy to run through a debugger (i.e. I cannot set a break-point on the moment when error occurs, but if I blindly set a break-point on the chrome.browserAction.setIcon() line, I don't see the message in the log anymore.)
So I'm curious if anyone could suggest how to remedy this error?
EDIT: Just to post an update. I am still unable to resolve this issue. The suggestion proposed by @abraham below offers a somewhat working approach, but it is not fail safe. For instance, in a situation when the tab is closing I may call his suggested chrome.browserAction.setIcon() that may succeed if the tab is not yet closed, but while within its callback function the tab may eventually close and thus any consecutive calls to some other API that requires that same tab ID, say setBadgeBackgroundColor() may still give me that same No tab with id exception. In other words, for those who know native programming, this is a classic race condition situation. And I'm not sure if it's a bug in Chrome, because obviously JS does not offer any thread synchronization methods...
I've witnessed this behavior several times while doing my tests. It doesn't happen often because we're talking about very precise timing situation, but it does happen. So if anyone finds a solution, please post it below.
解决方案
Include a callback and check chrome.runtime.lastError.
objIcon = {
"19": "images/icon19.png",
"38": "images/icon38.png"
};
function callback() {
if (chrome.runtime.lastError) {
console.log(chrome.runtime.lastError.message);
} else {
// Tab exists
}
}
chrome.browserAction.setIcon({
path: objIcon,
tabId: nTabID
}, callback);
这篇关于Chrome扩展程序错误:“运行browserAction.setIcon时未检查的runtime.lastError:没有标签,ID为”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!