2

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

1 回答 1

5

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

这是我如何让它工作的:

背景.js

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

通知.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.

于 2012-11-08T09:04:02.507 回答