2012-04-28 5 views
0

Chrome拡張機能の作成方法はまだ習っていますが、デスクトップ通知で問題が発生しています。私は通知をトリガすることができますが、例えばコンテンツスクリプト1のデスクトップ通知をトリガするときに発生します。デスクトップ通知はコンテンツスクリプト2のトリガも行います。同時にそれらがトリガしないようにする方法彼らが呼ばれた時だけ?一度にChrome拡張機能のデスクトップ通知を1つ送信する

背景ページ

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

答えて

3

ますC 1つのonRequestリスナーのみを使用するようにコードを単純化し、重複した通知の表示を停止します。

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

は非推奨とのonMessageとのsendMessageに交換する必要がされている私は –

+0

onRequestとのsendRequestを達成しようとしたまさにあなたに感謝します – DivZero

関連する問題