forked from pereorga/minimalist-web-notepad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
43 lines (35 loc) · 1.41 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
/*! Minimalist Web Notepad | https://github.com/pereorga/minimalist-web-notepad */
function uploadContent() {
if (content !== textarea.value) {
var temp = textarea.value;
var request = new XMLHttpRequest();
request.open('POST', window.location.href, true);
request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
request.onload = function() {
if (request.readyState === 4) {
// If the request has ended, check again after 1 second.
content = temp;
setTimeout(uploadContent, 1000);
}
}
request.onerror = function() {
// Try again after 1 second.
setTimeout(uploadContent, 1000);
}
request.send('text=' + encodeURIComponent(temp));
// Update the printable contents.
printable.removeChild(printable.firstChild);
printable.appendChild(document.createTextNode(temp));
}
else {
// If the content has not changed, check again after 1 second.
setTimeout(uploadContent, 1000);
}
}
var textarea = document.getElementById('content');
var printable = document.getElementById('printable');
var content = textarea.value;
// Initialize the printable contents with the initial value of the textarea.
printable.appendChild(document.createTextNode(content));
textarea.focus();
uploadContent();