// Focus
var gLastDropDownElement = null;
function OnFocusChange(event)
{
if (gLastDropDownElement)
{
if (event.relatedTarget && event.relatedTarget.tagName == "A") {
event.relatedTarget.click();
}
gLastDropDownElement.style.display = "none";
gLastDropDownElement = null;
}
if (!document.activeElement || !document.activeElement.parentElement.className.includes("dropdown")) {
return;
}
let dropDown = document.activeElement.parentElement.children[1];
if (!dropDown) {
return;
}
dropDown.style.display = "block";
gLastDropDownElement = dropDown;
}
window.addEventListener("focusin", OnFocusChange);
window.addEventListener("focusout", OnFocusChange);
// Utils
function IsNumeric(str) { return /^-?\d+$/.test(str); }
function ToClipboard(str) { navigator.clipboard.writeText(str); };
const COL_DEFS = {
w: "#FEFEFE",
gy: "#BEBEBE",
r: "#FF5656",
y: "#FFFF56",
g: "#56FF56",
c: "#56FFFF",
b: "#5656FF",
p: "#FF56FF",
};
function FormatColor(key, str) { return "[[;" + COL_DEFS[key] + ";]" + str + "]"; }
function FormatColorRaw(key, str)
{
const col = COL_DEFS[key];
return `${str}`;
}
function FormatClipboard(str) { return `${str}`; }
function FormatCommandExec(str) { return `${str}`; }
function FormatSpoiler(str) { return `${str}`; }
// Terminal
const TERMINAL_ID = "terminal";
var gTerminal = null;
var gCmds = {
NumFails: 0,
NumPerPage: 10,
Folder: "",
List: []
};
const CMD_NOTFOUND_MSG = FormatColorRaw("r", "Command not found.") + FormatColorRaw("y", " Type '" + FormatCommandExec("help") + "' to list available commands.");
const CMD_FAILEXECUTE_MSG = FormatColor("r", "(ERROR) Failed to execute command: ");
function FormatCommandInfo(cmd)
{
let buf = FormatColorRaw("gy", `${cmd.n}`);
if (cmd.u) {
buf += " " + FormatColorRaw("y", cmd.u);
}
if (cmd.d) {
buf += " - " + cmd.d;
}
return buf;
}
function OnTerminalInput(cmd, args)
{
if (cmd == "h" || cmd == "help" || cmd == "cmds")
{
if (args.length && !IsNumeric(args[0]))
{
const str = args[0].toLowerCase();
const searchStartOnly = (str[0] == "~");
if (searchStartOnly) {
str = str.substring(1);
}
this.echo("Available Commands:");
for (const command of gCmds.List)
{
const index = command.n.indexOf(str);
if (index == -1 || searchStartOnly && index != 0) {
continue;
}
this.echo("# " + FormatCommandInfo(command), { raw: true });
}
}
else
{
let numPages = 1;
if (gCmds.List.length > gCmds.NumPerPage) {
numPages += Math.floor((gCmds.List.length - 1) / gCmds.NumPerPage);
}
let curPage = (args.length ? Math.min(numPages, Math.max(1, parseInt(args[0]))) - 1 : 0);
let startIndex = (curPage * 10);
let endIndex = Math.min(gCmds.List.length, startIndex + gCmds.NumPerPage);
this.echo("Available Commands (" + FormatColor("y", "Page Index: " + (curPage + 1) + "/" + numPages + ")") + ":");
for (var i = startIndex; endIndex > i; ++i) {
this.echo("# " + FormatCommandInfo(gCmds.List[i]), { raw: true });
}
}
return;
}
const forceCmd = (cmd[0] == "~");
if (forceCmd) {
cmd = cmd.substring(1);
}
for (const command of gCmds.List)
{
if (command.n != cmd) {
continue;
}
if (command.a && command.a > args.length)
{
this.echo("USAGE: " + FormatColor("gy", command.n) + " " + FormatColor("y", command.u));
return;
}
if (command.Callback) {
command.Callback(args);
}
else
{
$.getScript(gCmds.Folder + command.n + ".js").done(function()
{
command.Callback = OnCommand.bind(gTerminal);
command.Callback(args);
}).fail(function(jqxhr, settings, exception)
{
this.echo(CMD_FAILEXECUTE_MSG + FormatColor("y", exception + "."));
});
}
return;
}
if (forceCmd)
{
$.getScript(gCmds.Folder + cmd + ".js") .done(function()
{
gCmds.List.push({ n: cmd, Callback: OnCommand.bind(gTerminal) });
OnCommand(args);
}).fail(function(jqxhr, settings, exception)
{
this.echo(CMD_FAILEXECUTE_MSG + FormatColor("y", exception + "."));
});
return;
}
this.echo(CMD_NOTFOUND_MSG, { raw: true });
}
function TerminalLoadCommands(folder, path)
{
if (!gTerminal)
{
setTimeout(TerminalLoadCommands, 100, folder, path);
return;
}
gCmds.Folder = folder;
$.getJSON(path, function(list)
{
list.sort((a, b) => (a.n > b.n) ? 1 : ((b.n > a.n) ? -1 : 0));
gCmds.List = list;
}).fail(function()
{
if (gCmds.NumFails++ > 3) {
gTerminal.echo(FormatColor("r", "(ERROR) Failed to load commands list."));
} else {
setTimeout(TerminalLoadCommands, 1000, folder, path);
}
});
}
let timeElement = document.getElementById("nav-time");
if (timeElement)
{
const fn = function()
{
const date = new Date();
const hh = date.getHours();
const mm = date.getMinutes();
const ss = date.getSeconds();
this.innerText = (10 > hh ? "0" : "") + hh + ":" + (10 > mm ? "0" : "") + mm + ":" + (10 > ss ? "0" : "") + ss;
}.bind(timeElement);
fn();
setInterval(fn, 1000);
}
// Initialization
$.ajaxSetup({
cache: true
});
jQuery(function($)
{
$("#" + TERMINAL_ID).terminal(function(input)
{
let args = input.split(" ");
if (!args.length || !args[0]) {
return;
}
const cmd = args[0].toLowerCase();
args.shift();
OnTerminalInput.call(this, cmd, args);
},
{
onInit: function(terminal)
{
gTerminal = terminal;
this.echo("Welcome to " + FormatColor("r", "sneakyevil.eu") + "!\n");
this.echo(FormatColorRaw("gy", "Type '" + FormatCommandExec("help") + "' for more details on the commands."), { raw: true });
},
greetings: "",
name: "",
prompt: "$> ",
keymap:
{
"TAB": function()
{
const cmd = this.get_command().toLowerCase();
if (!cmd) {
return false;
}
for (const command of gCmds.List)
{
if (command.n.startsWith(cmd))
{
this.set_command(command.n);
break;
}
}
return false;
}
}
});
});