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

时间:2012-11-08 08:35:08

标签: javascript html google-chrome-extension notifications desktop

我正在开发一个带有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>

我可以看到任何硬编码但没有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"
  ]
}

1 个答案:

答案 0 :(得分:5)

由于Content Security Policy (CSP)

,您必须使用外部脚本

以下是我如何运作:

<强> background.js

var notification = webkitNotifications.createHTMLNotification('notification.html');
notification.show();

<强> 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>

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

document.addEventListener('DOMContentLoaded', function() {
  document.getElementById('test').innerHTML = 'test';
});

然后应使用测试内容填充p标记的文本。

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

相关问题