【发布时间】:2019-04-09 09:31:37
【问题描述】:
我想我之前在开发 Chrome 扩展程序时处理过这个问题,所以现在在日常维护期间似乎又出现了同样的问题。
谁能告诉我为什么这段代码:
try
{
chrome.tabs.get(nTabID, function(tab) //this is line 484 where the error happens
{
var tabUrl = '';
try
{
tabUrl = tab.url;
}
catch(e)
{
//Failed to get tab URL -- mute it
}
if(tabUrl)
{
//Process it
}
});
}
catch(e)
{
//Failed to get tab for 'nTabID' -- mute it
}
无法在控制台中防止此错误:
运行 tabs.get 时未检查 runtime.lastError:没有 ID 的选项卡:N
【问题讨论】:
-
该错误消息具有误导性,因为它是由 C++ 开发人员编写的 :-)。您只需要在回调中访问
chrome.runtime.lastError,请参阅Unchecked runtime.lastError when using Chrome API -
@wOxxOm:谢谢。我会试试看。但这是处理异常的一种非常奇怪的方式。我只是好奇,C++ 是如何出现在这里的?
-
您不应将
try...catch与异步 Chrome API 方法一起使用。而不是这个,在方法的回调中检查chrome.runtime.lastError。 -
C++ 程序员实现了扩展 API,他们显然认为错误消息已经足够不言自明,因为它可能是为了解内部情况的人准备的。
标签: javascript google-chrome google-chrome-extension