-
Notifications
You must be signed in to change notification settings - Fork 21
/
iframe.js
111 lines (92 loc) · 3.44 KB
/
iframe.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/* Copyright (c) 2009 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.
*/
/* Use only multi-line comments in this file, since during testing
its contents will get read from disk and stuffed into the
iframe .src tag, which is a process that doesn't preserve line
breaks and makes single-line comment out the rest of the code.
*/
/* 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 = 1024;
window.addEventListener("message", function(e) {
var parser = new DOMParser();
var doc = parser.parseFromString(e.data, "text/xml");
if (doc) {
buildPreview(doc);
} else {
/* Already handled in subscribe.html */
}
}, false);
function buildPreview(doc) {
/* Start building the part we render inside an IFRAME. We use a table to
ensure that items are separated vertically from each other. */
var table = document.createElement("table");
var tbody = document.createElement("tbody");
table.appendChild(tbody);
/* 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.
TODO(aa): Do we need to check for type=html here? */
var itemDesc = item.getElementsByTagName('description')[0];
if (!itemDesc)
itemDesc = item.getElementsByTagName('summary')[0];
if (!itemDesc)
itemDesc = item.getElementsByTagName('content')[0];
if (itemDesc)
itemDesc = itemDesc.textContent;
else
itemDesc = "";
/* Grab the link URL. */
var itemLink = item.getElementsByTagName('link');
var link = "";
if (itemLink.length > 0) {
link = itemLink[0].childNodes[0];
if (link)
link = itemLink[0].childNodes[0].nodeValue;
else
link = itemLink[0].getAttribute('href');
}
var tr = document.createElement("tr");
var td = document.createElement("td");
/* If we found a link we'll create an anchor element,
otherwise just use a bold headline for the title. */
var anchor = (link != "") ? document.createElement("a") :
document.createElement("strong");
anchor.id = "anchor_" + String(i);
if (link != "")
anchor.href = link;
anchor.innerHTML = itemTitle;
anchor.target = "_top";
anchor.className = "item_title";
var span = document.createElement("span");
span.id = "desc_" + String(i);
span.className = "item_desc";
span.innerHTML = itemDesc;
td.appendChild(anchor);
td.appendChild(document.createElement("br"));
td.appendChild(span);
td.appendChild(document.createElement("br"));
td.appendChild(document.createElement("br"));
tr.appendChild(td);
tbody.appendChild(tr);
}
table.appendChild(tbody);
document.body.appendChild(table);
}