- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / test / data / extensions / subscribe_page_action / feed_finder.js
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.
4
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
8 // page action icon.
9
10 debugMsg(logLevels.info, "Running feed finder script");
11
12 if (!isFeedDocument()) {
13   debugMsg(logLevels.info, "Document is not a feed, check for <link> tags.");
14   findFeedLinks();
15 }
16
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);
25
26   var feeds = [];
27   var item;
28   var count = 0;
29   while (item = result.iterateNext()) {
30     feeds.push({"href": item.href, "title": item.title});
31     ++count;
32   }
33
34   if (count > 0) {
35     // Notify the extension needs to show the RSS page action icon.
36     chrome.extension.sendMessage({msg: "feedIcon", feeds: feeds});
37   }
38 }
39
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;
44
45   debugMsg(logLevels.info, "Checking if document is feed");
46
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);
51   }
52
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
56   // with no children).
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});
61     return true;
62   }
63
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");
72
73     if (currentLogLevel >= logLevels.error) {
74       var error = doc.getElementsByTagName("parsererror");
75       if (error.length)
76         debugMsg(logLevels.error, 'error: ' + doc.childNodes[0].outerHTML);
77     }
78
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});
83       return true;
84     }
85   }
86
87   debugMsg(logLevels.info, "Exiting: feed is not a feed document");
88
89   return false;
90 }