-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.html
More file actions
244 lines (213 loc) · 7.96 KB
/
test.html
File metadata and controls
244 lines (213 loc) · 7.96 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
<!doctype html>
<html>
<head>
<title>Live News Feed</title>
<script>
(function () {
var _data = {
feed: '',
count: 1,
interval: 7,
prefs: widget.preferences,
items: [],
timer: null,
title: '',
}
// update feeds in the speed-dial
function _updateFeeds(i) {
clearTimeout(_data.timer);
var ind = (i || 0) % _data.count;
var next = ind + 1;
document.getElementById('title').textContent = _data.items[ind].title;
document.getElementById('counter').textContent = (ind + 1) + '/' + _data.count;
document.getElementById('desc').textContent = _data.items[ind].desc;
document.getElementById('date').textContent = '(' + _data.items[ind].date + ')';
var name = _data.prefs.getItem('nameCheckbox') === "true" ?
_data.title :
_data.prefs.getItem('nameInput');
opera.contexts.speeddial.url = _data.items[ind].href;
opera.contexts.speeddial.title = name;
_data.feed = _data.prefs.getItem('feedUrl');
// switch feeds every X seconds
_data.timer = setTimeout(function () {
_updateFeeds(next)
}, _data.interval * 1000);
}
function _clearData(message) {
opera.extension.title = "Live news feed";
document.getElementById('title').textContent = '';
document.getElementById('desc').textContent = '';
document.getElementById('date').textContent = '';
document.getElementById('counter').style.display = 'none';
var info = document.getElementById('prefs');
info.style.display = '';
if (message) info.textContent = message;
else info.textContent = 'Click here to set your preferences!';
opera.contexts.speeddial.url = 'options.html';
opera.contexts.speeddial.title = widget.name;
_data.items = [];
clearTimeout(_data.timer);
}
// process the data received from the XHR
function _processData(xhr) {
var xmlDoc;
if (!xhr.responseXML) {
var parser = new window.DOMParser()
try {
xmlDoc = parser.parseFromString(xhr.responseText, "text/xml");
} catch (error) {
return opera.postError('Error parsing feed.');
}
} else {
xmlDoc = xhr.responseXML;
}
var title = xmlDoc.querySelector('title');
_data.title = title.textContent || "No title";
var items = xmlDoc.querySelectorAll('item, entry');
var len = items.length;
if (_data.count > len) _data.count = len;
if (_data.count == 0 || len == 0) {
_clearData("No data!");
return;
}
for (var i = 0; i < _data.count; i++) {
_parseItem(items[i]);
}
document.getElementById('prefs').style.display = 'none';
document.getElementById('counter').style.display = '';
_updateFeeds();
}
// parse single DOM Element
function _parseItem(item) {
// sometimes descriptions contains tags like IMG
var desc = document.createElement('div');
var xmlDesc = item.querySelector('description, encoded, content');
var description = xmlDesc ? xmlDesc.textContent : '';
desc.innerHTML = unescapeText(description);
var date = item.querySelector('pubDate');
if (date) {
date = date.textContent.substr(5);
} else {
date = item.querySelector('date, updated');
date = date ? date.textContent : '';
}
_data.items.push({
title: unescapeText(item.getElementsByTagName('title')[0].textContent.replace(
/(<([^>]+)>)/ig, "")),
desc: desc.textContent,
date: date,
href: item.getElementsByTagName('link')[0].textContent
})
}
function unescapeText(text) {
text = text.replace(/\—/g, '—');
return text;
}
// get feeds from url
function _XHRFeed() {
clearTimeout(_updateTimer);
if (!_data.feed || _data.feed == '') {
_clearData();
return;
}
xhr = new XMLHttpRequest();
xhr.open('GET', _data.feed, true);
xhr.onreadystatechange = function () {
if (this.readyState !== 4) {
_clearData('...');
return false;
};
_processData(this);
_updateTimer = setTimeout(_XHRFeed, 1000 * 120);
}
xhr.send(null);
}
// storage handler for the options page
function _storageHandler() {
clearTimeout(_updateTimer);
_data.feed = _data.prefs.getItem('feedUrl');
_data.count = _data.prefs.getItem('count');
_data.interval = _data.prefs.getItem('interval');
_XHRFeed();
}
// force an update of the storage values
var _updateTimer = setTimeout(_storageHandler, 100);
// against fast changes on options page
addEventListener('storage',
function () {
_updateTimer = setTimeout(_storageHandler, 500);
}, false);
})();
</script>
<style>
body {
margin: 0px;
font-size: 12px;
padding: 1em;
background-color: #f2f2f2;
font-family: sans-serif;
}
p {
font-size: 1em;
line-height: 1.4em;
color: #222;
}
p#date {
font-size: .75em;
color: #777;
margin: 0;
}
h1 {
font-size: 1.5em;
font-weight: normal;
font-family: Georgia, serif;
color: #bf2020;
text-shadow: 0 1px 0 #fff;
}
#fadeout {
position: absolute;
bottom: 0;
width: 100%;
height: 30px;
background: -o-linear-gradient(rgba(242, 242, 242, 0), rgba(242, 242, 242, 1));
}
#prefs {
padding-top: 50px;
text-align: center;
}
#counter {
font-family: Georgia, serif;
color: #555;
position: absolute;
right: 10px;
top: 0;
border: 1px solid #c2c2c2;
border-top: none;
padding: 4px;
margin: 0;
background-color: #f9f9f9;
border-bottom-right-radius: 4px;
border-bottom-left-radius: 4px;
box-shadow: 1px 1px 0 #fff;
}
#inset {
position: absolute;
width: 100%;
height: 100%;
top: 0;
left: 0;
box-shadow: inset 0 2px 6px rgba(0, 0, 0, .3);
z-index: 999;
}
</style>
</head>
<body>
<div id="inset"></div>
<div id="fadeout"></div>
<p id="counter"></p>
<p id="date"></p>
<h1 id="title"></h1>
<p id="desc"></p>
<p id="prefs" style="display:none"></p>
</body>
</html>