Google chrome extension Chrome扩展-立即卸载事件页面

Google chrome extension Chrome扩展-立即卸载事件页面,google-chrome-extension,Google Chrome Extension,我是Chrome扩展开发的新手。我编写了一个扩展,每次创建新选项卡时都会显示通知。但是,如果我打开一个选项卡并立即打开另一个选项卡,通知将仅显示第一个选项卡 我在网上读到: 一旦事件页面空闲一小段时间(几秒钟),chrome.runtime.onSuspend事件将被调度 显然,这解释了为什么第二个选项卡没有通知。是否可以立即卸载事件页面(即,一旦显示通知),而无需等待几秒钟 这是我的代码: manifest.json { "name": "test", "version": "

我是Chrome扩展开发的新手。我编写了一个扩展,每次创建新选项卡时都会显示通知。但是,如果我打开一个选项卡并立即打开另一个选项卡,通知将仅显示第一个选项卡

我在网上读到:

一旦事件页面空闲一小段时间(几秒钟),chrome.runtime.onSuspend事件将被调度

显然,这解释了为什么第二个选项卡没有通知。是否可以立即卸载事件页面(即,一旦显示通知),而无需等待几秒钟

这是我的代码:

manifest.json

{
    "name": "test",
    "version": "1.0",
    "description": "notification test",
    "manifest_version": 2,
    "permissions": [ "tabs", "notifications" ],
    "background": { "scripts": ["background.js"], "persistent": false }
}
background.js

var notification = webkitNotifications.createNotification(
    '48.png',
    'hello',
    'this is a test'
);

chrome.tabs.onCreated.addListener(function(tab) {
    notification.show();
});
代码a)

manifest.json

{
    "name": "test",
    "version": "1.0",
    "description": "notification test",
    "manifest_version": 2,
    "permissions": [ "tabs", "notifications" ],
    "background": { "scripts": ["background.js"], "persistent": false }
}
background.js

var notification = webkitNotifications.createNotification(
    '48.png',
    'hello',
    'this is a test'
);

chrome.tabs.onCreated.addListener(function(tab) {
    notification.show();
});
在该解决方案中,如果由于以下原因第一次/紧接着创建另一个选项卡,您将看到两个通知。但在这里,你会被吸引到事件页面而不是背景页面

代码b)

通过将事件页面转换为背景页面,您可以运行所需的功能;但是,如果您对事件页面有特殊要求,请不要使用它

manifest.json

{
    "name": "test",
    "version": "1.0",
    "description": "notification test",
    "manifest_version": 2,
    "permissions": [ "tabs", "notifications" ],
    "background": { "scripts": ["background.js"], "persistent": false }
}
background.js

var notification = webkitNotifications.createNotification(
    '48.png',
    'hello',
    'this is a test'
);

chrome.tabs.onCreated.addListener(function(tab) {
    notification.show();
});