I'm trying to get started writing chrome apps. For some reason when I view as a chrome app the javascript does not work but it works fine as a webpage. Here's the simplest instance of my issue.
opening index.html with chrome works as expected--the "hello world" string becomes "CLICK" when the button is pressed. Running as a chrome app nothing happens when the button is pressed.
manifest.json:
{
"manifest_version": 2,
"name": "My first app",
"version": "0.0.1",
"app": {
"background": {
"scripts": ["main.js"]
}
}
}
main.js
chrome.app.runtime.onLaunched.addListener(function() {
chrome.app.window.create('index.html', {
bounds: {
width: 800,
height: 609
}
});
});
index.html:
<!DOCTYPE html>
<html>
<head>
<title>test</title>
</head>
<body>
<button type="button" onclick="myFunction()">click me</button>
<script>
function myFunction(){
document.getElementById("testdiv").innerHTML = "CLICK"
}
</script>
<div id="testdiv">hello world</div>
</body>
</html>
well, Chrome Extensions Content-Security-Policy doesn't allow usage of inline scripts.
Inline JavaScript will not be executed. This restriction bans both inline blocks and inline event handlers (e.g. ).
try to include a script file containing your javascript in your page (<script src="script.js" type="text/javascript"></script">
) instead of using your javascript code within the page.
hope that helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With