OLD | NEW |
1 <html> | 1 <html> |
| 2 <head> |
2 <script> | 3 <script> |
3 // The Page Action ID. | 4 // The Page Action ID. |
4 var pageActionId = "RssPageAction"; | 5 var pageActionId = "RssPageAction"; |
5 | 6 |
| 7 // The icons to use. These correspond to the icons listed in the manifest. |
| 8 var subscribeId = 0; |
| 9 var alreadySubscribedId = 1; |
| 10 |
6 // The window this Page Action is associated with. | 11 // The window this Page Action is associated with. |
7 var windowId = -1; | 12 var windowId = -1; |
8 | 13 |
9 // The TabId this Page Action is associated with. | 14 // The TabId this Page Action is associated with. |
10 var tabId = -1; | 15 var tabId = -1; |
11 | 16 |
12 // The URL of the page that contains the feed. | 17 // The URL of the page that contains the feed. |
13 var pageUrl = ""; | 18 var pageUrl = ""; |
14 | 19 |
15 // The feed URL found on the page. | 20 // The feed URL found on the page. |
16 var feedUrl = ""; | 21 var feedUrl = ""; |
17 | 22 |
| 23 // The URL to use to check if user is subscribed already. |
| 24 var subscribedUrl = "http://www.google.com/reader/api/0/subscribed?s=feed%2F"; |
| 25 |
| 26 // The XMLHttpRequest object that checks if you are already subscribed. |
| 27 var req; |
| 28 |
| 29 // The status of whether you have already subscribed to this feed or not. |
| 30 var alreadySubscribed = false; |
| 31 |
| 32 function EnableIcon(subscribed) { |
| 33 alreadySubscribed = subscribed; |
| 34 if (!alreadySubscribed) { |
| 35 chrome.pageActions.enableForTab( |
| 36 pageActionId, {tabId: tabId, |
| 37 url: pageUrl, |
| 38 title: "Click to subscribe...", |
| 39 iconId: subscribeId}); |
| 40 } else { |
| 41 chrome.pageActions.enableForTab( |
| 42 pageActionId, {tabId: tabId, |
| 43 url: pageUrl, |
| 44 title: "You are already subscribed to this feed", |
| 45 iconId: alreadySubscribedId}); |
| 46 } |
| 47 } |
| 48 |
18 chrome.self.onConnect.addListener(function(port) { | 49 chrome.self.onConnect.addListener(function(port) { |
19 windowId = port.tab.windowId; | 50 windowId = port.tab.windowId; |
20 tabId = port.tab.id; | 51 tabId = port.tab.id; |
21 pageUrl = port.tab.url; | 52 pageUrl = port.tab.url; |
22 | 53 |
23 // This will get called from the content script using PostMessage. | 54 // This will get called from the content script using PostMessage. |
24 // |feedUrls| is a list of URL feeds found on the page. We only need 1 to | 55 // |feedUrls| is a list of URL feeds found on the page. We only need 1 to |
25 // enable the PageAction icon in the Omnibox. | 56 // enable the PageAction icon in the Omnibox. |
26 port.onMessage.addListener(function(feedUrls) { | 57 port.onMessage.addListener(function(feedUrls) { |
27 feedUrl = feedUrls[0]; | 58 feedUrl = feedUrls[0]; |
28 // Let Chrome know that the PageAction needs to be enabled for this tabId | 59 // Let Chrome know that the PageAction needs to be enabled for this tabId |
29 // and for the url of this page. | 60 // and for the url of this page. |
30 if (feedUrl) { | 61 if (feedUrl) { |
31 chrome.pageActions.enableForTab(pageActionId, | 62 EnableIcon(false); // Not subscribed (as far as we know, might change). |
32 {tabId: tabId, url: pageUrl}); | 63 |
| 64 // But also check the server to see if we have already subscribed so |
| 65 // that we can update the status. |
| 66 feedUrl = encodeURIComponent(feedUrl); |
| 67 req = new XMLHttpRequest(); |
| 68 req.onload = handleResponse; |
| 69 req.open("GET", subscribedUrl + feedUrl, false); |
| 70 req.send(null); |
33 } | 71 } |
34 }); | 72 }); |
35 }); | 73 }); |
36 | 74 |
| 75 function handleResponse() { |
| 76 if (req.responseText == "true") |
| 77 EnableIcon(true); // true == Already suscribed. |
| 78 } |
| 79 |
37 // Chrome will call into us when the user clicks on the icon in the OmniBox. | 80 // Chrome will call into us when the user clicks on the icon in the OmniBox. |
38 chrome.pageActions.onExecute.addListener(function(reply) { | 81 chrome.pageActions.onExecute.addListener(function(reply) { |
39 chrome.windows.getCurrent(function(window) { | 82 chrome.windows.getCurrent(function(window) { |
40 chrome.tabs.get(reply.data.tabId, function(tab) { | 83 chrome.tabs.get(reply.data.tabId, function(tab) { |
41 if (window.focused) { | 84 if (!alreadySubscribed && window.focused) { |
42 // We need to know if we are the active window, because the tab may | 85 // We need to know if we are the active window, because the tab may |
43 // have moved to another window and we don't want to execute this | 86 // have moved to another window and we don't want to execute this |
44 // action multiple times. | 87 // action multiple times. |
45 if (reply.pageActionId == pageActionId && | 88 if (reply.pageActionId == pageActionId && |
46 reply.data.tabUrl == pageUrl) { | 89 reply.data.tabUrl == pageUrl) { |
47 // Create a new tab showing the subscription page with the right | 90 // Create a new tab showing the subscription page with the right |
48 // feed URL. | 91 // feed URL. |
49 var url = "http://www.google.com/reader/view/feed/" + feedUrl; | |
50 chrome.tabs.create({url: "subscribe.html?" + feedUrl, | 92 chrome.tabs.create({url: "subscribe.html?" + feedUrl, |
51 windowId: windowId}); | 93 windowId: windowId}); |
52 } | 94 } |
53 } | 95 } |
54 }); | 96 }); |
55 }); | 97 }); |
56 }); | 98 }); |
57 </script> | 99 </script> |
| 100 </head> |
58 </html> | 101 </html> |
OLD | NEW |