0

I have a simple extension just for practice, but I can't get my content security policy in my manifest to work.

In the future I want my extension to save something to the local storage, that checks if the checkbox is check. If it is then it executes the script or if not then don't.

So can you help me with getting my content security policy to work?


Error message:

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>

Cray
  • 2,347
  • 7
  • 17
  • 27
orc13a
  • 85
  • 7

1 Answers1

0

You must move your javascript code from your html to js file. Inline script is not allowed by Chrome Policy. Check the documentation

Create popup.js file

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

Add link in your html

<script src="popup.js"></script>
Armen Stepanyan
  • 957
  • 8
  • 23