我是 Chrome 扩展程序开发的新手。我编写了一个扩展程序,每次创建新选项卡时都会显示通知。它可以工作,但是如果我打开一个选项卡并立即打开另一个选项卡,则通知将仅显示第一个选项卡。
在Chrome 扩展文档中,我读到了这个:
一旦事件页面空闲了一小段时间(几秒钟),就会调度 chrome.runtime.onSuspend 事件。
显然它解释了为什么第二个标签没有通知。是否可以立即卸载事件页面(即一旦显示通知),而无需等待几秒钟?
这是我的代码:
清单.json
{
"name": "test",
"version": "1.0",
"description": "notification test",
"manifest_version": 2,
"permissions": [ "tabs", "notifications" ],
"background": { "scripts": ["background.js"], "persistent": false }
}
背景.js
var notification = webkitNotifications.createNotification(
'48.png',
'hello',
'this is a test'
);
chrome.tabs.onCreated.addListener(function(tab) {
notification.show();
});