diff options
author | mkwst@chromium.org <mkwst@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-01-25 21:19:17 +0000 |
---|---|---|
committer | mkwst@chromium.org <mkwst@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-01-25 21:19:17 +0000 |
commit | ef351ec456725f925fe45dfb6975e0cf6c687291 (patch) | |
tree | b4dfe184d9478a5a147da522ad709f45a0a3455d /chrome/common | |
parent | ee73a1da2ba3d54690d0bdf10f348ac7b92cf95e (diff) | |
download | chromium_src-ef351ec456725f925fe45dfb6975e0cf6c687291.zip chromium_src-ef351ec456725f925fe45dfb6975e0cf6c687291.tar.gz chromium_src-ef351ec456725f925fe45dfb6975e0cf6c687291.tar.bz2 |
Updating the Gmail sample extension to manifest version 2.
BUG=111049
TEST=
Review URL: https://chromiumcodereview.appspot.com/9146040
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@119117 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/common')
-rw-r--r-- | chrome/common/extensions/docs/examples/extensions/gmail.zip | bin | 56165 -> 57157 bytes | |||
-rw-r--r-- | chrome/common/extensions/docs/examples/extensions/gmail/background.html | 271 | ||||
-rw-r--r-- | chrome/common/extensions/docs/examples/extensions/gmail/background.js | 271 | ||||
-rw-r--r-- | chrome/common/extensions/docs/examples/extensions/gmail/manifest.json | 4 | ||||
-rw-r--r-- | chrome/common/extensions/docs/examples/extensions/gmail/options.html | 35 | ||||
-rw-r--r-- | chrome/common/extensions/docs/examples/extensions/gmail/options.js | 37 | ||||
-rw-r--r-- | chrome/common/extensions/docs/samples.html | 4 | ||||
-rw-r--r-- | chrome/common/extensions/docs/samples.json | 6 |
8 files changed, 324 insertions, 304 deletions
diff --git a/chrome/common/extensions/docs/examples/extensions/gmail.zip b/chrome/common/extensions/docs/examples/extensions/gmail.zip Binary files differindex 7bda587..40b4b3a 100644 --- a/chrome/common/extensions/docs/examples/extensions/gmail.zip +++ b/chrome/common/extensions/docs/examples/extensions/gmail.zip diff --git a/chrome/common/extensions/docs/examples/extensions/gmail/background.html b/chrome/common/extensions/docs/examples/extensions/gmail/background.html index bce2c3f..054b9ef 100644 --- a/chrome/common/extensions/docs/examples/extensions/gmail/background.html +++ b/chrome/common/extensions/docs/examples/extensions/gmail/background.html @@ -1,275 +1,8 @@ <html> <head> -<script> -// Identifier used to debug the possibility of multiple instances of the -// extension making requests on behalf of a single user. -var instanceId = 'gmc' + parseInt(Date.now() * Math.random(), 10); -var animationFrames = 36; -var animationSpeed = 10; // ms -var canvas; -var canvasContext; -var loggedInImage; -var pollIntervalMin = 1000 * 60; // 1 minute -var pollIntervalMax = 1000 * 60 * 60; // 1 hour -var requestFailureCount = 0; // used for exponential backoff -var requestTimeout = 1000 * 2; // 5 seconds -var rotation = 0; -var unreadCount = -1; -var loadingAnimation = new LoadingAnimation(); -var requestTimerId; - -function getGmailUrl() { - var url = "https://mail.google.com/"; - if (localStorage.customDomain) - url += localStorage.customDomain + "/"; - else - url += "mail/" - return url; -} - -function getFeedUrl() { - // "zx" is a Gmail query parameter that is expected to contain a random - // string and may be ignored/stripped. - return getGmailUrl() + "feed/atom?zx=" + encodeURIComponent(instanceId); -} - -function isGmailUrl(url) { - // This is the Gmail we're looking for if: - // - starts with the correct gmail url - // - doesn't contain any other path chars - var gmail = getGmailUrl(); - if (url.indexOf(gmail) != 0) - return false; - - return url.length == gmail.length || url[gmail.length] == '?' || - url[gmail.length] == '#'; -} - -// A "loading" animation displayed while we wait for the first response from -// Gmail. This animates the badge text with a dot that cycles from left to -// right. -function LoadingAnimation() { - this.timerId_ = 0; - this.maxCount_ = 8; // Total number of states in animation - this.current_ = 0; // Current state - this.maxDot_ = 4; // Max number of dots in animation -} - -LoadingAnimation.prototype.paintFrame = function() { - var text = ""; - for (var i = 0; i < this.maxDot_; i++) { - text += (i == this.current_) ? "." : " "; - } - if (this.current_ >= this.maxDot_) - text += ""; - - chrome.browserAction.setBadgeText({text:text}); - this.current_++; - if (this.current_ == this.maxCount_) - this.current_ = 0; -} - -LoadingAnimation.prototype.start = function() { - if (this.timerId_) - return; - - var self = this; - this.timerId_ = window.setInterval(function() { - self.paintFrame(); - }, 100); -} - -LoadingAnimation.prototype.stop = function() { - if (!this.timerId_) - return; - - window.clearInterval(this.timerId_); - this.timerId_ = 0; -} - - -chrome.tabs.onUpdated.addListener(function(tabId, changeInfo) { - if (changeInfo.url && isGmailUrl(changeInfo.url)) { - getInboxCount(function(count) { - updateUnreadCount(count); - }); - } -}); - - -function init() { - canvas = document.getElementById('canvas'); - loggedInImage = document.getElementById('logged_in'); - canvasContext = canvas.getContext('2d'); - - chrome.browserAction.setBadgeBackgroundColor({color:[208, 0, 24, 255]}); - chrome.browserAction.setIcon({path: "gmail_logged_in.png"}); - loadingAnimation.start(); - - startRequest(); -} - -function scheduleRequest() { - if (requestTimerId) { - window.clearTimeout(requestTimerId); - } - var randomness = Math.random() * 2; - var exponent = Math.pow(2, requestFailureCount); - var multiplier = Math.max(randomness * exponent, 1); - var delay = Math.min(multiplier * pollIntervalMin, pollIntervalMax); - delay = Math.round(delay); - - requestTimerId = window.setTimeout(startRequest, delay); -} - -// ajax stuff -function startRequest() { - getInboxCount( - function(count) { - loadingAnimation.stop(); - updateUnreadCount(count); - scheduleRequest(); - }, - function() { - loadingAnimation.stop(); - showLoggedOut(); - scheduleRequest(); - } - ); -} - -function getInboxCount(onSuccess, onError) { - var xhr = new XMLHttpRequest(); - var abortTimerId = window.setTimeout(function() { - xhr.abort(); // synchronously calls onreadystatechange - }, requestTimeout); - - function handleSuccess(count) { - requestFailureCount = 0; - window.clearTimeout(abortTimerId); - if (onSuccess) - onSuccess(count); - } - - var invokedErrorCallback = false; - function handleError() { - ++requestFailureCount; - window.clearTimeout(abortTimerId); - if (onError && !invokedErrorCallback) - onError(); - invokedErrorCallback = true; - } - - try { - xhr.onreadystatechange = function(){ - if (xhr.readyState != 4) - return; - - if (xhr.responseXML) { - var xmlDoc = xhr.responseXML; - var fullCountSet = xmlDoc.evaluate("/gmail:feed/gmail:fullcount", - xmlDoc, gmailNSResolver, XPathResult.ANY_TYPE, null); - var fullCountNode = fullCountSet.iterateNext(); - if (fullCountNode) { - handleSuccess(fullCountNode.textContent); - return; - } else { - console.error(chrome.i18n.getMessage("gmailcheck_node_error")); - } - } - - handleError(); - } - - xhr.onerror = function(error) { - handleError(); - } - - xhr.open("GET", getFeedUrl(), true); - xhr.send(null); - } catch(e) { - console.error(chrome.i18n.getMessage("gmailcheck_exception", e)); - handleError(); - } -} - -function gmailNSResolver(prefix) { - if(prefix == 'gmail') { - return 'http://purl.org/atom/ns#'; - } -} - -function updateUnreadCount(count) { - if (unreadCount != count) { - unreadCount = count; - animateFlip(); - } -} - - -function ease(x) { - return (1-Math.sin(Math.PI/2+x*Math.PI))/2; -} - -function animateFlip() { - rotation += 1/animationFrames; - drawIconAtRotation(); - - if (rotation <= 1) { - setTimeout("animateFlip()", animationSpeed); - } else { - rotation = 0; - drawIconAtRotation(); - chrome.browserAction.setBadgeText({ - text: unreadCount != "0" ? unreadCount : "" - }); - chrome.browserAction.setBadgeBackgroundColor({color:[208, 0, 24, 255]}); - } -} - -function showLoggedOut() { - unreadCount = -1; - chrome.browserAction.setIcon({path:"gmail_not_logged_in.png"}); - chrome.browserAction.setBadgeBackgroundColor({color:[190, 190, 190, 230]}); - chrome.browserAction.setBadgeText({text:"?"}); -} - -function drawIconAtRotation() { - canvasContext.save(); - canvasContext.clearRect(0, 0, canvas.width, canvas.height); - canvasContext.translate( - Math.ceil(canvas.width/2), - Math.ceil(canvas.height/2)); - canvasContext.rotate(2*Math.PI*ease(rotation)); - canvasContext.drawImage(loggedInImage, - -Math.ceil(canvas.width/2), - -Math.ceil(canvas.height/2)); - canvasContext.restore(); - - chrome.browserAction.setIcon({imageData:canvasContext.getImageData(0, 0, - canvas.width,canvas.height)}); -} - -function goToInbox() { - chrome.tabs.getAllInWindow(undefined, function(tabs) { - for (var i = 0, tab; tab = tabs[i]; i++) { - if (tab.url && isGmailUrl(tab.url)) { - chrome.tabs.update(tab.id, {selected: true}); - return; - } - } - chrome.tabs.create({url: getGmailUrl()}); - }); -} - -// Called when the user clicks on the browser action. -chrome.browserAction.onClicked.addListener(function(tab) { - goToInbox(); -}); - -</script> +<script src="background.js"></script> </head> -<body onload="init()"> +<body> <img id="logged_in" src="gmail_logged_in.png"> <canvas id="canvas" width="19" height="19"> </body> diff --git a/chrome/common/extensions/docs/examples/extensions/gmail/background.js b/chrome/common/extensions/docs/examples/extensions/gmail/background.js new file mode 100644 index 0000000..98135cb --- /dev/null +++ b/chrome/common/extensions/docs/examples/extensions/gmail/background.js @@ -0,0 +1,271 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Identifier used to debug the possibility of multiple instances of the +// extension making requests on behalf of a single user. +var instanceId = 'gmc' + parseInt(Date.now() * Math.random(), 10); +var animationFrames = 36; +var animationSpeed = 10; // ms +var canvas; +var canvasContext; +var loggedInImage; +var pollIntervalMin = 1000 * 60; // 1 minute +var pollIntervalMax = 1000 * 60 * 60; // 1 hour +var requestFailureCount = 0; // used for exponential backoff +var requestTimeout = 1000 * 2; // 5 seconds +var rotation = 0; +var unreadCount = -1; +var loadingAnimation = new LoadingAnimation(); +var requestTimerId; + +function getGmailUrl() { + var url = "https://mail.google.com/"; + if (localStorage.customDomain) + url += localStorage.customDomain + "/"; + else + url += "mail/" + return url; +} + +function getFeedUrl() { + // "zx" is a Gmail query parameter that is expected to contain a random + // string and may be ignored/stripped. + return getGmailUrl() + "feed/atom?zx=" + encodeURIComponent(instanceId); +} + +function isGmailUrl(url) { + // This is the Gmail we're looking for if: + // - starts with the correct gmail url + // - doesn't contain any other path chars + var gmail = getGmailUrl(); + if (url.indexOf(gmail) != 0) + return false; + + return url.length == gmail.length || url[gmail.length] == '?' || + url[gmail.length] == '#'; +} + +// A "loading" animation displayed while we wait for the first response from +// Gmail. This animates the badge text with a dot that cycles from left to +// right. +function LoadingAnimation() { + this.timerId_ = 0; + this.maxCount_ = 8; // Total number of states in animation + this.current_ = 0; // Current state + this.maxDot_ = 4; // Max number of dots in animation +} + +LoadingAnimation.prototype.paintFrame = function() { + var text = ""; + for (var i = 0; i < this.maxDot_; i++) { + text += (i == this.current_) ? "." : " "; + } + if (this.current_ >= this.maxDot_) + text += ""; + + chrome.browserAction.setBadgeText({text:text}); + this.current_++; + if (this.current_ == this.maxCount_) + this.current_ = 0; +} + +LoadingAnimation.prototype.start = function() { + if (this.timerId_) + return; + + var self = this; + this.timerId_ = window.setInterval(function() { + self.paintFrame(); + }, 100); +} + +LoadingAnimation.prototype.stop = function() { + if (!this.timerId_) + return; + + window.clearInterval(this.timerId_); + this.timerId_ = 0; +} + + +chrome.tabs.onUpdated.addListener(function(tabId, changeInfo) { + if (changeInfo.url && isGmailUrl(changeInfo.url)) { + getInboxCount(function(count) { + updateUnreadCount(count); + }); + } +}); + + +function init() { + canvas = document.getElementById('canvas'); + loggedInImage = document.getElementById('logged_in'); + canvasContext = canvas.getContext('2d'); + + chrome.browserAction.setBadgeBackgroundColor({color:[208, 0, 24, 255]}); + chrome.browserAction.setIcon({path: "gmail_logged_in.png"}); + loadingAnimation.start(); + + startRequest(); +} + +function scheduleRequest() { + if (requestTimerId) { + window.clearTimeout(requestTimerId); + } + var randomness = Math.random() * 2; + var exponent = Math.pow(2, requestFailureCount); + var multiplier = Math.max(randomness * exponent, 1); + var delay = Math.min(multiplier * pollIntervalMin, pollIntervalMax); + delay = Math.round(delay); + + requestTimerId = window.setTimeout(startRequest, delay); +} + +// ajax stuff +function startRequest() { + getInboxCount( + function(count) { + loadingAnimation.stop(); + updateUnreadCount(count); + scheduleRequest(); + }, + function() { + loadingAnimation.stop(); + showLoggedOut(); + scheduleRequest(); + } + ); +} + +function getInboxCount(onSuccess, onError) { + var xhr = new XMLHttpRequest(); + var abortTimerId = window.setTimeout(function() { + xhr.abort(); // synchronously calls onreadystatechange + }, requestTimeout); + + function handleSuccess(count) { + requestFailureCount = 0; + window.clearTimeout(abortTimerId); + if (onSuccess) + onSuccess(count); + } + + var invokedErrorCallback = false; + function handleError() { + ++requestFailureCount; + window.clearTimeout(abortTimerId); + if (onError && !invokedErrorCallback) + onError(); + invokedErrorCallback = true; + } + + try { + xhr.onreadystatechange = function(){ + if (xhr.readyState != 4) + return; + + if (xhr.responseXML) { + var xmlDoc = xhr.responseXML; + var fullCountSet = xmlDoc.evaluate("/gmail:feed/gmail:fullcount", + xmlDoc, gmailNSResolver, XPathResult.ANY_TYPE, null); + var fullCountNode = fullCountSet.iterateNext(); + if (fullCountNode) { + handleSuccess(fullCountNode.textContent); + return; + } else { + console.error(chrome.i18n.getMessage("gmailcheck_node_error")); + } + } + + handleError(); + } + + xhr.onerror = function(error) { + handleError(); + } + + xhr.open("GET", getFeedUrl(), true); + xhr.send(null); + } catch(e) { + console.error(chrome.i18n.getMessage("gmailcheck_exception", e)); + handleError(); + } +} + +function gmailNSResolver(prefix) { + if(prefix == 'gmail') { + return 'http://purl.org/atom/ns#'; + } +} + +function updateUnreadCount(count) { + if (unreadCount != count) { + unreadCount = count; + animateFlip(); + } +} + + +function ease(x) { + return (1-Math.sin(Math.PI/2+x*Math.PI))/2; +} + +function animateFlip() { + rotation += 1/animationFrames; + drawIconAtRotation(); + + if (rotation <= 1) { + setTimeout(animateFlip, animationSpeed); + } else { + rotation = 0; + drawIconAtRotation(); + chrome.browserAction.setBadgeText({ + text: unreadCount != "0" ? unreadCount : "" + }); + chrome.browserAction.setBadgeBackgroundColor({color:[208, 0, 24, 255]}); + } +} + +function showLoggedOut() { + unreadCount = -1; + chrome.browserAction.setIcon({path:"gmail_not_logged_in.png"}); + chrome.browserAction.setBadgeBackgroundColor({color:[190, 190, 190, 230]}); + chrome.browserAction.setBadgeText({text:"?"}); +} + +function drawIconAtRotation() { + canvasContext.save(); + canvasContext.clearRect(0, 0, canvas.width, canvas.height); + canvasContext.translate( + Math.ceil(canvas.width/2), + Math.ceil(canvas.height/2)); + canvasContext.rotate(2*Math.PI*ease(rotation)); + canvasContext.drawImage(loggedInImage, + -Math.ceil(canvas.width/2), + -Math.ceil(canvas.height/2)); + canvasContext.restore(); + + chrome.browserAction.setIcon({imageData:canvasContext.getImageData(0, 0, + canvas.width,canvas.height)}); +} + +function goToInbox() { + chrome.tabs.getAllInWindow(undefined, function(tabs) { + for (var i = 0, tab; tab = tabs[i]; i++) { + if (tab.url && isGmailUrl(tab.url)) { + chrome.tabs.update(tab.id, {selected: true}); + return; + } + } + chrome.tabs.create({url: getGmailUrl()}); + }); +} + +// Called when the user clicks on the browser action. +chrome.browserAction.onClicked.addListener(function(tab) { + goToInbox(); +}); + +document.addEventListener('DOMContentLoaded', init); diff --git a/chrome/common/extensions/docs/examples/extensions/gmail/manifest.json b/chrome/common/extensions/docs/examples/extensions/gmail/manifest.json index 4449e71..ebd3a52 100644 --- a/chrome/common/extensions/docs/examples/extensions/gmail/manifest.json +++ b/chrome/common/extensions/docs/examples/extensions/gmail/manifest.json @@ -15,5 +15,7 @@ "permissions": [ "tabs", "http://*.google.com/", "https://*.google.com/" ], - "version": "3.2" + "version": "3.3", + + "manifest_version": 2 } diff --git a/chrome/common/extensions/docs/examples/extensions/gmail/options.html b/chrome/common/extensions/docs/examples/extensions/gmail/options.html index 5613c72..8c48ce0 100644 --- a/chrome/common/extensions/docs/examples/extensions/gmail/options.html +++ b/chrome/common/extensions/docs/examples/extensions/gmail/options.html @@ -47,6 +47,7 @@ body { margin-top:4em; } </style> +<script src="options.js"></script> </head> <body> @@ -59,39 +60,9 @@ body { <em>(eg a/mydomain.com)</em> <div id="footer"> - <button id="save-button" style="font-weight:bold" onclick="save()" - >Save</button> - <button onclick="init()">Cancel</button> + <button id="save-button" style="font-weight:bold">Save</button> + <button id="cancel-button">Cancel</button> </div> -<script> -var customDomainsTextbox; -var saveButton; - -init(); - -function init() { - customDomainsTextbox = document.getElementById("custom-domain"); - saveButton = document.getElementById("save-button"); - - customDomainsTextbox.value = localStorage.customDomain || ""; - markClean(); -} - -function save() { - localStorage.customDomain = customDomainsTextbox.value; - markClean(); - - chrome.extension.getBackgroundPage().init(); -} - -function markDirty() { - saveButton.disabled = false; -} - -function markClean() { - saveButton.disabled = true; -} -</script> </body> </html> diff --git a/chrome/common/extensions/docs/examples/extensions/gmail/options.js b/chrome/common/extensions/docs/examples/extensions/gmail/options.js new file mode 100644 index 0000000..c862c6b --- /dev/null +++ b/chrome/common/extensions/docs/examples/extensions/gmail/options.js @@ -0,0 +1,37 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +var customDomainsTextbox; +var saveButton; + +function init() { + customDomainsTextbox = document.getElementById("custom-domain"); + saveButton = document.getElementById("save-button"); + + customDomainsTextbox.value = localStorage.customDomain || ""; + markClean(); +} + +function save() { + localStorage.customDomain = customDomainsTextbox.value; + markClean(); + + chrome.extension.getBackgroundPage().init(); +} + +function markDirty() { + saveButton.disabled = false; +} + +function markClean() { + saveButton.disabled = true; +} + +document.addEventListener('DOMContentLoaded', function () { + init(); + document.querySelector('#cancel-button').addEventListener('DOMContentLoaded', + init); + document.querySelector('#save-button').addEventListener('DOMContentLoaded', + save); +}); diff --git a/chrome/common/extensions/docs/samples.html b/chrome/common/extensions/docs/samples.html index 11788bf..75cf8c6 100644 --- a/chrome/common/extensions/docs/samples.html +++ b/chrome/common/extensions/docs/samples.html @@ -2066,9 +2066,13 @@ </li><li> <code><a target="_blank" href="examples/extensions/gmail/background.html">background.html</a></code> </li><li> + <code><a target="_blank" href="examples/extensions/gmail/background.js">background.js</a></code> + </li><li> <code><a target="_blank" href="examples/extensions/gmail/manifest.json">manifest.json</a></code> </li><li> <code><a target="_blank" href="examples/extensions/gmail/options.html">options.html</a></code> + </li><li> + <code><a target="_blank" href="examples/extensions/gmail/options.js">options.js</a></code> </li> </ul> </div> diff --git a/chrome/common/extensions/docs/samples.json b/chrome/common/extensions/docs/samples.json index e42e123..c4337a5 100644 --- a/chrome/common/extensions/docs/samples.json +++ b/chrome/common/extensions/docs/samples.json @@ -1127,10 +1127,12 @@ "_locales\/zh_CN\/messages.json", "_locales\/zh_TW\/messages.json", "background.html", + "background.js", "manifest.json", - "options.html" + "options.html", + "options.js" ], - "source_hash": "0444302416c0220269a5ae3dc9bc0b1d93fb0f5a", + "source_hash": "13f0a37272c82d2fd1f0ea295ebf46ce99018337", "zip_path": "examples\/extensions\/gmail.zip" }, { |