JavaScript无法在Chrome扩展程序HTML桌面通知中使用

Con*_*mus 2 html javascript desktop notifications google-chrome-extension

我正在开发一个带有HTML桌面通知的Chrome扩展程序,但是当它们出现时,看起来它们不会执行任何JavaScript.我从一个复杂的JS开始,然后我将测试减少到这个:

<!DOCTYPE html>
<head>
    <title>notification</title>
</head>
<body>
    <script>document.write("content")</script>
    <img src='notification.png'><p>Yep!</p><p id="test"></p>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

我可以看到任何硬编码,但没有DOM操作(或简单的document.write)工作.我试图将JS放在一个单独的文件中,但它没有用.我目前的Chrome版本是23.0.1271.64(Apple v.).

你能帮助我吗?

[更新]这是manifest.json:

{
  "name": "BB",
  "version": "1.0",
  "manifest_version": 2,
  "description": "",
  "icons": {"48": "BB.png"},
  "browser_action": {
    "default_title": "BB",
    "default_icon": "icon.png",
    "default_popup": "popup.html"
  },
  "background": {
    "scripts": ["bkg.js"]
  },
  "permissions": [
    "notifications"
  ],
  "web_accessible_resources": [
    "notification.html","popup.html","popup.js"
  ]
}
Run Code Online (Sandbox Code Playgroud)

Dan*_*Lee 5

由于内容安全策略(CSP),您必须使用外部脚本.

以下是我如何使用它:

background.js

var notification = webkitNotifications.createHTMLNotification('notification.html');
notification.show();
Run Code Online (Sandbox Code Playgroud)

notification.html

<!DOCTYPE html>
<head>
  <title>notification</title>
  <script src="notification.js"></script>
</head>
<body>
    <img src='notification.png'><p>Yep!</p><p id="test"></p>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

最后是notification.js.添加事件侦听器以等待加载页面非常重要:

document.addEventListener('DOMContentLoaded', function() {
  document.getElementById('test').innerHTML = 'test';
});
Run Code Online (Sandbox Code Playgroud)

p然后应该用测试内容填充标签的文本.

顺便说一句:我认为您不需要将notification.html添加到您的web_accessible_resources.