1 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // First check to see if this document is a feed. If so, it will redirect.
6 // Otherwise, check if it has embedded feed links, such as:
7 // (<link rel="alternate" type="application/rss+xml" etc). If so, show the
10 debugMsg(logLevels
.info
, "Running feed finder script");
12 if (!isFeedDocument()) {
13 debugMsg(logLevels
.info
, "Document is not a feed, check for <link> tags.");
17 // See if the document contains a <link> tag within the <head> and
18 // whether that points to an RSS feed.
19 function findFeedLinks() {
20 // Find all the RSS link elements.
21 var result
= document
.evaluate(
22 '//*[local-name()="link"][contains(@rel, "alternate")] ' +
23 '[contains(@type, "rss") or contains(@type, "atom") or ' +
24 'contains(@type, "rdf")]', document
, null, 0, null);
29 while (item
= result
.iterateNext()) {
30 feeds
.push({"href": item
.href
, "title": item
.title
});
35 // Notify the extension needs to show the RSS page action icon.
36 chrome
.extension
.sendMessage({msg
: "feedIcon", feeds
: feeds
});
40 // Check to see if the current document is a feed delivered as plain text,
41 // which Chrome does for some mime types, or a feed wrapped in an html.
42 function isFeedDocument() {
43 var body
= document
.body
;
45 debugMsg(logLevels
.info
, "Checking if document is feed");
47 var soleTagInBody
= "";
48 if (body
&& body
.childElementCount
== 1) {
49 soleTagInBody
= body
.children
[0].tagName
;
50 debugMsg(logLevels
.info
, "The sole tag in the body is: " + soleTagInBody
);
53 // Some feeds show up as feed tags within the BODY tag, for example some
54 // ComputerWorld feeds. We cannot check for this at document_start since
55 // the body tag hasn't been defined at that time (contains only HTML element
57 if (soleTagInBody
== "RSS" || soleTagInBody
== "FEED" ||
58 soleTagInBody
== "RDF") {
59 debugMsg(logLevels
.info
, "Found feed: Tag is: " + soleTagInBody
);
60 chrome
.extension
.sendMessage({msg
: "feedDocument", href
: location
.href
});
64 // Chrome renders some content types like application/rss+xml and
65 // application/atom+xml as text/plain, resulting in a body tag with one
66 // PRE child containing the XML. So, we attempt to parse it as XML and look
67 // for RSS tags within.
68 if (soleTagInBody
== "PRE") {
69 debugMsg(logLevels
.info
, "Found feed: Wrapped in PRE");
70 var domParser
= new DOMParser();
71 var doc
= domParser
.parseFromString(body
.textContent
, "text/xml");
73 if (currentLogLevel
>= logLevels
.error
) {
74 var error
= doc
.getElementsByTagName("parsererror");
76 debugMsg(logLevels
.error
, 'error: ' + doc
.childNodes
[0].outerHTML
);
79 // |doc| now contains the parsed document within the PRE tag.
80 if (containsFeed(doc
)) {
81 // Let the extension know that we should show the subscribe page.
82 chrome
.extension
.sendMessage({msg
: "feedDocument", href
: location
.href
});
87 debugMsg(logLevels
.info
, "Exiting: feed is not a feed document");