// started on main page load
function init() {
init_board();
}
function init_board() {
var xhr = new XMLHttpRequest();
const searchParams = (new URL(document.location)).searchParams;
if (!searchParams.has('id')) {
redirect_to_new_page();
return;
}
// 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 file = new Blob([this.response]);
reader = new FileReader();
reader.onload = function() {
var board = document.getElementById("board");
board.innerHTML = reader.result;
// Initialization done. Now we can start modifying.
board.addEventListener("input", function() {on_modify(); });
setInterval(function() {checkupdate();}, 2000);
}
reader.readAsBinaryString(file);
//set_status(""); // OK
}
var parser = new DOMParser();
var xmlDocument = parser.parseFromString("", "text/xml");
var requestElement = xmlDocument.getElementsByTagName("request")[0];
var commandElement = xmlDocument.createElement("command");
commandElement.appendChild(document.createTextNode("getfile"));
requestElement.appendChild(commandElement);
var idElement = xmlDocument.createElement("id");
idElement.appendChild(document.createTextNode(get_id()));
requestElement.appendChild(idElement);
xhr.open("POST", "whiteboard.fcgi", true);
xhr.setRequestHeader("Content-type", "text/xml");
xhr.responseType = 'blob';
xhr.send(xmlDocument);
//set_status("Please wait while server prepares " + filename + " ...");
}
function get_id()
{
const searchParams = (new URL(document.location)).searchParams;
return searchParams.get('id');
}
function on_new_page()
{
redirect_to_new_page();
}
function redirect_to_new_page()
{
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 id = this.responseText;
//alert("location=" + document.location.href);
var new_location = document.location.href;
var pos = new_location.search("\\?");
if (pos >= 0)
new_location = new_location.substring(0, pos);
new_location += '?id=' + id;
window.location.href = new_location;
//set_status(""); // OK
}
var parser = new DOMParser();
var xmlDocument = parser.parseFromString("", "text/xml");
var requestElement = xmlDocument.getElementsByTagName("request")[0];
var commandElement = xmlDocument.createElement("command");
commandElement.appendChild(document.createTextNode("newid"));
requestElement.appendChild(commandElement);
xhr.open("POST", "whiteboard.fcgi", true);
xhr.setRequestHeader("Content-type", "text/xml");
xhr.send(xmlDocument);
//set_status("Please wait while server prepares " + filename + " ...");
}
function on_modify()
{
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;
}
//set_status(""); // OK
}
var parser = new DOMParser();
var xmlDocument = parser.parseFromString("", "text/xml");
var requestElement = xmlDocument.getElementsByTagName("request")[0];
var commandElement = xmlDocument.createElement("command");
commandElement.appendChild(document.createTextNode("modify"));
requestElement.appendChild(commandElement);
var idElement = xmlDocument.createElement("id");
idElement.appendChild(document.createTextNode(get_id()));
requestElement.appendChild(idElement);
var dataElement = xmlDocument.createElement("data");
dataElement.appendChild(document.createTextNode(document.getElementById("board").value));
requestElement.appendChild(dataElement);
xhr.open("POST", "whiteboard.fcgi", true);
xhr.setRequestHeader("Content-type", "text/xml");
xhr.responseType = 'blob';
xhr.send(xmlDocument);
//set_status("Please wait while server prepares " + filename + " ...");
}
// checksum of string
function checksum32(s) {
var result = 0;
for (var i = 0; i < s.length; i++) {
result = ((((result >>> 1) | ((result & 1) << 31)) | 0) ^ (s.charCodeAt(i) & 0xFF)) | 0;
}
return (result & 0x7FFFFFFF) | 0;
}
// gets called by regular polling
function checkupdate() {
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;
}
// no change if response is text/plain
if (this.getResponseHeader("Content-Type") == "application/octet-stream") {
var file = new Blob([this.response]);
reader = new FileReader();
reader.onload = function() {
var board = document.getElementById("board");
board.value = reader.result;
}
reader.readAsBinaryString(file);
}
//set_status(""); // OK
}
var parser = new DOMParser();
var xmlDocument = parser.parseFromString("", "text/xml");
var requestElement = xmlDocument.getElementsByTagName("request")[0];
var commandElement = xmlDocument.createElement("command");
commandElement.appendChild(document.createTextNode("checkupdate"));
requestElement.appendChild(commandElement);
var idElement = xmlDocument.createElement("id");
idElement.appendChild(document.createTextNode(get_id()));
requestElement.appendChild(idElement);
var checksumElement = xmlDocument.createElement("checksum");
checksumElement.appendChild(document.createTextNode(checksum32(document.getElementById("board").value)));
requestElement.appendChild(checksumElement);
xhr.open("POST", "whiteboard.fcgi", true);
xhr.setRequestHeader("Content-type", "text/xml");
xhr.responseType = 'blob';
xhr.send(xmlDocument);
//set_status("Please wait while server prepares " + filename + " ...");
}