Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / chrome / test / data / extensions / subscribe_page_action / iframe.js
1 /* Copyright (c) 2009 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
6 /* Use only multi-line comments in this file, since during testing
7    its contents will get read from disk and stuffed into the
8    iframe .src tag, which is a process that doesn't preserve line
9    breaks and makes single-line comments comment out the rest of the code.
10 */
11
12 /* The maximum number of feed items to show in the preview. */
13 var maxFeedItems = 10;
14
15 /* The maximum number of characters to show in the feed item title. */
16 var maxTitleCount = 1024;
17
18 /* Find the token and target origin for this conversation from the HTML. The
19    token is used for secure communication, and is generated and stuffed into the
20    frame by subscribe.js.
21 */
22 var token = '';
23 var targetOrigin = '';
24 var html = document.documentElement.outerHTML;
25 var startTag = '<!--Token:';
26 var tokenStart = html.indexOf(startTag);
27 if (tokenStart > -1) {
28   tokenStart += startTag.length;
29   targetOrigin = html.substring(tokenStart, tokenStart+32);
30   tokenStart += 32;
31   var tokenEnd = html.indexOf('-->', tokenStart);
32   if (tokenEnd > tokenStart)
33     token = html.substring(tokenStart, tokenEnd);
34 }
35
36 if (token.length > 0) {
37   var mc = new MessageChannel();
38   window.parent.postMessage(
39       token,
40       'chrome-extension:/' + '/' + targetOrigin,
41       [mc.port2]);
42   mc.port1.onmessage = function(event) {
43     var parser = new DOMParser();
44     var doc = parser.parseFromString(event.data, "text/xml");
45     if (doc) {
46       buildPreview(doc);
47     } else {
48       /* Already handled in subscribe.html */
49     }
50   }
51 }
52
53 function buildPreview(doc) {
54   /* Start building the part we render inside an IFRAME. We use a table to
55      ensure that items are separated vertically from each other. */
56   var table = document.createElement("table");
57   var tbody = document.createElement("tbody");
58   table.appendChild(tbody);
59
60   /* Now parse the rest. Some use <entry> for each feed item, others use
61      <channel><item>. */
62   var entries = doc.getElementsByTagName('entry');
63   if (entries.length == 0)
64     entries = doc.getElementsByTagName('item');
65
66   for (i = 0; i < entries.length && i < maxFeedItems; ++i) {
67     item = entries.item(i);
68
69     /* Grab the title for the feed item. */
70     var itemTitle = item.getElementsByTagName('title')[0];
71     if (itemTitle)
72       itemTitle = itemTitle.textContent;
73     else
74       itemTitle = "Unknown title";
75
76     /* Ensure max length for title. */
77     if (itemTitle.length > maxTitleCount)
78       itemTitle = itemTitle.substring(0, maxTitleCount) + "...";
79
80     /* Grab the description.
81        TODO(aa): Do we need to check for type=html here? */
82     var itemDesc = item.getElementsByTagName('description')[0];
83     if (!itemDesc)
84       itemDesc = item.getElementsByTagName('summary')[0];
85     if (!itemDesc)
86       itemDesc = item.getElementsByTagName('content')[0];
87
88     if (itemDesc)
89       itemDesc = itemDesc.textContent;
90     else
91       itemDesc = "";
92
93     /* Grab the link URL. */
94     var itemLink = item.getElementsByTagName('link');
95     var link = "";
96     if (itemLink.length > 0) {
97       link = itemLink[0].childNodes[0];
98       if (link)
99         link = itemLink[0].childNodes[0].nodeValue;
100       else
101         link = itemLink[0].getAttribute('href');
102     }
103
104     var tr = document.createElement("tr");
105     var td = document.createElement("td");
106
107     /* If we found a link we'll create an anchor element,
108     otherwise just use a bold headline for the title. */
109     var anchor = (link != "") ? document.createElement("a") :
110                                 document.createElement("strong");
111     anchor.id = "anchor_" + String(i);
112     if (link != "")
113       anchor.href = link;
114     anchor.innerHTML = itemTitle;
115     anchor.target = "_top";
116     anchor.className = "item_title";
117
118     var span = document.createElement("span");
119     span.id = "desc_" + String(i);
120     span.className = "item_desc";
121     span.innerHTML = itemDesc;
122
123     td.appendChild(anchor);
124     td.appendChild(document.createElement("br"));
125     td.appendChild(span);
126     td.appendChild(document.createElement("br"));
127     td.appendChild(document.createElement("br"));
128
129     tr.appendChild(td);
130     tbody.appendChild(tr);
131   }
132
133   table.appendChild(tbody);
134   document.body.appendChild(table);
135 }