Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(201)

Side by Side Diff: chrome/test/data/extensions/samples/subscribe_page_action/background.html

Issue 155514: Implement extension specific events (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 11 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 <html> 1 <html>
2 <head> 2 <head>
3 <script> 3 <script>
4 // The Page Action ID. 4 // The Page Action ID.
5 var pageActionId = "RssPageAction"; 5 var pageActionId = "RssPageAction";
6 6
7 // The icons to use. These correspond to the icons listed in the manifest. 7 // The icon to use. This corresponds to the icon listed in the manifest.
8 var subscribeId = 0; 8 var subscribeId = 0;
9 var alreadySubscribedId = 1;
10 9
11 // The window this Page Action is associated with. 10 // A dictionary keyed off of tabId that keeps track of data per tab (for
12 var windowId = -1; 11 // example what feedUrl was detected in the tab).
13 12 var feedData = {};
14 // The TabId this Page Action is associated with.
15 var tabId = -1;
16
17 // The URL of the page that contains the feed.
18 var pageUrl = "";
19
20 // The feed URL found on the page.
21 var feedUrl = "";
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 13
49 chrome.self.onConnect.addListener(function(port) { 14 chrome.self.onConnect.addListener(function(port) {
50 windowId = port.tab.windowId;
51 tabId = port.tab.id;
52 pageUrl = port.tab.url;
53
54 // This will get called from the content script using PostMessage. 15 // This will get called from the content script using PostMessage.
55 // |feedUrls| is a list of URL feeds found on the page. We only need 1 to 16 // |feedUrls| is a list of URL feeds found on the page. We only need 1 to
56 // enable the PageAction icon in the Omnibox. 17 // enable the PageAction icon in the Omnibox.
57 port.onMessage.addListener(function(feedUrls) { 18 port.onMessage.addListener(function(feedUrls) {
58 feedUrl = feedUrls[0]; 19 feedUrl = feedUrls[0];
59 // Let Chrome know that the PageAction needs to be enabled for this tabId 20 // Let Chrome know that the PageAction needs to be enabled for this tabId
60 // and for the url of this page. 21 // and for the url of this page.
61 if (feedUrl) { 22 if (feedUrl) {
62 EnableIcon(false); // Not subscribed (as far as we know, might change). 23 feedData[port.tab.id] = {pageUrl: port.tab.url,
24 feedUrl: feedUrl};
63 25
64 // But also check the server to see if we have already subscribed so 26 chrome.pageActions.enableForTab(
65 // that we can update the status. 27 pageActionId, {tabId: port.tab.id,
66 feedUrl = encodeURIComponent(feedUrl); 28 url: port.tab.url,
67 req = new XMLHttpRequest(); 29 title: "Click to subscribe...",
68 req.onload = handleResponse; 30 iconId: subscribeId});
69 req.open("GET", subscribedUrl + feedUrl, false);
70 req.send(null);
71 } 31 }
72 }); 32 });
73 }); 33 });
74 34
75 function handleResponse() {
76 if (req.responseText == "true")
77 EnableIcon(true); // true == Already suscribed.
78 }
79
80 // Chrome will call into us when the user clicks on the icon in the OmniBox. 35 // Chrome will call into us when the user clicks on the icon in the OmniBox.
81 chrome.pageActions.onExecute.addListener(function(reply) { 36 chrome.pageActions["RssPageAction"].addListener(function(reply) {
82 chrome.windows.getCurrent(function(window) { 37 chrome.windows.getCurrent(function(window) {
83 chrome.tabs.get(reply.data.tabId, function(tab) { 38 chrome.tabs.get(reply.data.tabId, function(tab) {
84 if (!alreadySubscribed && window.focused) { 39 // 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 40 // 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 41 // action multiple times.
87 // action multiple times. 42 if (window.focused) {
88 if (reply.pageActionId == pageActionId && 43 // Create a new tab showing the subscription page with the right
89 reply.data.tabUrl == pageUrl) { 44 // feed URL.
90 // Create a new tab showing the subscription page with the right 45 chrome.tabs.create({url: "subscribe.html?" +
91 // feed URL. 46 feedData[reply.data.tabId].feedUrl,
92 chrome.tabs.create({url: "subscribe.html?" + feedUrl, 47 windowId: window.windowId});
93 windowId: windowId});
94 } else {
95 console.log("Ignoring execute event.");
96 console.log("PageActionId: " + reply.pageActionId + " == " +
97 pageActionId);
98 console.log("TabUrl : " + reply.data.tabUrl + " == " +
99 pageUrl);
100 }
101 } 48 }
102 }); 49 });
103 }); 50 });
104 }); 51 });
52
53 chrome.tabs.onRemoved.addListener(function(reply) {
54 feedData[reply.tabId] = null;
55 });
105 </script> 56 </script>
106 </head> 57 </head>
107 </html> 58 </html>
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698