0

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;
                  });
4

1 に答える 1

3

コードを単純化して onRequest リスナーを 1 つだけ使用すると、重複する通知が表示されなくなります。

background_page

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);
});

content_script

chrome.extension.sendRequest({
  message: "There is a video" + videoURL},  // Change the message here as needed.
  function(response) {
  return response;
});
于 2012-04-28T14:45:11.923 に答える