我有一个 chrome-extension,我想使用新的丰富通知。我正在尝试实现以下内容:
var opt = {
type: "basic",
title: "New message from " + sBuffer[0] + ":",
message: sBuffer[2],
iconUrl: getUserIcon(sBuffer[0])
};
chrome.notifications.create("",opt,function(){});
但无论我做什么,我都会收到以下错误:
Uncaught TypeError: Cannot call method 'create' of undefined
我进入了chrome://flags
并将其中带有“通知”的所有内容设置为启用...我正在运行 chrome 31.0.1650.57 m。
这很好用:
var notification = webkitNotifications.createNotification(
getUserIcon(sBuffer[0]),
"New message from " + sBuffer[0] + ":",
sBuffer[2]
);
notification.show();
它不是很漂亮,但它可以工作(图标很小,即使它是高分辨率图像......有什么办法可以让图像图标变大吗?强>)
顺便说一句,我的 list 中有通知权限。
谢谢,戴夫
编辑:包含 list
{
"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。但它不适用于内容脚本,因此您需要在后台页面中创建通知。
如果需要传递特定的数据显示在通知中,可以使用 Message Passing 在内容脚本和后台页面之间进行通信。
例如:
/* 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`)
});
关于javascript - Chrome 扩展丰富的通知不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20317388/