2012-04-28 98 views
0

我仍在學習如何創建Chrome擴展程序,但我的問題是使用桌面通知。我能夠觸發通知,但是當發生這種情況時,我會觸發內容腳本1的桌面通知。桌面通知也會觸發內容腳本2.我如何使它不會同時觸發,以及只有當他們被稱爲?同時發送一個Chrome擴展程序桌面通知

背景頁面

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

你Ç將代碼簡化爲僅使用一個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

謝謝你正是我一直在試圖完成 – 2012-04-28 15:09:44

+0

onRequest和sendRequest將已過時,需要用的onMessage和的sendMessage被替換 – DivZero 2014-05-10 14:13:16

相關問題