diff options
author | finnur@chromium.org <finnur@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-01-22 23:03:47 +0000 |
---|---|---|
committer | finnur@chromium.org <finnur@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-01-22 23:03:47 +0000 |
commit | 1c1619682ce5dd4fbfc3caa8c76bdce0b13d09ca (patch) | |
tree | 3b35d1668ce087dc49e751082b43553f1c03c481 /chrome/test/data | |
parent | 2e6f365c30d478c2f57fe9f60a3f62c75884fe3c (diff) | |
download | chromium_src-1c1619682ce5dd4fbfc3caa8c76bdce0b13d09ca.zip chromium_src-1c1619682ce5dd4fbfc3caa8c76bdce0b13d09ca.tar.gz chromium_src-1c1619682ce5dd4fbfc3caa8c76bdce0b13d09ca.tar.bz2 |
Add feed sniffing to the RSS extension.
BUG=None
TEST=Go to http://www.whitehouse.gov/rss and click on any of the links to the feeds. It should take you to the subscription preview page in a new tab.
Review URL: http://codereview.chromium.org/543129
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@36918 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/test/data')
5 files changed, 108 insertions, 18 deletions
diff --git a/chrome/test/data/extensions/subscribe_page_action/background.html b/chrome/test/data/extensions/subscribe_page_action/background.html index c97d48f..28946b6 100644 --- a/chrome/test/data/extensions/subscribe_page_action/background.html +++ b/chrome/test/data/extensions/subscribe_page_action/background.html @@ -1,5 +1,5 @@ <!-- - * Copyright (c) 2009 The Chromium Authors. All rights reserved. Use of this + * Copyright (c) 2010 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. --> @@ -10,18 +10,31 @@ // example what feedUrl was detected in the tab). var feedData = {}; - chrome.extension.onRequest.addListener( - function(request, sender) { - if (request.msg == "feedIcon") { - // We have received a list of feed urls found on the page. - // Enable the page action icon. - feedData[sender.tab.id] = request.feeds; - chrome.pageAction.setTitle({ tabId: sender.tab.id, - title: "Click to subscribe..." - }); - chrome.pageAction.show(sender.tab.id); - } - }); + chrome.extension.onRequest.addListener(function(request, sender) { + if (request.msg == "feedIcon") { + // We have received a list of feed urls found on the page. + // Enable the page action icon. + feedData[sender.tab.id] = request.feeds; + chrome.pageAction.setTitle({ tabId: sender.tab.id, + title: "Click to subscribe..."}); + chrome.pageAction.show(sender.tab.id); + } else if (request.msg == "feedDocument") { + // We received word from the content script that this document + // is an RSS feed (not just a document linking to the feed). + // So, we go straight to the subscribe page in a new tab and + // navigate back on the current page (to get out of the xml page). + // We don't want to navigate in-place because trying to go back + // from the subscribe page takes us back to the xml page, which + // will redirect to the subscribe page again (we don't support a + // location.replace equivalant in the Tab navigation system). + chrome.tabs.executeScript(sender.tab.id, + {code: "if (history.length > 1) " + + "history.go(-1); else window.close();"}); + var url = "subscribe.html?" + encodeURIComponent(request.href); + url = chrome.extension.getURL(url); + chrome.tabs.create({url: url, index: sender.tab.index}); + } + }); chrome.tabs.onRemoved.addListener(function(tabId) { delete feedData[tabId]; diff --git a/chrome/test/data/extensions/subscribe_page_action/doc_start.js b/chrome/test/data/extensions/subscribe_page_action/doc_start.js new file mode 100644 index 0000000..37cbec9 --- /dev/null +++ b/chrome/test/data/extensions/subscribe_page_action/doc_start.js @@ -0,0 +1,8 @@ +// Copyright (c) 2010 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. + +// See if the current document is a feed document and if so, let +// the extension know that we should show the subscribe page instead. +if (containsFeed(document)) + chrome.extension.sendRequest({msg: "feedDocument", href: location.href}); diff --git a/chrome/test/data/extensions/subscribe_page_action/feed_finder.js b/chrome/test/data/extensions/subscribe_page_action/feed_finder.js index af265c1..d433c83 100644 --- a/chrome/test/data/extensions/subscribe_page_action/feed_finder.js +++ b/chrome/test/data/extensions/subscribe_page_action/feed_finder.js @@ -2,9 +2,17 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -findFeeds(); +// First check to see if this document is a feed. If so, it will redirect. +// Otherwise, check if it has embedded feed links, such as: +// (<link rel="alternate" type="application/rss+xml" etc). If so, show the +// page action icon. -function findFeeds() { +if (!isPlainTextFeedDocument()) + findFeedLinks(); + +// See if the document contains a <link> tag within the <head> and +// whether that points to an RSS feed. +function findFeedLinks() { // Find all the RSS link elements. var result = document.evaluate( '//*[local-name()="link"][@rel="alternate"][contains(@type, "rss") or ' + @@ -20,7 +28,37 @@ function findFeeds() { } if (count > 0) { - // Notify the extension of the feed URLs we found. + // Notify the extension needs to show the RSS page action icon. chrome.extension.sendRequest({msg: "feedIcon", feeds: feeds}); } } + +// Check to see if the current document is a feed delivered as plain text, +// which Chrome does for some mime types. +function isPlainTextFeedDocument() { + var body = document.body; + + // Chrome renders some content types like application/rss+xml and + // application/atom+xml as text/plain, resulting in a body tag with one + // PRE child containing the XML. So, we attempt to parse it as XML and look + // for RSS tags within. + if (body && body.childElementCount == 1 && + body.children[0].tagName == "PRE") { + var domParser = new DOMParser(); + var doc = domParser.parseFromString(body.textContent, "text/xml"); + + // Uncomment these three lines to see the parsing error. + // var error = docWithin.getElementsByTagName("parsererror"); + // if (error.length) + // console.log('error: ' + doc.childNodes[0].outerHTML); + + // |doc| now contains the parsed document within the PRE tag. + if (containsFeed(doc)) { + // Let the extension know that we should show the subscribe page. + chrome.extension.sendRequest({msg: "feedDocument", href: location.href}); + return true; + } + } + + return false; +} diff --git a/chrome/test/data/extensions/subscribe_page_action/manifest.json b/chrome/test/data/extensions/subscribe_page_action/manifest.json index be13375..a0fa67a 100644 --- a/chrome/test/data/extensions/subscribe_page_action/manifest.json +++ b/chrome/test/data/extensions/subscribe_page_action/manifest.json @@ -1,7 +1,7 @@ { "name": "RSS Subscription Extension (by Google)", "description": "Adds one-click subscription to your toolbar.", - "version": "1.9.1", + "version": "2.0", "permissions": [ "tabs", "http://*/*", @@ -12,7 +12,12 @@ "content_scripts": [ { "matches": ["http://*/*", "https://*/*"], - "js": ["feed_finder.js"] + "js": ["sniff_common.js", "doc_start.js"], + "run_at": "document_start" + }, + { + "matches": ["http://*/*", "https://*/*"], + "js": ["sniff_common.js", "feed_finder.js"] } ], "icons": { "128": "feed-icon-128x128.png" }, diff --git a/chrome/test/data/extensions/subscribe_page_action/sniff_common.js b/chrome/test/data/extensions/subscribe_page_action/sniff_common.js new file mode 100644 index 0000000..b16dae6 --- /dev/null +++ b/chrome/test/data/extensions/subscribe_page_action/sniff_common.js @@ -0,0 +1,26 @@ +// Copyright (c) 2010 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. + +function containsFeed(doc) { + // Find all the RSS link elements. + var result = doc.evaluate( + '//*[local-name()="rss" or local-name()="feed" or local-name()="RDF"]', + doc, null, 0, null); + + if (!result) + return false; // This is probably overly defensive, but whatever. + + var node = result.iterateNext(); + + if (!node) + return false; // No RSS tags were found. + + // The feed for arab dash jokes dot net, for example, contains + // a feed that is a child of the body tag so we continue only if the + // node contains no parent or if the parent is the body tag. + if (node.parentElement && node.parentElement.tagName != "BODY") + return false; + + return true; +} |