控制台不记录内容脚本中的 js 错误

Console doesn't log js errors from content script

我知道要调试内容脚本,请使用普通的 Web 开发人员工具 (https://developer.mozilla.org/en/docs/Mozilla/Add-ons/WebExtensions/Debugging#Debugging_content_scripts),这非常有效。 debugger 关键字按预期工作。

但在这种情况下,事情就坏了:

addon.id = "123-568-485"; // I never define `addon` before this line, so this cause: ReferenceError: "addon is not defined". We aren't aware of this mistake.

// Some more code
// Some more code
// Some more code
// Some more code

debugger; // Here we want to stop execution and inspect, some other stuff. Remember that we aren't aware of earlier mistake.

正如我们所期望的那样,在控制台中会出现关于 Reference error 的错误,但实际上并没有。控制台静默,我们不知道为什么我们的 debugger 关键字不起作用。

这种无提示错误,当我拼错变量名时发生在我身上。结果想不通哪里出了问题。

内容脚本在网页中执行,因此如您所知,要查看它的输出,您应该在该特定网页中打开控制台菜单(ctrl+shift+e 然后转到控制台)。
但是如果内容脚本有问题并导致它抛出异常,错误日志将显示在扩展程序的调试区域中:about:debugging

我认为原因是内容脚本被视为网页的额外框架,并且在那里显示了它们的错误。

由于 Firefox bug 1410932,选项卡的 Web 控制台中未报告内容脚本中的错误,该问题尚未修复(截至 2020-07-28 发布的 Firefox 79)。

我在 another answer 中列出了可能的解决方法:

  • use try..catch with logging,
  • check the Browser Console (which does show errors from the content script)
  • use the Debugger's "pause on exceptions" option.