OLD | NEW |
(Empty) | |
| 1 <html> |
| 2 <head> |
| 3 <script> |
| 4 var gmail = "http://mail.google.com/"; |
| 5 var gmailAtomRef = "http://mail.google.com/mail/feed/atom"; |
| 6 var pollInterval = 1000 * 10; // 10 seconds |
| 7 var requestTimeout = 1000 * 2; // 5 seconds |
| 8 var unreadCount = 0; |
| 9 var defaultIconId = 1; |
| 10 var whiteoutIconId = 2; |
| 11 var numberOfBlinks = 0; |
| 12 |
| 13 function init() { |
| 14 window.setTimeout(startRequest, 0); |
| 15 } |
| 16 |
| 17 function scheduleRequest() { |
| 18 window.setTimeout(startRequest, pollInterval); |
| 19 } |
| 20 |
| 21 // ajax stuff |
| 22 function startRequest() { |
| 23 getInboxCount( |
| 24 function(count) { |
| 25 updateUnreadCount(count); |
| 26 scheduleRequest(); |
| 27 }, |
| 28 function() { |
| 29 scheduleRequest(); |
| 30 } |
| 31 ); |
| 32 } |
| 33 |
| 34 function getInboxCount(onSuccess, onError) { |
| 35 var xhr = new XMLHttpRequest(); |
| 36 var abortTimerId = window.setTimeout(function() { |
| 37 xhr.abort(); |
| 38 onError(); |
| 39 }, requestTimeout); |
| 40 |
| 41 function handleSuccess(count) { |
| 42 window.clearTimeout(abortTimerId); |
| 43 onSuccess(count); |
| 44 } |
| 45 |
| 46 function handleError() { |
| 47 window.clearTimeout(abortTimerId); |
| 48 onError(); |
| 49 } |
| 50 |
| 51 try { |
| 52 console.log("request.."); |
| 53 xhr.onreadystatechange = function(){ |
| 54 console.log("readystate: " + xhr.readyState); |
| 55 if (xhr.readyState == 4) { |
| 56 console.log("responseText: " + xhr.responseText.substring(0, 200) + |
| 57 "..."); |
| 58 if (xhr.responseXML) { |
| 59 var xmlDoc = xhr.responseXML; |
| 60 var fullCountSet = xmlDoc.evaluate("/gmail:feed/gmail:fullcount", |
| 61 xmlDoc, gmailNSResolver, XPathResult.ANY_TYPE, null); |
| 62 var fullCountNode = fullCountSet.iterateNext(); |
| 63 if (fullCountNode) { |
| 64 handleSuccess(fullCountNode.textContent); |
| 65 } else { |
| 66 console.log("fullcount not found!"); |
| 67 console.error("Error: feed retrieved, but no <fullcount> node " + |
| 68 "found"); |
| 69 handleError(); |
| 70 } |
| 71 } else { |
| 72 console.log("No responseXML!"); |
| 73 } |
| 74 } |
| 75 } |
| 76 |
| 77 xhr.onerror = function(error) { |
| 78 console.log("error"); |
| 79 console.log(error); |
| 80 handleError(); |
| 81 } |
| 82 |
| 83 xhr.open("GET", gmailAtomRef, true); |
| 84 xhr.send(null); |
| 85 } catch(e) { |
| 86 console.log("ex: " + e); |
| 87 console.error("exception: " + e); |
| 88 handleError(); |
| 89 } |
| 90 } |
| 91 |
| 92 function gmailNSResolver(prefix) { |
| 93 if(prefix == 'gmail') { |
| 94 return 'http://purl.org/atom/ns#'; |
| 95 } |
| 96 } |
| 97 |
| 98 function updateUnreadCount(count) { |
| 99 if (unreadCount != count) { |
| 100 unreadCount = count; |
| 101 startBlink(); |
| 102 } |
| 103 } |
| 104 |
| 105 function startBlink() { |
| 106 chrome.browserAction.setIcon(defaultIconId); |
| 107 if (numberOfBlinks < 2) { |
| 108 setTimeout(doWhiteout, 200); |
| 109 numberOfBlinks = numberOfBlinks + 1; |
| 110 } else { |
| 111 numberOfBlinks = 0; |
| 112 chrome.browserAction.setBadgeText(unreadCount); |
| 113 chrome.browserAction.setName(unreadCount + " unread emails"); |
| 114 } |
| 115 } |
| 116 |
| 117 function doWhiteout() { |
| 118 chrome.browserAction.setIcon(whiteoutIconId); |
| 119 setTimeout(startBlink, 200); |
| 120 } |
| 121 |
| 122 function goToInbox() { |
| 123 chrome.tabs.create({url: gmail}); |
| 124 } |
| 125 |
| 126 // Called when the user clicks on the browser action. |
| 127 chrome.browserAction.onClicked.addListener(function(windowId) { |
| 128 goToInbox(); |
| 129 }); |
| 130 |
| 131 </script> |
| 132 </head> |
| 133 <body onload="init()"> |
| 134 </body> |
| 135 </html> |
| 136 |
OLD | NEW |