-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathscript.js
98 lines (84 loc) · 2.94 KB
/
script.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
// Add tooltip element on page
// Tooltip will be showed when user (or bot) move mouse under element with dmtooltip attribute
function initToolTip()
{
let body = document.querySelector('body');
if (!body) {
return;
}
let tooltip = document.querySelector('#tooltip');
body.removeEventListener('mousemove', this.updateToolTip);
body.addEventListener('mousemove', this.updateToolTip);
}
// Internal function to actually show or hide tooltip
// Must not be used at user code
function updateToolTip(event)
{
let tooltip = document.querySelector('#tooltip');
if (!tooltip) {
return;
}
let el = event ? event.target : null;
let text = '';
if (el) {
text = el.getAttribute('tooltip');
text = text ? text : ''; // ensure String
text = text.replace(/(?:\r\n|\r|\n)/g, '<br>');
text = text.replace(/ /g, ' ');
}
if (text) {
tooltip.innerHTML = '<p>' + text + '</p>';
}
else {
tooltip.innerHTML = '';
}
if (tooltip.innerHTML) {
tooltip.style.display = 'block';
let rect = el.tagName == 'IMG' ? el.parentElement.getBoundingClientRect() : el.getBoundingClientRect();
let emSize = parseFloat(getComputedStyle(el.parentElement).fontSize);
tooltip.style.top = (rect.bottom + emSize / 3) + 'px';
tooltip.style.left = (rect.left + rect.width / 2 - tooltip.getBoundingClientRect().width / 2) + 'px';
}
else {
tooltip.style.display = 'none';
}
}
function httpGet(theUrl)
{
var xmlHttp = new XMLHttpRequest();
xmlHttp.open("GET", theUrl, false); // false for synchronous request
xmlHttp.send(null);
return xmlHttp.responseText;
}
function onloadfunc()
{
initToolTip();
let map = JSON.parse(httpGet('https://api.github.com/repos/torrent-file-editor/torrent-file-editor/releases/latest'));
if (map) {
let assets = map.assets;
for (let i = 0; i < assets.length; ++i) {
let asset = assets[i];
let os;
if (asset.name.endsWith('-x32.exe')) {
os = 'win32';
}
else if (asset.name.endsWith('-x64.exe')) {
os = 'win64';
}
else if (asset.name.endsWith('.dmg')) {
os = 'macos';
}
else if (asset.name.endsWith('.tar.gz')) {
os = 'linux';
}
if (os) {
let btnEl = document.querySelector('header a.btn[os="' + os + '"]');
btnEl.setAttribute('href', asset.browser_download_url);
let tooltip = 'Size ' + Number((asset.size / (1024 * 1024))).toFixed(2) + ' MiB<br>Download count ' + asset.download_count;
btnEl.setAttribute('tooltip', tooltip);
let imgEl = document.querySelector('header a.btn[os="' + os + '"] img');
imgEl.setAttribute('tooltip', tooltip);
}
}
}
}