krish_cloudnow
krish_cloudnow

Reputation: 180

Refused to execute inline event handler because it violates the following Content Security Policy Error in Chrome App

I am building a chrome app. Below is my code

HTML

<input type="text" id="task" x-webkit-speech class="form-control" placeholder="enter your task" >

<button onclick = "showCD(document.getElementById('task').value)" class="btn btn-success">Add Task</button>

JS

function showCD(str) {
if (str=="") {
document.getElementById("txtHint").innerHTML="";
return;
} 

if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else {  // code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
  document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","http://localhost:8080/getcd.php?q="+str,true);
xmlhttp.send();
}

This works completely fine on the browser however when I try it in a chrome web app it throws the error mentioned in the title. I am confused. I also checked the following link but that did not help me a lot.

https://developer.chrome.com/extensions/tut_migration_to_manifest_v2#inline_scripts

Upvotes: 1

Views: 3100

Answers (1)

Marc Rochkind
Marc Rochkind

Reputation: 3740

It is very well documented that this is not allowed for Chrome Apps:

https://developer.chrome.com/apps/contentSecurityPolicy

All JavaScript must be in JavaScript files referenced either directly from the manifest.json file (a so-called background script) or from an HTML file that initializes a Chrome App window.

(Set a handler for the click event from within JavaScript.)

It's very important to read the Chrome App documentation, where the unique properties of Chrome Apps are described That what you want to do works in the browser is completely irrelevant.

Upvotes: 2

Related Questions