-
Notifications
You must be signed in to change notification settings - Fork 0
/
jzk.js
96 lines (83 loc) · 2.82 KB
/
jzk.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
const JZK_URL = 'http://jzk.cd.cz/everyone/jzk-osobni/index_cs.html'
function fetchJzk() {
function fetchAndLog(url) {
console.log("fetching "+url);
return fetch(url);
}
let homeUrl = JZK_URL;
return fetchAndLog(homeUrl)
.then(response => response.text())
.then(text => new DOMParser().parseFromString(text, "text/html"))
.then(doc => {
doc.head.insertBefore(doc.createElement("base"), doc.head.firstChild).setAttribute("href", homeUrl);
let latestUrl = "";
for (let link of doc.links)
if (link.href.endsWith('.txt'))
if (link.href > latestUrl)
latestUrl = link.href;
return fetchAndLog(latestUrl);
})
.then(response => response.text())
.then(text => text.split('\n').find(line => line.startsWith('EUR')))
.then(line => parseFloat(line.split('\t')[2]))
.then(jzk => browser.storage.local.set({jzkValue: jzk, jzkUpdated: Date.now()}))
.then(() => browser.storage.local.get(null))
.then(storage => console.log("jzk set to "+storage.jzkValue))
.catch(error => console.error(error));
}
async function getJzk() {
let storage = await browser.storage.local.get(null);
if (Date.now() > (storage.jzkUpdated ?? 0) + 24*3600*1000) {
await browser.storage.local.remove(['jzkValue','jzkUpdated']);
await fetchJzk();
storage = await browser.storage.local.get(null);
}
return storage.jzkValue ?? 0;
}
function convert(czk, jzk) {
return (parseInt(czk)/jzk).toFixed(1)+" \u20AC";
}
function queryAndProcess(selectors, callback) {
document.querySelectorAll(selectors).forEach(el => {
if (!el.classList.contains("processed")) {
el.classList.add("processed");
callback(el);
}
});
}
function process() {
getJzk().then(jzk => {
queryAndProcess(".rmenutop .rlinks", el => {
let jzkBox = el.cloneNode(true);
jzkBox.firstElementChild.innerText = "J\u017DK: 1 \u20AC = "+jzk+" K\u010D";
jzkBox.firstElementChild.href = JZK_URL;
el.after(jzkBox);
});
queryAndProcess(".buybut.green", el => {
let czk = el.childNodes[4];
czk.after(document.createElement("br"), document.createTextNode(convert(czk.textContent, jzk)));
});
queryAndProcess(".ticket .ptitle, .sh-ticketbox-modal .ptitle", el => {
let euro = el.cloneNode(true);
euro.innerText = convert(el.innerText, jzk);
el.after(euro);
});
queryAndProcess(".dropdown-toggle.is-btn.is-btn-white.is-btn-xs", el => {
el.removeAttribute('data-toggle');
el.onclick = e => el.nextElementSibling.firstElementChild.firstElementChild.click();
});
});
}
process();
new MutationObserver((mutations, observer) => {
let remain = observer.nextRun - performance.now();
clearTimeout(observer.delayedTimer);
let fun = () => {
process();
observer.nextRun = performance.now() + 100;
}
if (remain <= 0)
fun();
else
observer.delayedTimer = setTimeout(fun, remain);
}).observe(document, { subtree: true, childList: true });