-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
144 lines (104 loc) · 4.22 KB
/
script.js
File metadata and controls
144 lines (104 loc) · 4.22 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// main onload
$(document).ready(function() {
loadDoc();
});
// flattens an object (recursively!), similarly to Array#flatten
// e.g. flatten({ a: { b: { c: "hello!" } } }); // => "hello!"
function flatten(object) {
var check = _.isPlainObject(object) && _.size(object) === 1;
return check ? flatten(_.values(object)[0]) : object;
}
function parse(xml) {
var data = {};
var isText = xml.nodeType === 3,
isElement = xml.nodeType === 1,
body = xml.textContent && xml.textContent.trim(),
hasChildren = xml.children && xml.children.length,
hasAttributes = xml.attributes && xml.attributes.length;
// if it's text just return it
if (isText) { return xml.nodeValue.trim(); }
// if it doesn't have any children or attributes, just return the contents
if (!hasChildren && !hasAttributes) { return body; }
// if it doesn't have children but _does_ have body content, we'll use that
if (!hasChildren && body.length) { data.text = body; }
// if it's an element with attributes, add them to data.attributes
if (isElement && hasAttributes) {
data.attributes = _.reduce(xml.attributes, function(obj, name, id) {
var attr = xml.attributes.item(id);
obj[attr.name] = attr.value;
return obj;
}, {});
}
// recursively call #parse over children, adding results to data
_.each(xml.children, function(child) {
var name = child.nodeName;
// if we've not come across a child with this nodeType, add it as an object
// and return here
if (!_.has(data, name)) {
data[name] = parse(child);
return;
}
// if we've encountered a second instance of the same nodeType, make our
// representation of it an array
if (!_.isArray(data[name])) { data[name] = [data[name]]; }
// and finally, append the new child
data[name].push(parse(child));
});
// if we can, let's fold some attributes into the body
_.each(data.attributes, function(value, key) {
if (data[key] != null) { return; }
data[key] = value;
delete data.attributes[key];
});
// if data.attributes is now empty, get rid of it
if (_.isEmpty(data.attributes)) { delete data.attributes; }
// simplify to reduce number of final leaf nodes and return
return flatten(data);
}
function loadDoc() {
console.log("onclick");
//https://raw.githubusercontent.com/ZNClub/RSS-enabled-WebApp/master/rss.xml
//https://nevildsouza.wordpress.com/feed/
// rss.xml fails
$.get( "https://raw.githubusercontent.com/ZNClub/RSS-enabled-WebApp/master/rss.xml", function( data ) {
console.log("ajax success");
myFunction(data);
setTimeout(loadDoc,5000);
});
}
function myFunction(data) {
console.log(parse(data));
var data = new DOMParser().parseFromString(data, "text/xml");
var item = document.evaluate( '//rss//channel//item', data, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null );
var it=item.iterateNext();
count = 0;
var child=[];
while(it){
count+=1;
var start='<?xml version="1.0" encoding="UTF-8"?><item>';
var end='</item>';
var data = new DOMParser().parseFromString(start+it.innerHTML+end, "text/xml");
var ititle=document.evaluate('item//title',data,null,XPathResult.STRING_TYPE,null);
var ilink=document.evaluate('item//link',data,null,XPathResult.STRING_TYPE,null);
var ipub=document.evaluate('item//pubDate',data,null,XPathResult.STRING_TYPE,null);
console.log(ititle.stringValue);
console.log(ilink.stringValue);
console.log(ipub.stringValue);
// check no of elements in timeline $('.result')
var begin='<div class="line text-muted"></div>';
var date='<div class="separator text-muted"><time>'+ipub.stringValue+'</time></div>';
var start='<article class="panel panel-primary">';
var icon='<div class="panel-heading icon"><i class="glyphicon glyphicon-plus"></i></div>';
var head='<div class="panel-heading"><h2 class="panel-title">'+ititle.stringValue+'</h2></div>';
var body='<div class="panel-body"><a href="'+ilink.stringValue+'" target="_blank">Read Now</a></div>';
var end='</article>';
var html=begin+date+start+icon+head+body+end;
//$('.timeline').append(html);
child.push(html);
it=item.iterateNext();
}
$('.timeline').empty();
for(var i=0;i<child.length;i++){
$('.timeline').append(child[i]);
}
}