2016-03-12 13:50:43 -05:00
|
|
|
"use strict";
|
|
|
|
|
2016-05-01 09:24:37 -04:00
|
|
|
var gSocket;
|
2016-03-12 13:50:43 -05:00
|
|
|
var gCredentials;
|
|
|
|
var gErrorCount = 0;
|
2016-04-06 21:44:22 -04:00
|
|
|
var gCommandHistory = [];
|
2016-06-02 19:01:55 -04:00
|
|
|
var gSendKeyEvents = false;
|
2016-08-05 21:17:32 -04:00
|
|
|
var gSendDeviceOrientationEvents = false;
|
2016-08-05 18:09:50 -04:00
|
|
|
var gGeolocatorWatch;
|
2016-04-06 21:44:22 -04:00
|
|
|
|
2017-01-16 10:24:44 -05:00
|
|
|
var gEditor;
|
|
|
|
var gBackup;
|
|
|
|
|
2016-04-06 21:44:22 -04:00
|
|
|
var kMaxCommandHistory = 16;
|
2016-03-12 13:50:43 -05:00
|
|
|
|
2017-05-22 15:38:49 -04:00
|
|
|
var kErrorColor = "#dc322f";
|
|
|
|
var kStatusColor = "#fff";
|
|
|
|
|
2017-01-16 10:24:44 -05:00
|
|
|
window.addEventListener("keydown", function(event) {
|
|
|
|
if (event.keyCode == 69 && event.altKey) {
|
|
|
|
if (!editing()) {
|
|
|
|
edit();
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
} else if (event.keyCode == 83 && (event.altKey || event.ctrlKey)) {
|
|
|
|
if (editing()) {
|
|
|
|
save();
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
} else if (event.keyCode == 66 && event.altKey) {
|
|
|
|
if (editing()) {
|
|
|
|
closeEditor();
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-08-20 13:05:10 -04:00
|
|
|
function keydown(event) {
|
2016-03-12 13:50:43 -05:00
|
|
|
if (event.keyCode == 13) {
|
2016-04-06 21:44:22 -04:00
|
|
|
gCommandHistory.push(document.getElementById("input").value);
|
|
|
|
if (gCommandHistory.length > kMaxCommandHistory) {
|
|
|
|
gCommandHistory.shift();
|
|
|
|
}
|
2016-03-12 13:50:43 -05:00
|
|
|
send();
|
|
|
|
event.preventDefault();
|
2016-08-20 13:05:10 -04:00
|
|
|
} else if (event.keyCode == 38 && !event.altKey) {
|
2016-04-06 21:44:22 -04:00
|
|
|
if (gCommandHistory.length) {
|
|
|
|
var input = document.getElementById("input");
|
|
|
|
gCommandHistory.unshift(input.value);
|
|
|
|
input.value = gCommandHistory.pop();
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
2016-08-20 13:05:10 -04:00
|
|
|
} else if (event.keyCode == 40 && !event.altKey) {
|
2016-04-06 21:44:22 -04:00
|
|
|
if (gCommandHistory.length) {
|
|
|
|
var input = document.getElementById("input");
|
|
|
|
gCommandHistory.push(input.value);
|
|
|
|
input.value = gCommandHistory.shift();
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
2017-01-16 10:24:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function ensureLoaded(nodes, callback) {
|
|
|
|
if (!nodes.length) {
|
|
|
|
callback();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var search = nodes.shift();
|
|
|
|
var head = document.head;
|
|
|
|
var found = false;
|
|
|
|
for (var i = 0; i < head.childNodes.length; i++) {
|
|
|
|
if (head.childNodes[i].tagName == search.tagName) {
|
|
|
|
var match = true;
|
|
|
|
for (var attribute in search.attributes) {
|
|
|
|
if (head.childNodes[i].attributes[attribute].value != search.attributes[attribute]) {
|
|
|
|
match = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (match) {
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (found) {
|
|
|
|
ensureLoaded(nodes, callback);
|
|
|
|
} else {
|
|
|
|
var node = document.createElement(search.tagName);
|
|
|
|
node.onreadystatechange = node.onload = function() {
|
|
|
|
ensureLoaded(nodes, callback);
|
|
|
|
};
|
|
|
|
for (var attribute in search.attributes) {
|
|
|
|
node.setAttribute(attribute, search.attributes[attribute]);
|
|
|
|
}
|
|
|
|
head.insertBefore(node, head.firstChild);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function editing() {
|
|
|
|
return document.getElementById("editPane").style.display != 'none';
|
|
|
|
}
|
|
|
|
|
|
|
|
function edit() {
|
|
|
|
if (editing()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ensureLoaded([
|
2017-03-01 17:44:15 -05:00
|
|
|
{tagName: "script", attributes: {src: "https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.23.0/codemirror.min.js"}},
|
|
|
|
{tagName: "link", attributes: {rel: "stylesheet", href: "https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.23.0/theme/base16-dark.min.css"}},
|
|
|
|
{tagName: "link", attributes: {rel: "stylesheet", href: "https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.23.0/addon/search/matchesonscrollbar.min.css"}},
|
|
|
|
{tagName: "link", attributes: {rel: "stylesheet", href: "https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.23.0/addon/dialog/dialog.min.css"}},
|
|
|
|
{tagName: "link", attributes: {rel: "stylesheet", href: "https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.23.0/codemirror.min.css"}},
|
|
|
|
{tagName: "script", attributes: {src: "https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.23.0/addon/edit/trailingspace.min.js"}},
|
|
|
|
{tagName: "script", attributes: {src: "https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.23.0/addon/dialog/dialog.min.js"}},
|
|
|
|
{tagName: "script", attributes: {src: "https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.23.0/addon/search/search.min.js"}},
|
|
|
|
{tagName: "script", attributes: {src: "https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.23.0/addon/search/searchcursor.min.js"}},
|
|
|
|
{tagName: "script", attributes: {src: "https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.23.0/addon/search/jump-to-line.min.js"}},
|
|
|
|
{tagName: "script", attributes: {src: "https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.23.0/addon/search/matchesonscrollbar.min.js"}},
|
|
|
|
{tagName: "script", attributes: {src: "https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.23.0/addon/scroll/annotatescrollbar.min.js"}},
|
|
|
|
{tagName: "script", attributes: {src: "https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.23.0/mode/javascript/javascript.min.js"}},
|
2017-01-16 10:24:44 -05:00
|
|
|
], function() {
|
|
|
|
load();
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function load() {
|
|
|
|
var request = new XMLHttpRequest();
|
|
|
|
request.addEventListener("loadend", function() {
|
2017-01-18 18:21:42 -05:00
|
|
|
if (request.status == 200 || request.status == 404) {
|
2017-01-16 10:24:44 -05:00
|
|
|
document.getElementById("editPane").style.display = 'flex';
|
|
|
|
if (!gEditor) {
|
|
|
|
gEditor = CodeMirror.fromTextArea(document.getElementById("editor"), {
|
|
|
|
'theme': 'base16-dark',
|
|
|
|
'lineNumbers': true,
|
|
|
|
'tabSize': 4,
|
|
|
|
'indentUnit': 4,
|
|
|
|
'indentWithTabs': true,
|
|
|
|
'showTrailingSpace': true,
|
|
|
|
});
|
|
|
|
}
|
2017-01-18 18:21:42 -05:00
|
|
|
var text;
|
|
|
|
if (request.status == 200) {
|
|
|
|
text = request.responseText;
|
|
|
|
} else {
|
|
|
|
text = '// New script\nterminal.print("Hello, world!");\n';
|
|
|
|
}
|
|
|
|
gEditor.setValue(text);
|
2017-01-16 10:24:44 -05:00
|
|
|
gEditor.focus();
|
2017-01-18 18:21:42 -05:00
|
|
|
gBackup = text;
|
2017-01-16 10:24:44 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
request.addEventListener("error", function() {
|
|
|
|
alert("Error loading source.");
|
|
|
|
closeEditor();
|
|
|
|
});
|
|
|
|
request.addEventListener("timeout", function() {
|
|
|
|
alert("Timed out loading source.");
|
|
|
|
closeEditor();
|
|
|
|
});
|
|
|
|
request.addEventListener("abort", function() {
|
|
|
|
alert("Loading source aborted.");
|
|
|
|
closeEditor();
|
|
|
|
});
|
|
|
|
request.open("GET", url() + "/view");
|
|
|
|
request.send();
|
|
|
|
}
|
|
|
|
|
|
|
|
function closeEditor() {
|
|
|
|
document.getElementById("editPane").style.display = 'none';
|
|
|
|
}
|
|
|
|
|
|
|
|
function revert() {
|
|
|
|
gEditor.setValue(gBackup);
|
|
|
|
}
|
|
|
|
|
|
|
|
function explodePath() {
|
|
|
|
return /^\/~([^\/]+)\/([^\/]+)(.*)/.exec(window.location.pathname);
|
|
|
|
}
|
|
|
|
|
|
|
|
function packageOwner() {
|
|
|
|
return explodePath()[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
function packageName() {
|
|
|
|
return explodePath()[2];
|
|
|
|
}
|
|
|
|
|
|
|
|
function save(newName) {
|
|
|
|
document.getElementById("save").disabled = true;
|
|
|
|
document.getElementById("saveAs").disabled = true;
|
|
|
|
|
|
|
|
var contents = gEditor.getValue();
|
|
|
|
var run = document.getElementById("run").checked;
|
|
|
|
|
|
|
|
var request = new XMLHttpRequest();
|
|
|
|
|
|
|
|
var always = function() {
|
|
|
|
document.getElementById("save").disabled = false;
|
|
|
|
document.getElementById("saveAs").disabled = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
request.addEventListener("error", function() {
|
|
|
|
alert("Error saving: " + request.responseText);
|
|
|
|
always();
|
|
|
|
});
|
|
|
|
request.addEventListener("loadend", function() {
|
|
|
|
if (request.status == 200) {
|
|
|
|
gBackup = contents;
|
|
|
|
if (run) {
|
|
|
|
if (newName) {
|
|
|
|
window.location.href = "/~" + packageOwner() + "/" + newName + hash();
|
|
|
|
} else {
|
|
|
|
reconnect();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
alert("Unable to save: " + request.responseText);
|
|
|
|
}
|
|
|
|
always();
|
|
|
|
});
|
|
|
|
request.addEventListener("timeout", function() {
|
|
|
|
alert("Timed out saving: " + request.responseText);
|
|
|
|
always();
|
|
|
|
});
|
|
|
|
request.addEventListener("abort", function() {
|
|
|
|
alert("Save aborted: " + request.responseText);
|
|
|
|
always();
|
|
|
|
});
|
|
|
|
request.open("POST", newName ? newName + "/save" : packageName() + "/save", true);
|
|
|
|
request.setRequestHeader("Content-Type", "text/plain");
|
|
|
|
request.send(contents);
|
|
|
|
}
|
|
|
|
|
|
|
|
function saveAs() {
|
|
|
|
var newName = prompt("Save as:", packageName());
|
|
|
|
if (newName) {
|
|
|
|
save(newName);
|
2016-03-12 13:50:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function url() {
|
|
|
|
var hash = window.location.href.indexOf('#');
|
|
|
|
var question = window.location.href.indexOf('?');
|
2016-04-06 21:30:07 -04:00
|
|
|
var end = -1;
|
|
|
|
if (hash != -1 && (hash < end || end == -1))
|
|
|
|
{
|
|
|
|
end = hash;
|
|
|
|
}
|
|
|
|
if (question != -1 && (question < end || end == -1))
|
|
|
|
{
|
|
|
|
end = question;
|
|
|
|
}
|
2016-03-12 13:50:43 -05:00
|
|
|
return end != -1 ? window.location.href.substring(0, end) : window.location.href;
|
|
|
|
}
|
|
|
|
|
2016-04-06 21:30:07 -04:00
|
|
|
function hash() {
|
|
|
|
return window.location.hash != "#" ? window.location.hash : "";
|
|
|
|
}
|
|
|
|
|
2016-03-12 13:50:43 -05:00
|
|
|
function split(container, children) {
|
|
|
|
if (container) {
|
|
|
|
while (container.firstChild) {
|
|
|
|
container.removeChild(container.firstChild);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (children) {
|
|
|
|
for (var i = 0; i < children.length; i++) {
|
|
|
|
if (children[i].name) {
|
|
|
|
var node = document.createElement("div");
|
|
|
|
node.setAttribute("id", "terminal_" + children[i].name);
|
|
|
|
var grow = children[i].grow || "1";
|
|
|
|
var shrink = children[i].shrink || "1";
|
|
|
|
var basis = children[i].basis || "auto";
|
2016-06-02 19:01:55 -04:00
|
|
|
var style = children[i].style || "";
|
|
|
|
node.setAttribute("style", style + "; flex: " + grow + " " + shrink + " " + basis);
|
2016-04-04 16:26:01 -04:00
|
|
|
|
|
|
|
var classes = ["terminal"];
|
|
|
|
if (children[i].type == "vertical") {
|
|
|
|
classes.push("vbox");
|
|
|
|
} else if (children[i].type == "horizontal") {
|
|
|
|
classes.push("hbox");
|
|
|
|
}
|
|
|
|
node.setAttribute("class", classes.join(" "));
|
2016-03-12 13:50:43 -05:00
|
|
|
container.appendChild(node);
|
|
|
|
} else if (children[i].type) {
|
|
|
|
node = document.createElement("div");
|
|
|
|
if (children[i].type == "horizontal") {
|
|
|
|
node.setAttribute("class", "hbox");
|
|
|
|
} else if (children[i].type == "vertical") {
|
|
|
|
node.setAttribute("class", "vbox");
|
|
|
|
}
|
|
|
|
container.appendChild(node);
|
|
|
|
split(node, children[i].children);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-18 15:18:22 -04:00
|
|
|
var gCorkCount = 0;
|
|
|
|
var gCorkLines = [];
|
|
|
|
|
2016-04-10 20:09:21 -04:00
|
|
|
function receive(data) {
|
|
|
|
for (var i in data.lines) {
|
|
|
|
var line = data.lines[i];
|
|
|
|
|
2017-05-18 15:18:22 -04:00
|
|
|
if (line && line[0] && line[0].action == "cork"
|
|
|
|
|| line && line.value && line.value[0].action == "cork") {
|
|
|
|
++gCorkCount;
|
|
|
|
continue;
|
|
|
|
} else if (line && line[0] && line[0].action == "uncork"
|
|
|
|
|| line && line.value && line.value[0].action == "uncork") {
|
|
|
|
if (--gCorkCount <= 0) {
|
|
|
|
receive({lines: gCorkLines});
|
|
|
|
gCorkLines.length = 0;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gCorkCount > 0) {
|
|
|
|
gCorkLines.push(line);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-04-10 20:09:21 -04:00
|
|
|
var target = document.getElementsByClassName("terminal")[0].id;
|
|
|
|
if (line && line.terminal) {
|
|
|
|
if (document.getElementById("terminal_" + line.terminal)) {
|
|
|
|
target = "terminal_" + line.terminal;
|
2016-03-12 13:50:43 -05:00
|
|
|
}
|
2016-04-10 20:09:21 -04:00
|
|
|
line = line.value;
|
2016-03-12 13:50:43 -05:00
|
|
|
}
|
2017-05-18 15:18:22 -04:00
|
|
|
|
2016-04-10 20:09:21 -04:00
|
|
|
if (line && line.action == "ping") {
|
|
|
|
gSocket.send(JSON.stringify({action: "pong"}));
|
|
|
|
} else if (line && line.action == "session") {
|
2017-05-22 15:38:49 -04:00
|
|
|
setStatusMessage("...Executing...", kStatusColor, true);
|
2016-04-10 20:28:42 -04:00
|
|
|
gCredentials = line.credentials;
|
2016-04-10 20:09:21 -04:00
|
|
|
updateLogin();
|
|
|
|
} else if (line && line[0] && line[0].action == "ready") {
|
2017-05-22 15:38:49 -04:00
|
|
|
setStatusMessage(null);
|
2016-04-10 20:09:21 -04:00
|
|
|
if (window.location.hash) {
|
|
|
|
send({event: "hashChange", hash: window.location.hash});
|
|
|
|
}
|
|
|
|
} else if (line && line[0] && line[0].action == "notify") {
|
2016-04-21 06:13:28 -04:00
|
|
|
if (window.Notification) {
|
|
|
|
new Notification(line[0].title, line[0].options);
|
|
|
|
}
|
2016-04-30 06:50:43 -04:00
|
|
|
} else if (line && line[0] && line[0].action == "setTitle") {
|
2016-04-10 20:09:21 -04:00
|
|
|
window.document.title = line[0].value;
|
2016-04-30 06:50:43 -04:00
|
|
|
} else if (line && line[0] && line[0].action == "setPrompt") {
|
2016-04-10 20:09:21 -04:00
|
|
|
var prompt = document.getElementById("prompt");
|
|
|
|
while (prompt.firstChild) {
|
|
|
|
prompt.removeChild(prompt.firstChild);
|
|
|
|
}
|
|
|
|
prompt.appendChild(document.createTextNode(line[0].value));
|
2016-04-30 06:50:43 -04:00
|
|
|
} else if (line && line[0] && line[0].action == "setPassword") {
|
2016-04-10 20:09:21 -04:00
|
|
|
var prompt = document.getElementById("input");
|
|
|
|
prompt.setAttribute("type", line[0].value ? "password" : "text");
|
2016-04-30 06:50:43 -04:00
|
|
|
} else if (line && line[0] && line[0].action == "setHash") {
|
2016-04-10 20:09:21 -04:00
|
|
|
window.location.hash = line[0].value;
|
|
|
|
} else if (line && line[0] && line[0].action == "update") {
|
|
|
|
document.getElementById("update").setAttribute("Style", "display: inline");
|
|
|
|
} else if (line && line[0] && line[0].action == "split") {
|
|
|
|
split(document.getElementById("terminals"), line[0].options);
|
|
|
|
} else if (line && line[0] && line[0].action == "postMessageToIframe") {
|
|
|
|
var iframe = document.getElementById("iframe_" + line[0].name);
|
|
|
|
if (iframe) {
|
|
|
|
iframe.contentWindow.postMessage(line[0].message, "*");
|
|
|
|
}
|
2016-06-02 19:01:55 -04:00
|
|
|
} else if (line && line[0] && line[0].action == "setSendKeyEvents") {
|
|
|
|
var value = line[0].value;
|
|
|
|
if (value && !gSendKeyEvents) {
|
|
|
|
window.addEventListener("keydown", keyEvent);
|
|
|
|
window.addEventListener("keypress", keyEvent);
|
|
|
|
window.addEventListener("keyup", keyEvent);
|
|
|
|
} else if (!value && gSendKeyEvents) {
|
|
|
|
window.removeEventListener("keydown", keyEvent);
|
|
|
|
window.removeEventListener("keypress", keyEvent);
|
|
|
|
window.removeEventListener("keyup", keyEvent);
|
|
|
|
}
|
|
|
|
gSendKeyEvents = value;
|
2016-08-05 18:09:50 -04:00
|
|
|
} else if (line && line[0] && line[0].action == "getCurrentPosition") {
|
|
|
|
navigator.geolocation.getCurrentPosition(geolocationPosition, geolocationError, line[0].options);
|
|
|
|
} else if (line && line[0] && line[0].action == "watchPosition") {
|
|
|
|
if (navigator && navigator.geolocation && gGeolocatorWatch === undefined) {
|
|
|
|
gGeolocatorWatch = navigator.geolocation.watchPosition(geolocationPosition, geolocationError, line[0].options);
|
|
|
|
}
|
|
|
|
} else if (line && line[0] && line[0].action == "clearWatch") {
|
|
|
|
if (navigator && navigator.geolocation && gGeolocatorWatch !== undefined) {
|
|
|
|
navigator.geolocation.clearWatch(gGeolocatorWatch);
|
|
|
|
}
|
2016-08-05 21:17:32 -04:00
|
|
|
} else if (line && line[0] && line[0].action == "setSendDeviceOrientationEvents") {
|
|
|
|
let value = line[0].value;
|
|
|
|
if (value && !gSendDeviceOrientationEvents) {
|
|
|
|
window.addEventListener("deviceorientation", deviceOrientation);
|
|
|
|
} else if (!value && gSendDeviceOrientationEvents) {
|
|
|
|
window.removeEventListener("deviceorientation", deviceOrientation);
|
|
|
|
}
|
|
|
|
gSendDeviceOrientationEvents = value;
|
2016-03-12 13:50:43 -05:00
|
|
|
} else {
|
2016-04-10 20:09:21 -04:00
|
|
|
print(document.getElementById(target), line);
|
2016-03-12 13:50:43 -05:00
|
|
|
}
|
2016-04-10 20:09:21 -04:00
|
|
|
}
|
2016-03-12 13:50:43 -05:00
|
|
|
}
|
|
|
|
|
2016-08-05 18:09:50 -04:00
|
|
|
function geolocationPosition(position) {
|
|
|
|
send({
|
|
|
|
event: 'geolocation',
|
|
|
|
position: {
|
|
|
|
timestamp: position.timestamp,
|
|
|
|
coords: {
|
|
|
|
latitude: position.coords.latitude,
|
|
|
|
longitude: position.coords.longitude,
|
|
|
|
altitude: position.coords.altitude,
|
|
|
|
accuracy: position.coords.accuracy,
|
|
|
|
altitudeAccuracy: position.coords.altitudeAccuracy,
|
|
|
|
heading: position.coords.heading,
|
|
|
|
speed: position.coords.speed,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function geolocationError(error) {
|
|
|
|
send({
|
|
|
|
event: 'geolocation',
|
|
|
|
error: {
|
|
|
|
code: error.code,
|
|
|
|
message: error.message,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-08-05 21:17:32 -04:00
|
|
|
function deviceOrientation(event) {
|
|
|
|
send({
|
|
|
|
event: 'deviceorientation',
|
|
|
|
orientation: {
|
|
|
|
alpha: event.alpha,
|
|
|
|
beta: event.beta,
|
|
|
|
gamma: event.gamma,
|
|
|
|
absolute: event.absolute,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-06-02 19:01:55 -04:00
|
|
|
function keyEvent(event) {
|
|
|
|
send({
|
|
|
|
event: "key",
|
|
|
|
type: event.type,
|
|
|
|
which: event.which,
|
|
|
|
keyCode: event.keyCode,
|
|
|
|
charCode: event.charCode,
|
|
|
|
character: String.fromCharCode(event.keyCode || event.which),
|
2016-06-02 19:48:45 -04:00
|
|
|
altKey: event.altKey,
|
2016-06-02 19:01:55 -04:00
|
|
|
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-03-12 13:50:43 -05:00
|
|
|
function autoNewLine(terminal) {
|
|
|
|
terminal.appendChild(document.createElement("br"));
|
|
|
|
}
|
|
|
|
|
|
|
|
function print(terminal, data) {
|
|
|
|
autoNewLine(terminal);
|
|
|
|
printStructured(terminal, data);
|
|
|
|
autoScroll(terminal);
|
|
|
|
}
|
|
|
|
|
2016-04-30 14:28:33 -04:00
|
|
|
function printSvg(container, data, name, namespace) {
|
2016-06-02 19:01:55 -04:00
|
|
|
var node;
|
|
|
|
if (typeof data == "string") {
|
|
|
|
node = document.createTextNode(data);
|
|
|
|
} else {
|
|
|
|
node = document.createElementNS("http://www.w3.org/2000/svg", name);
|
|
|
|
for (var i in data.attributes) {
|
|
|
|
node.setAttribute(i, data.attributes[i]);
|
|
|
|
}
|
|
|
|
if (data.children) {
|
|
|
|
for (var i in data.children) {
|
|
|
|
node.appendChild(printSvg(node, data.children[i], data.children[i].name));
|
|
|
|
}
|
2016-04-30 14:28:33 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2016-03-12 13:50:43 -05:00
|
|
|
function printStructured(container, data) {
|
|
|
|
if (typeof data == "string") {
|
|
|
|
container.appendChild(document.createTextNode(data));
|
|
|
|
} else if (data && data[0] !== undefined) {
|
|
|
|
for (var i in data) {
|
|
|
|
printStructured(container, data[i]);
|
|
|
|
}
|
|
|
|
} else if (data && data.action == "clear") {
|
|
|
|
while (container.firstChild) {
|
|
|
|
container.removeChild(container.firstChild);
|
|
|
|
}
|
|
|
|
} else if (data) {
|
|
|
|
var node;
|
|
|
|
if (data.href) {
|
|
|
|
node = document.createElement("a");
|
|
|
|
node.setAttribute("href", data.href);
|
2016-06-02 19:01:55 -04:00
|
|
|
node.setAttribute("target", data.target || "_blank");
|
2016-03-12 13:50:43 -05:00
|
|
|
} else if (data.iframe) {
|
|
|
|
node = document.createElement("iframe");
|
2016-09-17 16:53:03 -04:00
|
|
|
if (data.src) {
|
|
|
|
node.setAttribute("src", data.src);
|
|
|
|
} else {
|
|
|
|
node.setAttribute("srcdoc", data.iframe);
|
|
|
|
}
|
|
|
|
node.setAttribute("sandbox", "allow-forms allow-scripts allow-top-navigation allow-same-origin");
|
2017-01-03 18:23:50 -05:00
|
|
|
if (data.width !== null) {
|
|
|
|
node.setAttribute("width", data.width || 320);
|
|
|
|
}
|
|
|
|
if (data.height !== null) {
|
|
|
|
node.setAttribute("height", data.height || 240);
|
|
|
|
}
|
2016-03-12 13:50:43 -05:00
|
|
|
if (data.name) {
|
|
|
|
node.setAttribute("id", "iframe_" + data.name);
|
|
|
|
}
|
2016-04-30 14:28:33 -04:00
|
|
|
} else if (data.svg) {
|
|
|
|
node = printSvg(container, data.svg, "svg");
|
2016-03-12 13:50:43 -05:00
|
|
|
} else if (data.image) {
|
|
|
|
node = document.createElement("img");
|
|
|
|
node.setAttribute("src", data.image);
|
2016-05-01 09:24:37 -04:00
|
|
|
} else if (data.input) {
|
|
|
|
node = document.createElement("input");
|
|
|
|
node.setAttribute("type", data.input);
|
|
|
|
if (data.name) {
|
|
|
|
node.name = data.name;
|
|
|
|
}
|
|
|
|
if (data.input == "submit") {
|
|
|
|
node.onclick = submitButton;
|
|
|
|
}
|
2016-03-12 13:50:43 -05:00
|
|
|
} else {
|
|
|
|
node = document.createElement("span");
|
|
|
|
}
|
2016-05-01 09:24:37 -04:00
|
|
|
|
2016-03-12 13:50:43 -05:00
|
|
|
if (data.style) {
|
|
|
|
node.setAttribute("style", data.style);
|
|
|
|
}
|
|
|
|
if (data.class) {
|
|
|
|
node.setAttribute("class", data.class);
|
|
|
|
}
|
|
|
|
var value = data.value || data.href || data.command || "";
|
2016-05-01 09:24:37 -04:00
|
|
|
if (data.input) {
|
|
|
|
node.value = value;
|
|
|
|
} else if (!value && data.message && data.stackTrace) {
|
2016-03-12 13:50:43 -05:00
|
|
|
printStructured(node, data.message);
|
|
|
|
node.appendChild(document.createElement("br"));
|
|
|
|
printStructured(node, data.fileName + ":" + data.lineNumber + ":");
|
|
|
|
node.appendChild(document.createElement("br"));
|
|
|
|
if (data.stackTrace.length) {
|
|
|
|
for (var i = 0; i < data.stackTrace.length; i++) {
|
|
|
|
printStructured(node, data.stackTrace[i]);
|
|
|
|
node.appendChild(document.createElement("br"));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
printStructured(node, data.sourceLine);
|
|
|
|
}
|
|
|
|
} else if (value === undefined) {
|
|
|
|
printStructured(node, JSON.stringify(value));
|
|
|
|
} else {
|
|
|
|
printStructured(node, value);
|
|
|
|
}
|
|
|
|
if (data.command) {
|
|
|
|
node.dataset.command = data.command;
|
|
|
|
node.onclick = commandClick;
|
|
|
|
node.setAttribute("class", "command");
|
|
|
|
}
|
|
|
|
container.appendChild(node);
|
|
|
|
} else {
|
|
|
|
printStructured(container, JSON.stringify(data));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function commandClick() {
|
|
|
|
send(this.dataset.command);
|
2016-04-11 11:54:26 -04:00
|
|
|
document.getElementById("input").focus();
|
2016-03-12 13:50:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function autoScroll(terminal) {
|
|
|
|
terminal.scrollTop = terminal.scrollHeight - terminal.clientHeight;
|
|
|
|
}
|
|
|
|
|
2017-05-22 15:38:49 -04:00
|
|
|
function setStatusMessage(message, color, keep) {
|
2016-04-10 20:28:42 -04:00
|
|
|
var node = document.getElementById("status");
|
2017-05-22 15:38:49 -04:00
|
|
|
if (!keep) {
|
|
|
|
while (node.firstChild) {
|
|
|
|
node.removeChild(node.firstChild);
|
|
|
|
}
|
2016-04-10 20:28:42 -04:00
|
|
|
}
|
2016-05-07 07:07:54 -04:00
|
|
|
if (message) {
|
|
|
|
node.appendChild(document.createTextNode(message));
|
2017-05-22 15:38:49 -04:00
|
|
|
node.setAttribute("style", "display: inline; color: " + (color || kErrorColor));
|
2016-05-07 07:07:54 -04:00
|
|
|
}
|
2016-04-10 20:28:42 -04:00
|
|
|
}
|
|
|
|
|
2016-03-12 13:50:43 -05:00
|
|
|
function send(command) {
|
|
|
|
var value = command;
|
|
|
|
if (!command) {
|
2016-06-05 09:21:00 -04:00
|
|
|
value = document.getElementById("input").value;
|
2016-04-11 11:54:26 -04:00
|
|
|
document.getElementById("input").value = "";
|
2016-03-12 13:50:43 -05:00
|
|
|
}
|
2016-04-10 20:09:21 -04:00
|
|
|
try {
|
|
|
|
gSocket.send(JSON.stringify({action: "command", command: value}));
|
|
|
|
} catch (error) {
|
2017-05-22 15:38:49 -04:00
|
|
|
setStatusMessage("Send failed: " + error.toString(), kErrorColor);
|
2016-04-10 20:09:21 -04:00
|
|
|
}
|
2016-03-12 13:50:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function updateLogin() {
|
|
|
|
var login = document.getElementById("login");
|
|
|
|
while (login.firstChild) {
|
|
|
|
login.removeChild(login.firstChild);
|
|
|
|
}
|
|
|
|
|
|
|
|
var a = document.createElement("a");
|
|
|
|
if (gCredentials && gCredentials.session) {
|
|
|
|
a.appendChild(document.createTextNode("logout " + gCredentials.session.name));
|
2016-04-03 15:31:03 -04:00
|
|
|
if (gCredentials.session.google) {
|
|
|
|
gapi.load("auth2", function() {
|
|
|
|
gapi.auth2.init();
|
|
|
|
});
|
|
|
|
a.setAttribute("onclick", "logoutGoogle()");
|
|
|
|
a.setAttribute("href", "#");
|
|
|
|
} else {
|
2016-04-06 21:30:07 -04:00
|
|
|
a.setAttribute("href", "/login/logout?return=" + encodeURIComponent(url() + hash()));
|
2016-04-03 15:31:03 -04:00
|
|
|
}
|
2016-03-12 13:50:43 -05:00
|
|
|
} else {
|
2016-04-14 19:37:23 -04:00
|
|
|
a.appendChild(document.createTextNode("login"));
|
|
|
|
a.setAttribute("href", "/login?return=" + encodeURIComponent(url() + hash()));
|
2016-03-12 13:50:43 -05:00
|
|
|
}
|
|
|
|
login.appendChild(a);
|
|
|
|
}
|
|
|
|
|
2016-04-03 15:31:03 -04:00
|
|
|
function logoutGoogle() {
|
|
|
|
gapi.auth2.getAuthInstance().signOut().then(function() {
|
2016-04-06 21:30:07 -04:00
|
|
|
window.location.href = "/login/logout?return=" + encodeURIComponent(url() + hash());
|
2016-04-03 15:31:03 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-03-12 13:50:43 -05:00
|
|
|
var gOriginalInput;
|
|
|
|
function dragHover(event) {
|
|
|
|
event.stopPropagation();
|
|
|
|
event.preventDefault();
|
2016-04-11 11:54:26 -04:00
|
|
|
var input = document.getElementById("input");
|
2016-03-12 13:50:43 -05:00
|
|
|
if (event.type == "dragover") {
|
2016-04-11 11:54:26 -04:00
|
|
|
if (!input.classList.contains("drop")) {
|
|
|
|
input.classList.add("drop");
|
|
|
|
gOriginalInput = input.value;
|
|
|
|
input.value = "drop file to upload";
|
2016-03-12 13:50:43 -05:00
|
|
|
}
|
|
|
|
} else {
|
2016-04-11 11:54:26 -04:00
|
|
|
input.classList.remove("drop");
|
|
|
|
input.value = gOriginalInput;
|
2016-03-12 13:50:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function fixImage(sourceData, maxWidth, maxHeight, callback) {
|
|
|
|
var result = sourceData;
|
|
|
|
var image = new Image();
|
|
|
|
image.crossOrigin = "anonymous";
|
|
|
|
image.referrerPolicy = "no-referrer";
|
|
|
|
image.onload = function() {
|
|
|
|
if (image.width > maxWidth || image.height > maxHeight) {
|
|
|
|
var downScale = Math.min(maxWidth / image.width, maxHeight / image.height);
|
|
|
|
var canvas = document.createElement("canvas");
|
|
|
|
canvas.width = image.width * downScale;
|
|
|
|
canvas.height = image.height * downScale;
|
|
|
|
var context = canvas.getContext("2d");
|
|
|
|
context.clearRect(0, 0, canvas.width, canvas.height);
|
|
|
|
image.width = canvas.width;
|
|
|
|
image.height = canvas.height;
|
|
|
|
context.drawImage(image, 0, 0, image.width, image.height);
|
|
|
|
result = canvas.toDataURL();
|
|
|
|
}
|
|
|
|
callback(result);
|
|
|
|
};
|
|
|
|
image.src = sourceData;
|
|
|
|
}
|
|
|
|
|
|
|
|
function sendImage(image) {
|
|
|
|
fixImage(image, 320, 240, function(result) {
|
|
|
|
send({image: result});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function fileDropRead(event) {
|
|
|
|
sendImage(event.target.result);
|
|
|
|
}
|
|
|
|
|
|
|
|
function fileDrop(event) {
|
|
|
|
dragHover(event);
|
|
|
|
|
|
|
|
var done = false;
|
|
|
|
if (!done) {
|
|
|
|
var files = event.target.files || event.dataTransfer.files;
|
|
|
|
for (var i = 0; i < files.length; i++) {
|
|
|
|
var file = files[i];
|
|
|
|
if (file.type.substring(0, "image/".length) == "image/") {
|
|
|
|
var reader = new FileReader();
|
|
|
|
reader.onloadend = fileDropRead;
|
|
|
|
reader.readAsDataURL(file);
|
|
|
|
done = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!done) {
|
|
|
|
var html = event.dataTransfer.getData("text/html");
|
|
|
|
var match = /<img.*src="([^"]+)"/.exec(html);
|
|
|
|
if (match) {
|
|
|
|
sendImage(match[1]);
|
|
|
|
done = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!done) {
|
|
|
|
var text = event.dataTransfer.getData("text/plain");
|
|
|
|
if (text) {
|
|
|
|
send(text);
|
|
|
|
done = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function enableDragDrop() {
|
|
|
|
var body = document.body;
|
|
|
|
body.addEventListener("dragover", dragHover);
|
|
|
|
body.addEventListener("dragleave", dragHover);
|
|
|
|
|
|
|
|
body.addEventListener("drop", fileDrop);
|
|
|
|
}
|
|
|
|
|
|
|
|
function hashChange() {
|
|
|
|
send({event: 'hashChange', hash: window.location.hash});
|
|
|
|
}
|
|
|
|
|
|
|
|
function focus() {
|
2016-05-07 07:07:54 -04:00
|
|
|
if (gSocket && gSocket.readyState == gSocket.CLOSED) {
|
|
|
|
connectSocket();
|
|
|
|
} else {
|
|
|
|
send({event: "focus"});
|
|
|
|
}
|
2016-03-12 13:50:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function blur() {
|
2016-05-07 07:07:54 -04:00
|
|
|
if (gSocket && gSocket.readyState == gSocket.OPEN) {
|
|
|
|
send({event: "blur"});
|
|
|
|
}
|
2016-03-12 13:50:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function onMessage(event) {
|
2016-09-17 16:53:03 -04:00
|
|
|
if (event.data && event.data.event == "resizeMe" && event.data.width && event.data.height) {
|
|
|
|
var iframe = document.getElementById("iframe_" + event.data.name);
|
|
|
|
iframe.setAttribute("width", event.data.width);
|
|
|
|
iframe.setAttribute("height", event.data.height);
|
2016-09-18 08:56:31 -04:00
|
|
|
var node = iframe.parentElement;
|
|
|
|
while (node && !node.classList.contains("terminal")) {
|
|
|
|
node = node.parentElement;
|
|
|
|
}
|
|
|
|
if (node) {
|
|
|
|
autoScroll(node);
|
|
|
|
}
|
2016-09-17 16:53:03 -04:00
|
|
|
} else {
|
|
|
|
send({event: "onWindowMessage", message: event.data});
|
|
|
|
}
|
2016-03-12 13:50:43 -05:00
|
|
|
}
|
|
|
|
|
2016-05-01 09:24:37 -04:00
|
|
|
function submitButton() {
|
|
|
|
var inputs = document.getElementsByTagName("input");
|
|
|
|
var data = {};
|
|
|
|
for (var i in inputs) {
|
|
|
|
var input = inputs[i];
|
|
|
|
if (input.name) {
|
|
|
|
data[input.name] = input.value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
send({event: "submit", value: data});
|
|
|
|
}
|
2016-04-10 20:09:21 -04:00
|
|
|
|
2017-01-16 10:24:44 -05:00
|
|
|
function reconnect() {
|
|
|
|
let oldSocket = gSocket;
|
|
|
|
gSocket = null
|
|
|
|
oldSocket.onclose = function() {}
|
|
|
|
oldSocket.onmessage = function() {}
|
|
|
|
oldSocket.close();
|
|
|
|
split(document.getElementById("terminals"), [{name: "terminal_"}]);
|
|
|
|
connectSocket();
|
|
|
|
}
|
|
|
|
|
2016-05-07 07:07:54 -04:00
|
|
|
function connectSocket() {
|
|
|
|
if (!gSocket || gSocket.readyState == gSocket.CLOSED) {
|
2017-05-22 15:38:49 -04:00
|
|
|
setStatusMessage("Connecting...", kStatusColor, true);
|
2016-05-07 07:07:54 -04:00
|
|
|
gSocket = new WebSocket(
|
|
|
|
(window.location.protocol == "https:" ? "wss://" : "ws://")
|
|
|
|
+ window.location.hostname
|
|
|
|
+ (window.location.port.length ? ":" + window.location.port : "")
|
|
|
|
+ "/terminal/socket");
|
|
|
|
gSocket.onopen = function() {
|
2017-05-22 15:38:49 -04:00
|
|
|
setStatusMessage("...Authenticating...", kStatusColor, true);
|
2016-05-07 07:07:54 -04:00
|
|
|
gSocket.send(JSON.stringify({
|
|
|
|
action: "hello",
|
|
|
|
path: window.location.pathname,
|
|
|
|
terminalApi: [
|
|
|
|
['clear'],
|
|
|
|
['notify', 'title', 'options'],
|
|
|
|
['postMessageToIframe', 'name', 'message'],
|
|
|
|
['setHash', 'value'],
|
|
|
|
['setPassword', 'value'],
|
|
|
|
['setPrompt', 'value'],
|
|
|
|
['setTitle', 'value'],
|
|
|
|
['split', 'options'],
|
2016-06-02 19:01:55 -04:00
|
|
|
['setSendKeyEvents', 'value'],
|
2016-08-05 18:09:50 -04:00
|
|
|
|
|
|
|
['getCurrentPosition', 'options'],
|
|
|
|
['watchPosition', 'options'],
|
|
|
|
['clearWatch'],
|
2016-08-05 21:17:32 -04:00
|
|
|
|
2017-05-18 15:18:22 -04:00
|
|
|
['cork'],
|
|
|
|
['uncork'],
|
|
|
|
|
2016-08-05 21:17:32 -04:00
|
|
|
['setSendDeviceOrientationEvents', 'value'],
|
2016-05-07 07:07:54 -04:00
|
|
|
],
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
gSocket.onmessage = function(event) {
|
|
|
|
receive(JSON.parse(event.data));
|
|
|
|
}
|
|
|
|
gSocket.onclose = function(event) {
|
2017-05-22 15:38:49 -04:00
|
|
|
setStatusMessage("Connection closed with code " + event.code, kErrorColor);
|
2016-05-07 07:07:54 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-11 11:54:26 -04:00
|
|
|
window.addEventListener("load", function() {
|
2016-04-21 06:13:28 -04:00
|
|
|
if (window.Notification) {
|
2016-03-12 13:50:43 -05:00
|
|
|
Notification.requestPermission();
|
|
|
|
}
|
2016-04-11 11:54:26 -04:00
|
|
|
var input = document.getElementById("input");
|
2016-08-20 13:05:10 -04:00
|
|
|
input.addEventListener("keydown", keydown);
|
2016-04-11 11:54:26 -04:00
|
|
|
input.focus();
|
2016-03-12 13:50:43 -05:00
|
|
|
window.addEventListener("hashchange", hashChange);
|
|
|
|
window.addEventListener("focus", focus);
|
|
|
|
window.addEventListener("blur", blur);
|
|
|
|
window.addEventListener("message", onMessage, false);
|
2016-05-07 07:07:54 -04:00
|
|
|
window.addEventListener("online", connectSocket);
|
2016-03-12 13:50:43 -05:00
|
|
|
enableDragDrop();
|
2016-05-07 07:07:54 -04:00
|
|
|
connectSocket();
|
2016-03-12 13:50:43 -05:00
|
|
|
});
|