Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refused to execute inline event handler because of Content-Security-Policy

What do I need to change here so that the JavaScript code works with the manifest version 2 and the security policy?

Screenshot:
Screenshot

Code:

function init()
{
    chrome.tabs.getSelected(null, function(tab)
    {
        url = tab.url;
        if(url.indexOf('chrome://') == -1 && url.indexOf('about:blank') == -1){
            document.main.q.value = url;
        }
    });
}
like image 511
fnkr Avatar asked Feb 20 '23 17:02

fnkr


2 Answers

I got it working after I put the JavaScript code from the HTML file into an own file and added the following to the manifest file:

"content_scripts": [ {"js": [ "popup.html" ], "matches": [ "http://*/" ]} ],
"permissions": [ "tabs" ]
like image 61
fnkr Avatar answered May 01 '23 11:05

fnkr


The inline event handler definition must be rewritten in terms of addEventListener and extracted into popup.js, so you should remove the onfocus="this.select()" in your popup.html, and in the popup.js, add the following lines:

document.addEventListener('DOMContentLoaded', function () {
  document.getElementById('address_box').addEventListener('onfocus', this.select);
});
like image 44
chaohuang Avatar answered May 01 '23 12:05

chaohuang