【问题标题】:Send one Chrome Extension desktop notification at a time一次发送一个 Chrome 扩展程序桌面通知
【发布时间】:2012-04-28 13:40:12
【问题描述】:

我仍在学习如何创建 Chrome 扩展程序,但我的问题在于桌面通知。我能够触发通知,但是当这种情况发生时,例如,我会触发内容脚本 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;
                  });

【问题讨论】:

    标签: google-chrome-extension


    【解决方案1】:

    您可以将代码简化为仅使用一个 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 和 sendMessage
    猜你喜欢
    • 2012-02-05
    • 2011-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-23
    • 2012-08-16
    • 2016-06-09
    • 2022-07-16
    相关资源
    最近更新 更多