diff options
author | finnur@chromium.org <finnur@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-07-09 21:30:53 +0000 |
---|---|---|
committer | finnur@chromium.org <finnur@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-07-09 21:30:53 +0000 |
commit | 361b28a80cfd2a9a0db454c7ebdb9c95ea55840c (patch) | |
tree | a6cf253e64ded952e1a5518f8d25ac8848c6200b /chrome/test | |
parent | 76b118a0f798a2e88103492173225e57b2062a37 (diff) | |
download | chromium_src-361b28a80cfd2a9a0db454c7ebdb9c95ea55840c.zip chromium_src-361b28a80cfd2a9a0db454c7ebdb9c95ea55840c.tar.gz chromium_src-361b28a80cfd2a9a0db454c7ebdb9c95ea55840c.tar.bz2 |
Add a rudamentary feed preview to the RSS extension. It
doesn't handle inline HTML in the item description (it just
dumps it as text) and the feed needs to be valid XML for it
to show any preview, but it is better than nothing. We can
easily change it to display the HTML but we want to (at some
point) try to use a separate origin so that we can render
the HTML code from untrusted sources safely.
Also fix a bug in the image tracker. It should not try
to communicate with the view if the view has gone away
(which was the whole point of the image tracker...)
BUG=None
TEST=Install the extension, browse to a page with a feed
and click onthe rss icon in the Omnibox. An interstitial
page should appear with a preview of the feed.
Review URL: http://codereview.chromium.org/155180
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@20316 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/test')
-rw-r--r-- | chrome/test/data/extensions/samples/subscribe_page_action/background.html | 6 | ||||
-rw-r--r-- | chrome/test/data/extensions/samples/subscribe_page_action/manifest.json | 4 | ||||
-rw-r--r-- | chrome/test/data/extensions/samples/subscribe_page_action/subscribe.html | 230 | ||||
-rw-r--r-- | chrome/test/data/feeds/feed.html | 8 | ||||
-rw-r--r-- | chrome/test/data/feeds/feed1.xml | 10 | ||||
-rw-r--r-- | chrome/test/data/feeds/feed2.xml | 9 | ||||
-rw-r--r-- | chrome/test/data/feeds/feed_invalid1.xml | 0 | ||||
-rw-r--r-- | chrome/test/data/feeds/feed_invalid2.xml | 1 | ||||
-rw-r--r-- | chrome/test/data/feeds/no_feed.html | 7 | ||||
-rw-r--r-- | chrome/test/test_location_bar.h | 3 |
10 files changed, 265 insertions, 13 deletions
diff --git a/chrome/test/data/extensions/samples/subscribe_page_action/background.html b/chrome/test/data/extensions/samples/subscribe_page_action/background.html index 0eeca7e..94761ff 100644 --- a/chrome/test/data/extensions/samples/subscribe_page_action/background.html +++ b/chrome/test/data/extensions/samples/subscribe_page_action/background.html @@ -91,6 +91,12 @@ // 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); } } }); diff --git a/chrome/test/data/extensions/samples/subscribe_page_action/manifest.json b/chrome/test/data/extensions/samples/subscribe_page_action/manifest.json index 6a8f225..5807816 100644 --- a/chrome/test/data/extensions/samples/subscribe_page_action/manifest.json +++ b/chrome/test/data/extensions/samples/subscribe_page_action/manifest.json @@ -1,14 +1,14 @@ { "name": "RSS Subscription Extension", "description": "Adds one-click subscription to your toolbar", - "version": "1.0", + "version": "1.1", "permissions": [ "http://*/*" ], "background_page": "background.html", "content_scripts": [ { - "matches": ["http://*/*"], + "matches": ["http://*/*", "file://*.*"], "js": ["feed_finder.js"] } ], diff --git a/chrome/test/data/extensions/samples/subscribe_page_action/subscribe.html b/chrome/test/data/extensions/samples/subscribe_page_action/subscribe.html index 011b6c9..54860bb 100644 --- a/chrome/test/data/extensions/samples/subscribe_page_action/subscribe.html +++ b/chrome/test/data/extensions/samples/subscribe_page_action/subscribe.html @@ -3,22 +3,230 @@ <title>Subscribe to feed</title> <script type="text/javascript"> + // Grab the URL from the querystring, removing question mark at the front. + var feedUrl = document.location.search.substring(1); + + // The XMLHttpRequest object that tries to load and parse the feed. + var req; + + // What to show when we cannot parse the feed name. + var unknownName = "Unknown feed name"; + + // The maximum number of feed items to show in the preview. + var maxFeedItems = 10; + + // The maximum number of characters to show in the feed item title. + var maxTitleCount = 64; + + // The maximum number of characters to show for the feed item description. + var maxDescCount = 1024; + + // Navigates to the reader of the user's choice (for subscribing to the feed). function navigate() { - var select = document.getElementById("readers"); - // Grab the URL from the querystring, removing question mark at the front. - var feed_url = document.location.search.substring(1); - var url = select.options[select.selectedIndex].value + feed_url; + var select = document.getElementById('readers'); + var url = select.options[select.selectedIndex].value + feedUrl; document.location = url; } + + // Parses the feed specified. We will either end up in handleResponse or + // handleError from here. + function loadFeed() { + feedUrl = decodeURIComponent(feedUrl); + req = new XMLHttpRequest(); + req.onload = handleResponse; + req.onerror = handleError; + req.open("GET", feedUrl, false); + req.send(null); + } + + // Sets the title for the feed. + function setFeedTitle(title) { + var titleTag = document.getElementById('title'); + titleTag.textContent = "Feed for '" + title + "'"; + } + + // Handles errors during the XMLHttpRequest. + function handleError() { + handleFeedParsingFailed("Error fetching feed"); + } + + // Handles feed parsing errors. + function handleFeedParsingFailed(error) { + setFeedTitle(unknownName); + + // First part of the error message. + var spanErrorPrefix = document.createElement('span'); + spanErrorPrefix.setAttribute('class', 'item_desc'); + spanErrorPrefix.appendChild( + document.createTextNode("Unable to show feed preview (")); + + // The actual error. + var spanError = document.createElement('span'); + spanError.id = 'error'; + spanError.setAttribute('class', 'item_desc'); + spanError.appendChild(document.createTextNode(error)); + + // Closing braces, etc. + var spanErrorSuffix = document.createElement('span'); + spanErrorSuffix.setAttribute('class', 'item_desc'); + spanErrorSuffix.appendChild( + document.createTextNode(").")); + + // Add the error message. + var itemsTag = document.getElementById('items'); + itemsTag.appendChild(spanErrorPrefix); + itemsTag.appendChild(spanError); + itemsTag.appendChild(spanErrorSuffix); + } + + // Handles parsing the feed data we got back from XMLHttpRequest. + function handleResponse() { + var itemsTag = document.getElementById('items'); + + // Uncomment these two lines to see what the feed data looks like. + // itemsTag.textContent = req.responseText; + // return; + + var doc = req.responseXML; + if (!doc) { + handleFeedParsingFailed("Not a valid feed"); + return; + } + + // Figure out what the title of the whole feed is. + var title = doc.getElementsByTagName('title')[0]; + if (title) + setFeedTitle(title.textContent); + else + setFeedTitle(unknownName); + + // Now parse the rest. Some use <entry> for each feed item, others use + // <channel><item>. + var entries = doc.getElementsByTagName('entry'); + if (entries.length == 0) + entries = doc.getElementsByTagName('item'); + + for (i = 0; i < entries.length && i < maxFeedItems; ++i) { + item = entries.item(i); + + // Grab the title for the feed item. + var itemTitle = item.getElementsByTagName('title')[0]; + if (itemTitle) + itemTitle = itemTitle.textContent; + else + itemTitle = "Unknown title"; + + // Ensure max length for title. + if (itemTitle.length > maxTitleCount) + itemTitle = itemTitle.substring(0, maxTitleCount) + "..."; + + // Grab the description. + var itemDesc = item.getElementsByTagName('description')[0]; + if (!itemDesc) + itemDesc = item.getElementsByTagName('summary')[0]; + + if (itemDesc) + itemDesc = itemDesc.textContent; + else + itemDesc = ""; + + // Ensure max length for description. + if (itemDesc.length > maxDescCount) + itemDesc = itemDesc.substring(0, maxDescCount) + "..."; + + // Grab the link URL. + var itemLink = item.getElementsByTagName('link'); + var link = itemLink[0].childNodes[0]; + if (link) + link = itemLink[0].childNodes[0].nodeValue; + else + link = itemLink[0].getAttribute('href'); + + // Create an anchor node for showing the title. + var anchorTitle = document.createElement('a'); + anchorTitle.id = 'anchor_' + String(i); + anchorTitle.setAttribute('class', 'item_title'); + anchorTitle.setAttribute('href', link); + anchorTitle.setAttribute('target', '_blank'); + anchorTitle.appendChild(document.createTextNode(itemTitle)); + + // Create a description node. + var spanDesc = document.createElement('span'); + spanDesc.id = 'desc_' + String(i); + spanDesc.setAttribute('class', 'item_desc'); + spanDesc.appendChild(document.createTextNode(itemDesc)); + + // Add this to the document. + itemsTag.appendChild(anchorTitle); + itemsTag.appendChild(document.createElement('br')); + itemsTag.appendChild(spanDesc); + itemsTag.appendChild(document.createElement('br')); + itemsTag.appendChild(document.createElement('br')); + } + } </script> + +<style> +body { + font-family: arial, sans-serif; + font-size: 11px; +} +a { + color: #2244D2; + font-weight:bold; + text-decoration: none; +} +a:hover { + font-weight:bold; + text-decoration: underline; +} + +.splitter { + padding: 2px 8px 2px 5px; + border-top: solid 1px #9CC2EF; + background: #EBEFF9; + font-size: 13px; + font-weight:bold; +} +.item_title { + font-size: 12px; +} +.item_desc { + font-size: 12px; +} +</style> </head> -<body> - <img src="feed-icon-64x64.png" align="absmiddle" /> - Subscribe to this feed using: - <select id="readers"> - <option value="http://www.google.com/reader/view/feed/">Google</option> - </select> - <button onclick="navigate();">Subscribe Now</button> +<body onload="loadFeed();"> + <table> + <tr> + <td width="75"> + <img src="feed-icon-64x64.png" alt="feed-icon" align="absmiddle" /> + </td> + <td> + <b><span id="title"></span></b><br> + <span>Subscribe to this feed using:</span> + <select id="readers"> + <option value="http://www.google.com/reader/view/feed/"> + Google Reader + </option> + <option value="http://www.google.com/ig/adde?moduleurl="> + iGoogle + </option> + <option value="http://www.bloglines.com/login?r=/sub/"> + Bloglines + </option> + <option value="http://add.my.yahoo.com/rss?url="> + My Yahoo + </option> + </select> + <button onclick="navigate();">Subscribe Now</button> + </td> + </tr> + </table> + + <div> </div> + <div class="splitter"><b>Feed preview</b></div><br> + <div id="items"></div> </body> </html> diff --git a/chrome/test/data/feeds/feed.html b/chrome/test/data/feeds/feed.html new file mode 100644 index 0000000..a776481 --- /dev/null +++ b/chrome/test/data/feeds/feed.html @@ -0,0 +1,8 @@ +<html> +<head> + <link rel="alternate" type="application/rss+xml" title="RSS" href="feed1.xml"/> +</head> +<body> + This is a page with a feed link. +</body> +</html> diff --git a/chrome/test/data/feeds/feed1.xml b/chrome/test/data/feeds/feed1.xml new file mode 100644 index 0000000..b480fdf --- /dev/null +++ b/chrome/test/data/feeds/feed1.xml @@ -0,0 +1,10 @@ +<rss version="2.0">
+<channel>
+ <title>MyFeedTitle</title>
+ <item>
+ <title>Title 1</title>
+ <link>http://www.google.com</link>
+ <description>Desc</description>
+ </item>
+</channel>
+</rss>
\ No newline at end of file diff --git a/chrome/test/data/feeds/feed2.xml b/chrome/test/data/feeds/feed2.xml new file mode 100644 index 0000000..aff3bf2 --- /dev/null +++ b/chrome/test/data/feeds/feed2.xml @@ -0,0 +1,9 @@ +<feed>
+ <title>MyFeed2</title>
+
+ <entry>
+ <title>My item title1</title>
+ <link rel="alternate" type="text/html" href="http://www.google.com" />
+ <summary>This is a summary.</summary>
+ </entry>
+</feed>
\ No newline at end of file diff --git a/chrome/test/data/feeds/feed_invalid1.xml b/chrome/test/data/feeds/feed_invalid1.xml new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/chrome/test/data/feeds/feed_invalid1.xml diff --git a/chrome/test/data/feeds/feed_invalid2.xml b/chrome/test/data/feeds/feed_invalid2.xml new file mode 100644 index 0000000..b24b52b --- /dev/null +++ b/chrome/test/data/feeds/feed_invalid2.xml @@ -0,0 +1 @@ +This is a garbage feed.
\ No newline at end of file diff --git a/chrome/test/data/feeds/no_feed.html b/chrome/test/data/feeds/no_feed.html new file mode 100644 index 0000000..9151a8c --- /dev/null +++ b/chrome/test/data/feeds/no_feed.html @@ -0,0 +1,7 @@ +<html> +<head> +</head> +<body> + This is a page with no feed link. +</body> +</html> diff --git a/chrome/test/test_location_bar.h b/chrome/test/test_location_bar.h index 95ce2c1..995d07a 100644 --- a/chrome/test/test_location_bar.h +++ b/chrome/test/test_location_bar.h @@ -43,6 +43,9 @@ class TestLocationBar : public LocationBar { virtual AutocompleteEditView* location_entry() { return NULL; } + virtual LocationBarTesting* GetLocationBarForTesting() { + return NULL; + } private: |