Files
send2ereader/download.html
2020-03-05 14:36:12 +01:00

103 lines
2.2 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!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>