Chrome Extension - Content Security Policy - executing inline code

后端 未结 6 1660
梦谈多话
梦谈多话 2020-12-08 10:15

I am using an external JavaScript lib in my chrome extension. I has inline execution, so I get following kind of error

(The error I get on console)

6条回答
  •  温柔的废话
    2020-12-08 10:41

    I've got this problem after I added a simple checkbox on the login page to toggle password visibility and here is how I have fixed my problem, hope it helps;

    • I've stopped using my checkbox's onclick event, which was causing this problem in incognito mode of chrome and instead gave an id to my checkbox.

    Before

    After

    • I've created a Script.js file and added an event listener method into it to handle onclick event of my checkbox to do the job.

    Remember to reference your js file, if you haven't, yet. You can simply reference it like this.

    
    

    And here is the event listener that I've added into my Script.js.

    $(document).ready(function () {
        addEventListenerForCheckboxTogglePasswordVisibility()
    });
    
    function addEventListenerForCheckboxTogglePasswordVisibility() {
        var checkbox = document.getElementById("checkboxTogglePasswordVisibility");
        if (checkbox !== null) {
            checkbox.addEventListener('click', togglePasswordVisibility);
        }
    }
    
    function togglePasswordVisibility() {
        var passwordElement = document.getElementById("password");
        if (passwordElement.type === "password") {
            passwordElement.type = "text";
        } else {
            passwordElement.type = "password";
        }
    }
    

    Error before the fix;

提交回复
热议问题