Google chrome extension Chrome扩展后台脚本控制台.log()错误

Google chrome extension Chrome扩展后台脚本控制台.log()错误,google-chrome-extension,Google Chrome Extension,在我的舱单中,我有: "background": { "page": "background.html", // "scripts": ["background.js"], "persistent": true }, 请注意,后台脚本是从“background.html”加载的,因为我需要使用该html页面的一些特殊原因。清单的这一部分无法更改 使用此设置,后台脚本工作正常,但当我包含console.log(“Succeed.”)行时,脚本加载时出错,如下所示: 奇怪

在我的舱单中,我有:

"background": {
    "page": "background.html",
    // "scripts": ["background.js"],
    "persistent": true
},
请注意,后台脚本是从“background.html”加载的,因为我需要使用该html页面的一些特殊原因。清单的这一部分无法更改

使用此设置,后台脚本工作正常,但当我包含
console.log(“Succeed.”)
行时,脚本加载时出错,如下所示:

奇怪的是,日志记录实际上起了作用,我可以在
background.html
的控制台中看到它

但我无法消除错误,尽管添加了:

var console2=chrome.extension.getBackgroundPage().console;


这是一个相对较小的错误,因为除了出现错误消息外,其他一切似乎都正常工作。

为什么要在后台页面中使用
chrome.extension.getBackgroundPage().console
,而不是直接调用
console.log
?直接调用console.log会出现相同的错误,我只是尝试了一些变体…在调用console.log之前,您的后台脚本中可能还有其他东西失败。您可以通过检查lastError来清除内部状态:
console.log(chrome.runtime.lastError)
,但是如果我在background.js中消除对
console.log
的所有调用,脚本将加载并运行,不会出现任何错误。在background.js中甚至有对
console.log()
的一些调用,但它们在一些事件处理程序函数中,他们很好。但是background.js的“main”部分中的
console.log()
会导致错误。