summaryrefslogtreecommitdiffhomepage
path: root/html/whiteboard.js
blob: f79443e463d2dddacd3258fe589d0b867d7d4cb6 (plain)
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// started on main page load
function init() {

	// Connect "Enter" in text field with Button click
	var url = document.getElementById("url");
	url.addEventListener("keyup", function(event) {
  		if (event.keyCode === 13) {
   			event.preventDefault();
   			on_start();
  		}
	});
}

function set_status(message) {
	if (message == "")
		message = " ";

	document.getElementById("status").innerHTML = message;
}

// started on button click: get filename
function on_start() {
	var xhr = new XMLHttpRequest();

	// run on data received back
	xhr.onreadystatechange = function() {
                                if (this.readyState != 4) {
					return;
				}
				if (this.status != 200) {
					set_status("Server Error while retrieving filename, " + filename + ", status: " + this.status + " " + this.statusText);
                                        return;
                                }

				var filename = this.responseText;

				get_file(filename);
	}

	var parser = new DOMParser();
	var xmlDocument = parser.parseFromString("<request></request>", "text/xml");
	
	var requestElement = xmlDocument.getElementsByTagName("request")[0];

	var commandElement = xmlDocument.createElement("command");
	commandElement.appendChild(document.createTextNode("getfilename"));
	requestElement.appendChild(commandElement);

	var urlElement = xmlDocument.createElement("url");
	urlElement.appendChild(document.createTextNode(document.getElementById("url").value));
	requestElement.appendChild(urlElement);

	var formatElement = xmlDocument.createElement("format");
	formatElement.appendChild(document.createTextNode(document.getElementById("mp3").checked ? "mp3" : "mp4"));
	requestElement.appendChild(formatElement);
	
	xhr.open("POST", "downtube.fcgi", true);
	xhr.setRequestHeader("Content-type", "text/xml");
	xhr.responseType = 'text';
	xhr.send(xmlDocument);

	set_status("Please wait while retrieving filename...");
}

// started on button click: get file
function get_file(filename) {
	var xhr = new XMLHttpRequest();

	// run on data received back
	xhr.onreadystatechange = function() {
				if (this.readyState == 3) {
					set_status("Please wait while downloading " + filename + " ...");
					return;
				}
                                if (this.readyState != 4) {
					return;
				}
				if (this.status != 200) {
					set_status("Server Error while retrieving " + filename + ", status: " + this.status + " " + this.statusText);
                                        return;
                                }

                                var a = document.getElementById("download-a");
                                a.setAttribute("download", filename);
                                var file = new Blob([this.response]);
                                a.href = window.URL.createObjectURL(file);
                                a.click();
				
				set_status(""); // OK
	}

	var parser = new DOMParser();
	var xmlDocument = parser.parseFromString("<request></request>", "text/xml");
	
	var requestElement = xmlDocument.getElementsByTagName("request")[0];

	var commandElement = xmlDocument.createElement("command");
	commandElement.appendChild(document.createTextNode("getfile"));
	requestElement.appendChild(commandElement);

	var urlElement = xmlDocument.createElement("url");
	urlElement.appendChild(document.createTextNode(document.getElementById("url").value));
	requestElement.appendChild(urlElement);

	var formatElement = xmlDocument.createElement("format");
	formatElement.appendChild(document.createTextNode(document.getElementById("mp3").checked ? "mp3" : "mp4"));
	requestElement.appendChild(formatElement);
	
	xhr.open("POST", "downtube.fcgi", true);
	xhr.setRequestHeader("Content-type", "text/xml");
	xhr.responseType = 'blob';
	xhr.send(xmlDocument);

	set_status("Please wait while server prepares " + filename + " ...");
}