Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript not working in chrome-app

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>
like image 696
user1816847 Avatar asked Dec 22 '13 08:12

user1816847


1 Answers

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.

like image 175
geevee Avatar answered Sep 28 '22 17:09

geevee