Javascript Chrome扩展丰富通知不工作
我有一个chrome扩展名,我想使用新的富通知。我正在尝试实现以下内容:Javascript Chrome扩展丰富通知不工作,javascript,google-chrome,google-chrome-extension,Javascript,Google Chrome,Google Chrome Extension,我有一个chrome扩展名,我想使用新的富通知。我正在尝试实现以下内容: var opt = { type: "basic", title: "New message from " + sBuffer[0] + ":", message: sBuffer[2], iconUrl: getUserIcon(sBuffer[0]) }; chrome.notificatio
var opt = {
type: "basic",
title: "New message from " + sBuffer[0] + ":",
message: sBuffer[2],
iconUrl: getUserIcon(sBuffer[0])
};
chrome.notifications.create("",opt,function(){});
但无论我做什么,我都会得到以下错误:
未捕获类型错误:无法调用未定义的方法“create”
我进入了chrome://flags
并将其中包含“通知”的所有内容设置为已启用。。。我正在运行chrome 31.0.1650.57 m
这很好:
var notification = webkitNotifications.createNotification(
getUserIcon(sBuffer[0]),
"New message from " + sBuffer[0] + ":",
sBuffer[2]
);
notification.show();
它不漂亮,但可以工作(即使是高分辨率图像,图标也很小…有没有办法让图像图标变大?)
顺便说一句,我的清单中有通知权限
谢谢,戴夫
编辑:包含的清单
{
"manifest_version": 2,
"name": "Notifier",
"description": "This extension will listen the the JS on the page and popup notifications",
"version": "0.1",
"permissions": [
"background","notifications"
],
"content_scripts": [
{
"matches": ["http://MY_WEB_SITE"],
"js": ["Notifier.js"]
}
]
}
似乎您正试图从内容脚本访问chrome.notifications
API。但它不适用于内容脚本,因此您需要在后台页面中创建通知
如果需要传递要在通知中显示的特定数据,可以使用在内容脚本和背景页面之间进行通信。
例如:
确保您在清单中设置了权限(您应该包括这些权限)。另外,id
应该是唯一的。你说的“背景页”是什么意思?它是一个必须通过
包含的.js文件吗。不确定您正在谈论的.js
文件是什么,以及
是如何发挥作用的。
/* In content script */
chrome.runtime.sendMessage({
from: sBuffer[0],
body: sBuffer[2]
});
/* In background page */
chrome.runtime.onMessage.addListener(function(msg, sender) {
/* Verify the message's format */
(msg.from !== undefined) || return;
(msg.body !== undefined) || return;
/* Create and show the notification */
// ...your notification creation code goes here
// (replace `sBuffer[0]`/`sBuffer[2]` with `msg.from`/`msg.body`)
});