forked from daniel-j/send2ereader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdownload.html
102 lines (93 loc) · 2.21 KB
/
download.html
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
<!doctype html>
<html>
<head>
<title>Send to Kobo/Kindle</title>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<link rel="stylesheet" type="text/css" href="/style.css"/>
</head>
<body>
<div class="wrapper">
<h1 class="center">Send to Kobo/Kindle</h1>
<div class="center">
<div style="font-size: 1.4em;">Unique key:</div>
<div id="key">––––</div>
<br/>
<button id="keygen">Generate new key</button>
</div>
<br/>
<hr/>
<br/>
<div class="center" id="downloads">
<a id="downloadlink"></a>
</div>
</div>
<script type="text/javascript">
var keyOutput = document.getElementById('key')
var keyGenBtn = document.getElementById('keygen')
var downloads = document.getElementById('downloads')
var downloadlink = document.getElementById('downloadlink')
var key = null
var pollTimer = null
function xhr(method, url, cb) {
var x = new XMLHttpRequest()
x.onload = function () {
cb(x)
}
x.onerror = function () {
cb(x)
}
x.open(method, url, true)
x.send(null)
}
function pollFile () {
xhr('GET', '/status/' + key, function (x) {
var data
try {
data = JSON.parse(x.responseText)
} catch (err) {
keyOutput.textContent = '––––'
key = null
downloads.style.display = 'none'
return
}
if (data.error) {
if (pollTimer) clearInterval(pollTimer)
key = null
keyOutput.textContent = '––––'
downloads.style.display = 'none'
}
if (data.file) {
downloadlink.textContent = data.file.name
downloads.style.display = 'block'
} else {
downloads.style.display = 'none'
}
})
}
function generateKey () {
keyOutput.textContent = '––––'
if (pollTimer) clearInterval(pollTimer)
downloads.style.display = 'none'
xhr('POST', '/generate', function (x) {
if (x.responseText !== 'error' && x.status === 200) {
key = x.responseText
keyOutput.textContent = key
downloadlink.href = '/download/' + key
if (pollTimer) clearInterval(pollTimer)
pollTimer = setInterval(pollFile, 5 * 1000)
} else {
keyOutput.textContent = '––––'
key = null
downloadlink.href = ''
}
keyGenBtn.blur()
})
}
window.onload = function () {
keyGenBtn.onclick = generateKey
generateKey()
}
</script>
</body>
</html>