| OLD | NEW |
| 1 <html> | 1 <html> |
| 2 <head> | 2 <head> |
| 3 <script> | 3 <script> |
| 4 // Identifier used to debug the possibility of multiple instances of the | 4 // Identifier used to debug the possibility of multiple instances of the |
| 5 // extension making requests on behalf of a single user. | 5 // extension making requests on behalf of a single user. |
| 6 var instanceId = 'gmc' + parseInt(Date.now() * Math.random(), 10); | 6 var instanceId = 'gmc' + parseInt(Date.now() * Math.random(), 10); |
| 7 var animationFrames = 36; | 7 var animationFrames = 36; |
| 8 var animationSpeed = 10; // ms | 8 var animationSpeed = 10; // ms |
| 9 var canvas; | 9 var canvas; |
| 10 var canvasContext; | 10 var canvasContext; |
| 11 var loggedInImage; | 11 var loggedInImage; |
| 12 var pollIntervalMin = 1000 * 60; // 1 minute | 12 var pollIntervalMin = 1000 * 60; // 1 minute |
| 13 var pollIntervalMax = 1000 * 60 * 60; // 1 hour | 13 var pollIntervalMax = 1000 * 60 * 60; // 1 hour |
| 14 var requestFailureCount = 0; // used for exponential backoff | 14 var requestFailureCount = 0; // used for exponential backoff |
| 15 var requestTimeout = 1000 * 2; // 5 seconds | 15 var requestTimeout = 1000 * 2; // 5 seconds |
| 16 var rotation = 0; | 16 var rotation = 0; |
| 17 var unreadCount = -1; | 17 var unreadCount = -1; |
| 18 var loadingAnimation = new LoadingAnimation(); | 18 var loadingAnimation = new LoadingAnimation(); |
| 19 var requestTimerId; |
| 19 | 20 |
| 20 function getGmailUrl() { | 21 function getGmailUrl() { |
| 21 var url = "https://mail.google.com/"; | 22 var url = "https://mail.google.com/"; |
| 22 if (localStorage.customDomain) | 23 if (localStorage.customDomain) |
| 23 url += localStorage.customDomain + "/"; | 24 url += localStorage.customDomain + "/"; |
| 24 else | 25 else |
| 25 url += "mail/" | 26 url += "mail/" |
| 26 return url; | 27 return url; |
| 27 } | 28 } |
| 28 | 29 |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 canvasContext = canvas.getContext('2d'); | 103 canvasContext = canvas.getContext('2d'); |
| 103 | 104 |
| 104 chrome.browserAction.setBadgeBackgroundColor({color:[208, 0, 24, 255]}); | 105 chrome.browserAction.setBadgeBackgroundColor({color:[208, 0, 24, 255]}); |
| 105 chrome.browserAction.setIcon({path: "gmail_logged_in.png"}); | 106 chrome.browserAction.setIcon({path: "gmail_logged_in.png"}); |
| 106 loadingAnimation.start(); | 107 loadingAnimation.start(); |
| 107 | 108 |
| 108 startRequest(); | 109 startRequest(); |
| 109 } | 110 } |
| 110 | 111 |
| 111 function scheduleRequest() { | 112 function scheduleRequest() { |
| 113 if (requestTimerId) { |
| 114 window.clearTimeout(requestTimerId); |
| 115 } |
| 112 var randomness = Math.random() * 2; | 116 var randomness = Math.random() * 2; |
| 113 var exponent = Math.pow(2, requestFailureCount); | 117 var exponent = Math.pow(2, requestFailureCount); |
| 114 var multiplier = Math.max(randomness * exponent, 1); | 118 var multiplier = Math.max(randomness * exponent, 1); |
| 115 var delay = Math.min(multiplier * pollIntervalMin, pollIntervalMax); | 119 var delay = Math.min(multiplier * pollIntervalMin, pollIntervalMax); |
| 116 delay = Math.round(delay); | 120 delay = Math.round(delay); |
| 117 | 121 |
| 118 window.setTimeout(startRequest, delay); | 122 requestTimerId = window.setTimeout(startRequest, delay); |
| 119 } | 123 } |
| 120 | 124 |
| 121 // ajax stuff | 125 // ajax stuff |
| 122 function startRequest() { | 126 function startRequest() { |
| 123 getInboxCount( | 127 getInboxCount( |
| 124 function(count) { | 128 function(count) { |
| 125 loadingAnimation.stop(); | 129 loadingAnimation.stop(); |
| 126 updateUnreadCount(count); | 130 updateUnreadCount(count); |
| 127 scheduleRequest(); | 131 scheduleRequest(); |
| 128 }, | 132 }, |
| (...skipping 11 matching lines...) Expand all Loading... |
| 140 xhr.abort(); // synchronously calls onreadystatechange | 144 xhr.abort(); // synchronously calls onreadystatechange |
| 141 }, requestTimeout); | 145 }, requestTimeout); |
| 142 | 146 |
| 143 function handleSuccess(count) { | 147 function handleSuccess(count) { |
| 144 requestFailureCount = 0; | 148 requestFailureCount = 0; |
| 145 window.clearTimeout(abortTimerId); | 149 window.clearTimeout(abortTimerId); |
| 146 if (onSuccess) | 150 if (onSuccess) |
| 147 onSuccess(count); | 151 onSuccess(count); |
| 148 } | 152 } |
| 149 | 153 |
| 154 var invokedErrorCallback = false; |
| 150 function handleError() { | 155 function handleError() { |
| 151 ++requestFailureCount; | 156 ++requestFailureCount; |
| 152 window.clearTimeout(abortTimerId); | 157 window.clearTimeout(abortTimerId); |
| 153 if (onError) | 158 if (onError && !invokedErrorCallback) |
| 154 onError(); | 159 onError(); |
| 160 invokedErrorCallback = true; |
| 155 } | 161 } |
| 156 | 162 |
| 157 try { | 163 try { |
| 158 xhr.onreadystatechange = function(){ | 164 xhr.onreadystatechange = function(){ |
| 159 if (xhr.readyState != 4) | 165 if (xhr.readyState != 4) |
| 160 return; | 166 return; |
| 161 | 167 |
| 162 if (xhr.responseXML) { | 168 if (xhr.responseXML) { |
| 163 var xmlDoc = xhr.responseXML; | 169 var xmlDoc = xhr.responseXML; |
| 164 var fullCountSet = xmlDoc.evaluate("/gmail:feed/gmail:fullcount", | 170 var fullCountSet = xmlDoc.evaluate("/gmail:feed/gmail:fullcount", |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 262 }); | 268 }); |
| 263 | 269 |
| 264 </script> | 270 </script> |
| 265 </head> | 271 </head> |
| 266 <body onload="init()"> | 272 <body onload="init()"> |
| 267 <img id="logged_in" src="gmail_logged_in.png"> | 273 <img id="logged_in" src="gmail_logged_in.png"> |
| 268 <canvas id="canvas" width="19" height="19"> | 274 <canvas id="canvas" width="19" height="19"> |
| 269 </body> | 275 </body> |
| 270 </html> | 276 </html> |
| 271 | 277 |
| OLD | NEW |