Google chrome extension 一次发送一个Chrome扩展桌面通知

Google chrome extension 一次发送一个Chrome扩展桌面通知,google-chrome-extension,Google Chrome Extension,我仍在学习如何创建Chrome扩展,但我的问题是桌面通知。我能够触发通知,但当这种情况发生时,例如,我会触发content script 1的桌面通知。桌面通知也会触发内容脚本2。我如何使它们不会同时触发,并且只在调用它们时触发 背景页面 chrome.extension.onRequest.addListener(function(request, sender, sendResponse) { // Create a simple text notification var

我仍在学习如何创建Chrome扩展,但我的问题是桌面通知。我能够触发通知,但当这种情况发生时,例如,我会触发content script 1的桌面通知。桌面通知也会触发内容脚本2。我如何使它们不会同时触发,并且只在调用它们时触发

背景页面

chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
    // Create a simple text notification
    var notifyWinner = webkitNotifications.createNotification('48.png', 'Notification', request.winnerMessage);
    notifyWinner.show();
    setTimeout(function(){ notifyWinner.cancel(); },10000);
});

chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
    // Create a simple text notification
    var notifyVideo = webkitNotifications.createNotification('48.png', 'Notification', request.videoMessage);
    notifyVideo.show();
    setTimeout(function(){ notifyVideo.cancel(); },10000);
});
内容脚本1

chrome.extension.sendRequest({winnerMessage: "You won!!!"}, function(response) {
                return response;
            });
内容脚本2

chrome.extension.sendRequest({videoMessage: "There is a video" + videoURL}, function(response) {
                      return response;
                  });

您可以将代码简化为仅使用一个onRequest侦听器,然后它将停止显示重复的通知

背景页

chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
    // Create a simple text notification
    var notify = webkitNotifications.createNotification('48.png', 'Notification', request.message);
    notify.show();
    setTimeout(function(){ notify.cancel(); },10000);
});
内容脚本

chrome.extension.sendRequest({
  message: "There is a video" + videoURL},  // Change the message here as needed.
  function(response) {
  return response;
});

您可以将代码简化为仅使用一个onRequest侦听器,然后它将停止显示重复的通知

背景页

chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
    // Create a simple text notification
    var notify = webkitNotifications.createNotification('48.png', 'Notification', request.message);
    notify.show();
    setTimeout(function(){ notify.cancel(); },10000);
});
内容脚本

chrome.extension.sendRequest({
  message: "There is a video" + videoURL},  // Change the message here as needed.
  function(response) {
  return response;
});

onRequest和sendRequest已弃用,需要替换为onMessage和sendMessageonRequest和sendRequest已弃用,需要替换为onMessage和sendMessage