-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
69 lines (65 loc) · 1.86 KB
/
index.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
<html>
<head>
<style>
html, body, textarea { height:100%; width:100%; margin:0; }
</style>
</head>
<body style="display:table">
<div style='display:table-row'><span id='id'></span></div>
<div style='display:table-row;height:100%'>
<textarea id='edit' style='resize: none;'></textarea>
</div>
<script type="text/javascript">
function updateHeader() {
document.getElementById('id').textContent = "id: " + paest_id;
}
function req(method, paest_id, data) {
return new Promise(function(ret, thr){
var xhr = new XMLHttpRequest();
xhr.open(method, "http://a.pae.st/" + paest_id, true);
xhr.onreadystatechange=function() {
if(xhr.readyState===4){
if(xhr.status===200)
ret(xhr.responseText);
if(xhr.status===403)
thr(xhr.responseText);
}
}
if(method === "POST") {
xhr.setRequestHeader('Content-Type', 'application/json');
}
xhr.send(data);
});
}
var scheduled_save;
function save() {
if(scheduled_save) return; // we'll save in a moment. don't spam
req("POST", paest_id +"/"+ paest_key, JSON.stringify({d:editor.value}))
.then(function(data){
data = JSON.parse(data);
if(!data.p) // deleted the paest or something. don't set stuff to 'undefined'
return
paest_id = data.p;
paest_key = data.k;
history.replaceState({}, "", "/" + paest_id + "/" + paest_key);
scheduled_save = 0;
updateHeader();
})
.catch(function(){
if(scheduled_save)
clearTimeout(scheduled_save)
scheduled_save = setTimeout(save, 2000); // Try again in 2 seconds, this loops until saved
});
}
var editor = document.getElementById('edit');
var hashes = document.location.pathname.substring(1).split("/")
var paest_id = hashes[0];
var paest_key = hashes[1] || "";
req("GET", paest_id).then(function(data) {
editor.value=data;
});
editor.onkeyup=save;
updateHeader();
</script>
</body>
</html>