Chromium Code Reviews| Index: chrome/common/extensions/docs/examples/extensions/gmail/background.js |
| diff --git a/chrome/common/extensions/docs/examples/extensions/gmail/background.js b/chrome/common/extensions/docs/examples/extensions/gmail/background.js |
| index 155c67c43d15b555153c16ed7388f909c5009050..f4fc8ce17413569b3009f25a01372c806c379e17 100644 |
| --- a/chrome/common/extensions/docs/examples/extensions/gmail/background.js |
| +++ b/chrome/common/extensions/docs/examples/extensions/gmail/background.js |
| @@ -7,15 +7,14 @@ |
| var instanceId = 'gmc' + parseInt(Date.now() * Math.random(), 10); |
| var animationFrames = 36; |
| var animationSpeed = 10; // ms |
| -var canvas; |
| -var canvasContext; |
| -var loggedInImage; |
| +var canvas = document.getElementById('canvas'); |
|
Aaron Boodman
2012/09/13 04:03:06
See note for init().
|
| +var loggedInImage = document.getElementById('logged_in'); |
| +var canvasContext = canvas.getContext('2d'); |
| var pollIntervalMin = 5; // 5 minutes |
| var pollIntervalMax = 60; // 1 hour |
| var requestFailureCount = 0; // used for exponential backoff |
| var requestTimeout = 1000 * 2; // 2 seconds |
| var rotation = 0; |
| -var unreadCount = -1; |
|
Aaron Boodman
2012/09/13 04:03:06
Moved unreadCount into local storage so that we ca
|
| var loadingAnimation = new LoadingAnimation(); |
| // Legacy support for pre-event-pages. |
| @@ -25,7 +24,7 @@ var requestTimerId; |
| function getGmailUrl() { |
| var url = "https://mail.google.com/"; |
| if (localStorage.customDomain) |
| - url += localStorage.customDomain + "/"; |
| + url += localStorage.customDomain.replace(/^\/?(.*?)\/?$/, '$1') + '/'; |
|
Aaron Boodman
2012/09/13 04:03:06
I'll remove this in a new patchset.
|
| else |
| url += "mail/" |
| return url; |
| @@ -91,15 +90,8 @@ LoadingAnimation.prototype.stop = function() { |
| this.timerId_ = 0; |
| } |
| -function init() { |
|
Aaron Boodman
2012/09/13 04:03:06
Because the unreadCount wasn't saved, the updateIc
|
| - canvas = document.getElementById('canvas'); |
| - loggedInImage = document.getElementById('logged_in'); |
| - canvasContext = canvas.getContext('2d'); |
| - updateIcon(); |
| -} |
| - |
| function updateIcon() { |
| - if (unreadCount == -1) { |
| + if (!localStorage.hasOwnProperty('unreadCount')) { |
| chrome.browserAction.setIcon({path:"gmail_not_logged_in.png"}); |
| chrome.browserAction.setBadgeBackgroundColor({color:[190, 190, 190, 230]}); |
| chrome.browserAction.setBadgeText({text:"?"}); |
| @@ -107,7 +99,7 @@ function updateIcon() { |
| chrome.browserAction.setIcon({path: "gmail_logged_in.png"}); |
| chrome.browserAction.setBadgeBackgroundColor({color:[208, 0, 24, 255]}); |
| chrome.browserAction.setBadgeText({ |
| - text: unreadCount != "0" ? unreadCount : "" |
| + text: localStorage.unreadCount != "0" ? localStorage.unreadCount : "" |
| }); |
| } |
| } |
| @@ -117,6 +109,7 @@ function scheduleRequest() { |
| var exponent = Math.pow(2, requestFailureCount); |
| var multiplier = Math.max(randomness * exponent, 1); |
| var delay = Math.min(multiplier * pollIntervalMin, pollIntervalMax); |
| + console.log('Scheduling for: ' + delay); |
| if (oldChromeVersion) { |
| if (requestTimerId) { |
| @@ -149,7 +142,7 @@ function startRequest(params) { |
| }, |
| function() { |
| stopLoadingAnimation(); |
| - unreadCount = -1; |
| + delete localStorage.unreadCount; |
| updateIcon(); |
| doCallback(); |
| } |
| @@ -179,7 +172,7 @@ function getInboxCount(onSuccess, onError) { |
| } |
| try { |
| - xhr.onreadystatechange = function(){ |
| + xhr.onreadystatechange = function() { |
| if (xhr.readyState != 4) |
| return; |
| @@ -189,6 +182,15 @@ function getInboxCount(onSuccess, onError) { |
| xmlDoc, gmailNSResolver, XPathResult.ANY_TYPE, null); |
| var fullCountNode = fullCountSet.iterateNext(); |
| if (fullCountNode) { |
| + var result = xmlDoc.evaluate("/gmail:feed/gmail:link/@href", |
|
Aaron Boodman
2012/09/13 04:03:06
This handles the case where mail.google.com/mail/
Aaron Boodman
2012/09/13 05:00:26
Nevermind, tested on a non-multilogin gmail accoun
|
| + xmlDoc, gmailNSResolver, XPathResult.STRING_TYPE, null); |
| + if (result.resultType == XPathResult.STRING_TYPE) { |
| + var anchor = document.createElement('a'); |
| + anchor.href = result.stringValue; |
| + localStorage.customDomain = |
| + anchor.pathname.replace(/^\/?(.*?)\/?$/, '$1'); |
| + console.log(localStorage.customDomain); |
| + } |
| handleSuccess(fullCountNode.textContent); |
| return; |
| } else { |
| @@ -218,8 +220,8 @@ function gmailNSResolver(prefix) { |
| } |
| function updateUnreadCount(count) { |
| - var changed = unreadCount != count; |
| - unreadCount = count; |
| + var changed = localStorage.unreadCount != count; |
| + localStorage.unreadCount = count; |
| updateIcon(); |
| if (changed) |
| animateFlip(); |
| @@ -238,11 +240,7 @@ function animateFlip() { |
| setTimeout(animateFlip, animationSpeed); |
| } else { |
| rotation = 0; |
| - drawIconAtRotation(); |
| - chrome.browserAction.setBadgeText({ |
| - text: unreadCount != "0" ? unreadCount : "" |
| - }); |
| - chrome.browserAction.setBadgeBackgroundColor({color:[208, 0, 24, 255]}); |
| + updateIcon(); |
| } |
| } |
| @@ -275,23 +273,21 @@ function goToInbox() { |
| }); |
| } |
| -document.addEventListener('DOMContentLoaded', init); |
|
Aaron Boodman
2012/09/13 04:03:06
See note at init().
|
| - |
| function onInit() { |
| startRequest({scheduleRequest:true, showLoadingAnimation:true}); |
| } |
| +function onAlarm() { |
|
Aaron Boodman
2012/09/13 04:03:06
Just moved from below.
|
| + startRequest({scheduleRequest:true, showLoadingAnimation:false}); |
| +} |
| + |
| if (oldChromeVersion) { |
| + updateIcon(); |
|
Aaron Boodman
2012/09/13 04:03:06
See note at init().
|
| onInit(); |
| } else { |
| chrome.runtime.onInstalled.addListener(onInit); |
| -} |
| - |
| -function onAlarm() { |
| - startRequest({scheduleRequest:true, showLoadingAnimation:false}); |
| -} |
| -if (!oldChromeVersion) |
| chrome.alarms.onAlarm.addListener(onAlarm); |
| +} |
| var filters = { |
| // TODO(aa): Cannot use urlPrefix because all the url fields lack the protocol |
| @@ -300,6 +296,8 @@ var filters = { |
| }; |
| chrome.webNavigation.onDOMContentLoaded.addListener(function(changeInfo) { |
| + console.log(changeInfo.url); |
| + console.log(isGmailUrl(changeInfo.url)); |
| if (changeInfo.url && isGmailUrl(changeInfo.url)) { |
| startRequest({scheduleRequest:false, showLoadingAnimation:false}); |
| } |