2013-04-11 35 views

回答

9

您可以掛鉤webkitNotifications.createNotification函數,以便每當創建通知時運行一些特定的代碼。

  1. 創建一個名爲文件notifhook.js

    (function() { 
        // save the original function 
        var origCreateNotif = webkitNotifications.createNotification; 
    
        // overwrite createNotification with a new function 
        webkitNotifications.createNotification = function(img, title, body) { 
    
         // call the original notification function 
         var result = origCreateNotif.apply(this, arguments); 
    
         // bind a listener for when the notification is displayed 
         result.addEventListener("display", function() { 
          // do something when the notification is displayed 
          // use img, title, and body to read the notification 
          // YOUR TRIGGERED CODE HERE! 
         }); 
    
         return result; 
        } 
    })(); 
    
  2. 接下來,在您在您的清單web_accessible_resources listnotifhook.js

  3. 最後,在content script,注入<script>元素到頁面notifhook.jssrc

    var s = document.createElement("script"); 
    s.src = chrome.extension.getURL("notifhook.js"); 
    document.documentElement.appendChild(s); 
    

你也許會只使用notifhook.js爲您的內容腳本,但韓元沒有用,因爲內容腳本和網頁有separate execution environments。覆蓋內容腳本的webkitNotifications.createNotification版本根本不會影響Google日曆頁面。因此,您需要通過<script>標記注入它,這會影響頁面和內容腳本。

+0

謝謝。所以,沒有API可以做到這一點。但這個黑客會做到這一點。 – 2013-04-11 21:17:50

0

如果您想要的是發送通知的Chrome擴展程序捕獲Google日曆,則不行。如果我沒有弄錯,Google日曆會發送郵件和短信通知。另外,我認爲沒有任何API函數可以請求查看是否有未決通知。文檔中唯一的事情就是將Google+事件通知發送到Google+,並且可能可以獲取API的訪問權限。