-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathterminal.js
More file actions
182 lines (162 loc) · 5.6 KB
/
terminal.js
File metadata and controls
182 lines (162 loc) · 5.6 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
var inputObject = {
history: []
}
var indexHistory = inputObject.history.length - 1;
var registry = new Object();
function getRandomInt() {
return Math.floor(Math.random() * Date.now());
}
function recordToHistory(command) {
if (inputObject.history.length < 20) inputObject.history.push(command);
else {
inputObject.history.shift();
inputObject.history.push(command);
}
}
function smart_split(input, del, empty_space) {
if (input.length === 0) return input;
var outputs = [""];
var compare = function (base, insert, position) {
if ((position + insert.length) > base.length) return false;
for (var i = 0; i < insert.length; i++) {
if (!(base.charAt(position + i) === insert.charAt(i))) return false;
}
return true;
};
var quotes = false;
for (var i = 0; i < input.length; i++) {
var char = input.charAt(i);
if (char === '"') {
quotes = !quotes;
continue;
}
if (!quotes && compare(input, del, i)) {
outputs.push("");
i += del.length - 1;
continue;
}
outputs[outputs.length - 1] += char;
}
if (!empty_space) {
for (var i = 0; i < outputs.length; i++) {
if (outputs[i] === "") {
outputs.splice(i, 1);
}
}
}
return outputs;
}
var terminal_user_title = "guest";
var terminal_user_client = "default"
var clientID = "<a style='color:#DC143C'>" + terminal_user_title + "</a>" + "@" + "<a style='color:#7FFF00'>" + terminal_user_client + "</a>" + " $ ";
function update_user_title(title, client) {
terminal_user_title = title;
terminal_user_client = client;
clientID = "<a style='color:#DC143C'>" + terminal_user_title + "</a>" + "@" + "<a style='color:#7FFF00'>" + terminal_user_client + "</a>" + " $ ";
document.getElementById("input_title").innerHTML = clientID;
}
update_user_title(terminal_user_title, terminal_user_client);
function new_block() {
var wrapper = document.getElementById('wrapper');
var current_block = document.createElement("div");
current_block.classList.add("log");
wrapper.appendChild(current_block);
return current_block;
}
function clear_all_block() {
var wrapper = document.getElementById('wrapper');
wrapper.textContent = ''
}
/**
* Display message to terminal screen
* @param {String} message
*/
function block_log(message) {
var current_block = new_block();
var randid = String(getRandomInt())
current_block.innerHTML += "<p style='white-space:pre-wrap; ' id=" + randid + ">" + message + "</p>";
return document.getElementById(randid)
}
/**
* Default function display message to terminal screen
* @param {String} message
*/
function log(message) {
var wrapper = document.getElementById('wrapper');
wrapper.innerHTML += "<div class='log'><p>" + message + "</p></div>";
}
document.getElementById('input_source').onblur = function () {
document.getElementById("input_source").focus();
};
$("#input_source").keyup(() => {
var command = document.getElementById("input_source").value;
submit_command((command) => {
var base = command.split(" ")[0]
if (registry.hasOwnProperty(base)) {
registry[base].load(command);
} else {
block_log("'" + base + "' is not a registered command, please use 'help' command for listing available commands.");
}
}, clientID + "<a id='inputted'>" + command + "</p>")
});
/**
* Register command to registry.
*/
function register_cmd(cmd = { cmd_name: null, callback: null, description: "", usage: null }) {
if (!cmd.cmd_name || !cmd.callback || !cmd.description) throw Error("Property must not be null.")
if (!cmd.usage) cmd.usage = cmd.cmd_name + " PARAMS"
registry[cmd.cmd_name] = {
load: cmd.callback,
description: cmd.description,
usage: cmd.usage
}
}
function submit_command(handler,
message = "") {
event.preventDefault();
if (!(event.keyCode === 13)) return;
var command = document.getElementById("input_source").value;
if (command) {
recordToHistory(command)
document.getElementById("input_source").value = "";
block_log(message);
handler(command);
}
}
register_cmd({
cmd_name: "help",
callback: (cmd) => {
if (getParameters(cmd).length == 0) {
var msg = ""
for (let key in registry) msg += " - " + key + " - " + registry[key].description + "\n";
block_log("Registry Command List: \n" + msg);
} else {
var command = getParameters(cmd)[0];
if (!registry.hasOwnProperty(command))
return block_log("Command " + command + " is not registered in registry, please use 'help' to show available commands.")
else block_log("USAGE: \n - " + registry[command].usage + "\nDESCRIPTION: \n - " + registry[command].description)
}
},
description: "Show all available commands and their descriptions",
usage: "help [COMMAND]"
});
const getUA = () => {
let device = "Unknown";
const ua = {
"Generic Linux": /Linux/i,
"Android": /Android/i,
"BlackBerry": /BlackBerry/i,
"Bluebird": /EF500/i,
"Chrome OS": /CrOS/i,
"Datalogic": /DL-AXIS/i,
"Honeywell": /CT50/i,
"iPad": /iPad/i,
"iPhone": /iPhone/i,
"iPod": /iPod/i,
"macOS": /Macintosh/i,
"Windows": /IEMobile|Windows/i,
"Zebra": /TC70|TC55/i,
}
Object.keys(ua).map(v => navigator.userAgent.match(ua[v]) && (device = v));
return device;
}