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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: chrome/test/data/extensions/samples/subscribe_page_action/background.html
===================================================================
--- chrome/test/data/extensions/samples/subscribe_page_action/background.html (revision 20629)
+++ chrome/test/data/extensions/samples/subscribe_page_action/background.html (working copy)
@@ -4,53 +4,14 @@
// The Page Action ID.
var pageActionId = "RssPageAction";
- // The icons to use. These correspond to the icons listed in the manifest.
+ // The icon to use. This corresponds to the icon listed in the manifest.
var subscribeId = 0;
- var alreadySubscribedId = 1;
- // The window this Page Action is associated with.
- var windowId = -1;
+ // A dictionary keyed off of tabId that keeps track of data per tab (for
+ // example what feedUrl was detected in the tab).
+ var feedData = {};
- // The TabId this Page Action is associated with.
- var tabId = -1;
-
- // The URL of the page that contains the feed.
- var pageUrl = "";
-
- // 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;
- pageUrl = port.tab.url;
-
// This will get called from the content script using PostMessage.
// |feedUrls| is a list of URL feeds found on the page. We only need 1 to
// enable the PageAction icon in the Omnibox.
@@ -59,49 +20,39 @@
// Let Chrome know that the PageAction needs to be enabled for this tabId
// and for the url of this page.
if (feedUrl) {
- EnableIcon(false); // Not subscribed (as far as we know, might change).
+ feedData[port.tab.id] = {pageUrl: port.tab.url,
+ feedUrl: feedUrl};
- // 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);
+ chrome.pageActions.enableForTab(
+ pageActionId, {tabId: port.tab.id,
+ url: port.tab.url,
+ title: "Click to subscribe...",
+ iconId: subscribeId});
}
});
});
- 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.pageActions["RssPageAction"].addListener(function(reply) {
chrome.windows.getCurrent(function(window) {
chrome.tabs.get(reply.data.tabId, function(tab) {
- 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.
- if (reply.pageActionId == pageActionId &&
- reply.data.tabUrl == pageUrl) {
- // Create a new tab showing the subscription page with the right
- // feed URL.
- chrome.tabs.create({url: "subscribe.html?" + feedUrl,
- windowId: windowId});
- } else {
- console.log("Ignoring execute event.");
- console.log("PageActionId: " + reply.pageActionId + " == " +
- pageActionId);
- console.log("TabUrl : " + reply.data.tabUrl + " == " +
- pageUrl);
- }
+ // 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.
+ if (window.focused) {
+ // Create a new tab showing the subscription page with the right
+ // feed URL.
+ chrome.tabs.create({url: "subscribe.html?" +
+ feedData[reply.data.tabId].feedUrl,
+ windowId: window.windowId});
}
});
});
});
+
+ chrome.tabs.onRemoved.addListener(function(reply) {
+ feedData[reply.tabId] = null;
+ });
</script>
</head>
</html>

Powered by Google App Engine
This is Rietveld 408576698