有没有办法在 chrome 扩展 (MV3) 中允许不安全的内联代码?

Is there a way to allow unsafe-inline code in a chrome extension (MV3)?

我目前正在制作一个 chrome 扩展(在清单版本 3 中),我正在尝试允许内联事件处理程序。在我的 popup.html 中,我想 运行 单击按钮时的功能,如下所示:

HTML:

<button onclick="foo(this)">bar</button>

(分开)JavaScript:

function foo(x){
    console.log(x);
}

当我 运行 扩展程序时,出现以下错误:

Refused to execute inline event handler because it violates the following Content Security Policy directive: "script-src 'self'". Either the 'unsafe-inline' keyword, a hash ('sha256-...'), or a nonce ('nonce-...') is required to enable inline execution.

我已尝试向我的 JavaScript 文件添加内容安全策略和随机数(以允许内联执行工作)但也没有奏效:

"content_security_policy": {
    "extension_pages": "script-src 'self' 'nonce-baz'"
},

添加内容安全策略导致以下错误:

'content_security_policy.extension_pages': Insecure CSP value "'nonce-2726c7f26c'" in directive 'script-src'. Could not load manifest.

所以我想知道如何允许执行内联代码。 (我看过这个post:Chrome Extension - Content Security Policy - executing inline code,然而,答案是使用Manifest Version 2,而我使用的是Manifest Version 3。)

我不太了解制作 chrome 扩展,但如果您使用 .addEventListener 可能会有所帮助。我听说不推荐使用 onclick,因此您可能还是想使用 and .addEventListener

在你的情况下,我认为它应该是这样的:

html:

<button id='putIdHere'>bar</button>

js:

document.getElementById('putIdHere').addEventListener('click', {
   console.log('stuff')
});

不确定这是否有帮助,但希望你能用它做点什么:)。