| Index: chrome/test/data/extensions/samples/subscribe_page_action/background.html
|
| ===================================================================
|
| --- chrome/test/data/extensions/samples/subscribe_page_action/background.html (revision 19579)
|
| +++ chrome/test/data/extensions/samples/subscribe_page_action/background.html (working copy)
|
| @@ -1,8 +1,13 @@
|
| <html>
|
| +<head>
|
| <script>
|
| // The Page Action ID.
|
| var pageActionId = "RssPageAction";
|
|
|
| + // The icons to use. These correspond to the icons listed in the manifest.
|
| + var subscribeId = 0;
|
| + var alreadySubscribedId = 1;
|
| +
|
| // The window this Page Action is associated with.
|
| var windowId = -1;
|
|
|
| @@ -15,6 +20,32 @@
|
| // The feed URL found on the page.
|
| var feedUrl = "";
|
|
|
| + // The URL to use to check if user is subscribed already.
|
| + var subscribedUrl = "http://www.google.com/reader/api/0/subscribed?s=feed%2F";
|
| +
|
| + // The XMLHttpRequest object that checks if you are already subscribed.
|
| + var req;
|
| +
|
| + // The status of whether you have already subscribed to this feed or not.
|
| + var alreadySubscribed = false;
|
| +
|
| + function EnableIcon(subscribed) {
|
| + alreadySubscribed = subscribed;
|
| + if (!alreadySubscribed) {
|
| + chrome.pageActions.enableForTab(
|
| + pageActionId, {tabId: tabId,
|
| + url: pageUrl,
|
| + title: "Click to subscribe...",
|
| + iconId: subscribeId});
|
| + } else {
|
| + chrome.pageActions.enableForTab(
|
| + pageActionId, {tabId: tabId,
|
| + url: pageUrl,
|
| + title: "You are already subscribed to this feed",
|
| + iconId: alreadySubscribedId});
|
| + }
|
| + }
|
| +
|
| chrome.self.onConnect.addListener(function(port) {
|
| windowId = port.tab.windowId;
|
| tabId = port.tab.id;
|
| @@ -28,17 +59,29 @@
|
| // Let Chrome know that the PageAction needs to be enabled for this tabId
|
| // and for the url of this page.
|
| if (feedUrl) {
|
| - chrome.pageActions.enableForTab(pageActionId,
|
| - {tabId: tabId, url: pageUrl});
|
| + EnableIcon(false); // Not subscribed (as far as we know, might change).
|
| +
|
| + // But also check the server to see if we have already subscribed so
|
| + // that we can update the status.
|
| + feedUrl = encodeURIComponent(feedUrl);
|
| + req = new XMLHttpRequest();
|
| + req.onload = handleResponse;
|
| + req.open("GET", subscribedUrl + feedUrl, false);
|
| + req.send(null);
|
| }
|
| });
|
| });
|
|
|
| + function handleResponse() {
|
| + if (req.responseText == "true")
|
| + EnableIcon(true); // true == Already suscribed.
|
| + }
|
| +
|
| // Chrome will call into us when the user clicks on the icon in the OmniBox.
|
| chrome.pageActions.onExecute.addListener(function(reply) {
|
| chrome.windows.getCurrent(function(window) {
|
| chrome.tabs.get(reply.data.tabId, function(tab) {
|
| - if (window.focused) {
|
| + if (!alreadySubscribed && window.focused) {
|
| // We need to know if we are the active window, because the tab may
|
| // have moved to another window and we don't want to execute this
|
| // action multiple times.
|
| @@ -46,7 +89,6 @@
|
| reply.data.tabUrl == pageUrl) {
|
| // Create a new tab showing the subscription page with the right
|
| // feed URL.
|
| - var url = "http://www.google.com/reader/view/feed/" + feedUrl;
|
| chrome.tabs.create({url: "subscribe.html?" + feedUrl,
|
| windowId: windowId});
|
| }
|
| @@ -55,4 +97,5 @@
|
| });
|
| });
|
| </script>
|
| +</head>
|
| </html>
|
|
|