javascript Chrome 扩展错误:“运行 browserAction.setIcon 时未选中 runtime.lastError:没有带 ID 的标签”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25735050/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 04:56:06  来源:igfitidea点击:

Chrome Extension error: "Unchecked runtime.lastError while running browserAction.setIcon: No tab with id"

javascriptgoogle-chromegoogle-chrome-extensionrace-condition

提问by c00000fd

I'm coding my Google Chrome Extension where I set the app's icon from the background scriptas such:

我正在编写我的 Google Chrome 扩展程序,我在后台脚本中设置了应用程序的图标,如下所示:

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.

请注意,我将调用包装在 try/catch 块中。

Still, sometimes I'm getting the following message in the console log:

尽管如此,有时我会在控制台日志中收到以下消息:

Unchecked runtime.lastError while running browserAction.setIcon: No tab with id: 11618.

运行 browserAction.setIcon 时未选中的 runtime.lastError:没有标签,ID:11618。

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.)

很难调试这个错误,因为它似乎只有在我关闭或重新加载 Chrome 选项卡时才会出现,它没有行号或任何信息供我跟踪,而且通过调试器运行并不容易(即我不能在错误发生的那一刻设置断点,但是如果我盲目地chrome.browserAction.setIcon()在行上设置断点,我就看不到日志中的消息了。)

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 workingapproach, but it is notfail 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 idexception. In other words, for those who know native programming, this is a classic race conditionsituation. And I'm not sure if it's a bug in Chrome, because obviously JS does not offer any thread synchronization methods...

编辑:只是为了发布更新。我仍然无法解决这个问题。下面@abraham 提出的建议提供了一种有点可行的方法,但它不是故障安全的。例如,在选项卡关闭的情况下,chrome.browserAction.setIcon()如果选项卡尚未关闭,我可能会调用他的建议,如果选项卡尚未关闭,该建议可能会成功,但在其回调函数中,选项卡可能最终关闭,因此对其他一些 API 的任何连续调用都需要相同的标签 ID,说setBadgeBackgroundColor()可能仍然给我同样的No tab with id例外。换句话说,对于那些了解原生编程的人来说,这是一个经典的竞争条件情况。而且我不确定这是否是 Chrome 中的错误,因为显然 JS 不提供任何线程同步方法......

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.

我在做测试时多次目睹了这种行为。它并不经常发生,因为我们谈论的是非常精确的计时情况,但它确实发生了。因此,如果有人找到解决方案,请在下面发布。

回答by abraham

Include a callback and check chrome.runtime.lastError.

包括回调并检查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);