无法让 content_security_policy 在我的扩展程序中工作

Can't get content_security_policy to work in my extension

我有一个简单的扩展只是为了练习,但我无法使我的清单中的内容安全策略起作用。

将来我希望我的扩展程序将一些东西保存到本地存储,检查复选框是否被选中。如果是则执行脚本,如果不是则不执行。

那么你能帮我让我的内容安全策略生效吗?


错误信息:

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

manifest.json

    {
        "name": "no-name",
        "version": "0.0.01",
        "description": "non yet",
        "permissions": ["storage"],
        "content_scripts": [
            {
                "matches": [
                    "<all_urls>"
                ],
                "js": ["content-script.js"]
            }
        ],
        "browser_action": {
            "default_popup": "popup.html"
        },
        "content_security_policy": "???",
        "manifest_version": 2
    }

popup.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <script>
            window.addEventListener('DOMContentLoaded', (event) => {
                function click() {
                    let switchPopup = document.getElementById('ex_switchPopup');
            
                    if(switchPopup.checked == true) {
                        console.log('It\'s checked');
                    }
                }
            });
        </script>
    </head>
    <body>
        <label class="switch">
            <input type="checkbox" id="ex_switchPopup" checked onclick="click()">
            <span class="slider round"></span>
        </label>
    </body>
    </html>

您必须将 javascript 代码从 html 移动到 js 文件。 Chrome 策略不允许内联脚本。检查 documentation

创建popup.js文件

window.addEventListener('DOMContentLoaded', (event) => {
  document.getElementById('ex_switchPopup').addEventListener('click', YOUR_LISTENER);
})

在您的 html

中添加 link
<script src="popup.js"></script>