-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathcore.js
210 lines (187 loc) · 5 KB
/
core.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
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
var maxFeedItems = 15;
var req;
var buildPopupAfterResponse = false;
var OnFeedSuccess = null;
var OnFeedFail = null;
var retryMilliseconds = 120000;
function SetInitialOption(key, value) {
if (localStorage[key] == null) {
localStorage[key] = value;
}
}
function UpdateIfReady(force) {
var lastRefresh = parseFloat(localStorage["HN.LastRefresh"]);
var interval = parseFloat(localStorage["HN.RequestInterval"]);
var nextRefresh = lastRefresh + interval;
var curTime = parseFloat((new Date()).getTime());
var isReady = (curTime > nextRefresh);
var isNull = (localStorage["HN.LastRefresh"] == null);
if ((force == true) || (localStorage["HN.LastRefresh"] == null)) {
UpdateFeed();
}
else {
if (isReady) {
UpdateFeed();
}
}
}
function UpdateFeed() {
var xhr = new XMLHttpRequest();
xhr.open('GET', 'https://news.ycombinator.com/rss');
xhr.onload = function() {
if (xhr.status === 200) {
onRssSuccess(xhr.responseText);
}
else {
onRssError();
}
};
xhr.send();
}
function onRssSuccess(doc) {
console.log('doc', doc);
if (!doc) {
handleFeedParsingFailed("Not a valid feed.");
return;
}
links = parseHNLinks(doc);
SaveLinksToLocalStorage(links);
if (buildPopupAfterResponse == true) {
buildPopup(links);
buildPopupAfterResponse = false;
}
localStorage["HN.LastRefresh"] = (new Date()).getTime();
}
function updateLastRefreshTime() {
localStorage["HN.LastRefresh"] = (new Date()).getTime();
}
function onRssError(xhr, type, error) {
handleFeedParsingFailed('Failed to fetch RSS feed.');
}
function handleFeedParsingFailed(error) {
//var feed = document.getElementById("feed");
//feed.className = "error"
//feed.innerText = "Error: " + error;
localStorage["HN.LastRefresh"] = localStorage["HN.LastRefresh"] + retryMilliseconds;
}
function parseXml(xml) {
var xmlDoc;
try {
xmlDoc = new ActiveXObject('Microsoft.XMLDOM');
xmlDoc.async = false;
xmlDoc.loadXML(xml);
}
catch (e) {
xmlDoc = (new DOMParser).parseFromString(xml, 'text/xml');
}
return xmlDoc;
}
function parseHNLinks(rawXmlStr) {
var parser = new DOMParser();
var doc = parser.parseFromString(rawXmlStr, "text/xml");
var entries = doc.getElementsByTagName('entry');
if (entries.length == 0) {
entries = doc.getElementsByTagName('item');
}
var count = Math.min(entries.length, maxFeedItems);
var links = new Array();
for (var i=0; i< count; i++) {
item = entries.item(i);
var hnLink = new Object();
//Grab the title
var itemTitle = item.getElementsByTagName('title')[0];
if (itemTitle) {
hnLink.Title = itemTitle.textContent;
} else {
hnLink.Title = "Unknown Title";
}
//Grab the Link
var itemLink = item.getElementsByTagName('link')[0];
if (!itemLink) {
itemLink = item.getElementsByTagName('comments')[0];
}
if (itemLink) {
hnLink.Link = itemLink.textContent;
} else {
hnLink.Link = '';
}
//Grab the comments link
var commentsLink = item.getElementsByTagName('comments')[0];
if (commentsLink) {
hnLink.CommentsLink = commentsLink.textContent;
} else {
hnLink.CommentsLink = '';
}
links.push(hnLink);
}
return links;
}
function SaveLinksToLocalStorage(links) {
localStorage["HN.NumLinks"] = links.length;
for (var i=0; i<links.length; i++) {
localStorage["HN.Link" + i] = JSON.stringify(links[i]);
}
}
function RetrieveLinksFromLocalStorage() {
var numLinks = localStorage["HN.NumLinks"];
if (numLinks == null) {
return null;
}
else {
var links = new Array();
for (var i=0; i<numLinks; i++) {
links.push(JSON.parse(localStorage["HN.Link" + i]))
}
return links;
}
}
function openOptions() {
var optionsUrl = chrome.extension.getURL('options.html');
chrome.tabs.create({url: optionsUrl});
}
function openLink(e) {
e.preventDefault();
openUrl(this.href, (localStorage['HN.BackgroundTabs'] == 'false'));
}
function openLinkFront(e) {
e.preventDefault();
openUrl(this.href, true);
}
function printTime(d) {
var hour = d.getHours();
var minute = d.getMinutes();
var ap = "AM";
if (hour > 11) { ap = "PM"; }
if (hour > 12) { hour = hour - 12; }
if (hour == 0) { hour = 12; }
if (minute < 10) { minute = "0" + minute; }
var timeString = hour +
':' +
minute +
" " +
ap;
return timeString;
}
// Show |url| in a new tab.
function openUrl(url, take_focus) {
// Only allow http and https URLs.
if (url.indexOf("http:") != 0 && url.indexOf("https:") != 0) {
return;
}
chrome.tabs.create({url: url, selected: take_focus});
}
function hideElement(id) {
var e = document.getElementById(id);
e.style.display = 'none';
}
function showElement(id) {
var e = document.getElementById(id);
e.style.display = 'block';
}
function toggle(id) {
var e = document.getElementById(id);
if(e.style.display == 'block')
e.style.display = 'none';
else
e.style.display = 'block';
}