javascript无法在chrome-app中运行

问题描述 投票:5回答:1

我正在尝试开始编写Chrome应用程序。 出于某种原因,当我查看作为Chrome应用程序时,javascript不起作用,但它作为网页工作正常。 这是我的问题最简单的例子。

使用chrome打开index.html按预期工作 - 按下按钮时,“hello world”字符串变为“CLICK”。 作为Chrome应用程序运行按下按钮时没有任何反应。

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>
google-chrome-app
1个回答
7
投票

好吧, Chrome Extensions Content-Security-Policy不允许使用内联脚本

内联JavaScript将不会被执行。 此限制禁止内联块和内联事件处理程序(例如)。

尝试在页面中包含一个包含javascript的脚本文件( <script src="script.js" type="text/javascript"></script"> ),而不是在页面中使用您的javascript代码。

希望有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.