javascript not working in chrome-app

人盡茶涼 提交于 2019-12-10 13:23:58

问题


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>

回答1:


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.



来源:https://stackoverflow.com/questions/20727493/javascript-not-working-in-chrome-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!