-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
60 lines (50 loc) · 1.71 KB
/
popup.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
/**
* @author Christian Blach, Ulrik Moe
**/
const maxFeedLength = 19;
const maxTitleLength = 56; // characters
window.onload = function () {
updateFeed();
document.getElementById('timeRangeSelect').addEventListener('change', updateFeed);
};
function AJAX(url, payload)
{
const method = (payload) ? 'POST' : 'GET';
const xhr = new XMLHttpRequest();
xhr.responseType = 'document';
xhr.onload = buildFeed;
xhr.onerror = function (e) {};
xhr.open(method, url, true);
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.send(payload);
}
function zeroPad(num) {
return ('0' + num).slice(-2);
}
// Takes the toplist response from amino and parses each thread title and link:
function buildFeed() {
const feed = document.getElementById('feed');
const frag = document.createDocumentFragment();
const html = this.response;
for (var i = 1; i < maxFeedLength; i++) {
const e = html.getElementById('ctl00_ctl00_bcr_bcr_TopListThreadsRepeater_ctl' + zeroPad(i) + '_HyperLink2');
if (!e) { continue; }
let text = e.text.substring(0, maxTitleLength);
if (e.text.length > 53) { text += '...'; }
const li = document.createElement('li');
const a = document.createElement('a');
a.href = e.href;
a.textContent = text;
a.title = e.text;
a.classList.add('story');
li.appendChild(a);
frag.appendChild(li);
}
feed.appendChild(frag);
}
function updateFeed(evt) {
const time = (evt) ? evt.target.value : 'day';
const payload = postObj[time];
AJAX('https://www.amino.dk/forums/Toplister.aspx', payload);
document.getElementById('feed').innerHTML = '';
}