201 lines
6.7 KiB
JavaScript
201 lines
6.7 KiB
JavaScript
|
// TERM API BY GZOD01 (FROM CYBERTERMHACK BY GZOD01)
|
||
|
const clog = console.log
|
||
|
const cwarn = console.warn
|
||
|
const cerror = console.error
|
||
|
const cclear = console.clear
|
||
|
console.log = function(str){if(devmodeenabled){termshowhtml('<span id=console style="background-color:grey; color: black">JS-LOG: '+str+' </span>');}clog(str)}
|
||
|
console.warn = function(str){if(devmodeenabled){termshowhtml('<span id= console style="background-color:yellow; color:black">JS-WARN: '+str+' </span>')}cwarn(str)}
|
||
|
console.error = function(str){if(devmodeenabled){termshowhtml('<span id= consolestyle="background-color:red; color:white">JS-ERROR: '+str+' </span>')}cerror(str)}
|
||
|
console.clear = function(){if(devmodeenabled){let l = document.querySelectorAll('#console'); for(let i=0; i<l.length; i++){l[i].remove()}}cclear()}
|
||
|
devmodeenabled = false
|
||
|
// the variable to check if the input is complete (if the user press enter)
|
||
|
var inputcomplete = false
|
||
|
|
||
|
// function to choose a random int (random.randint() in python)
|
||
|
function randint(min, max){
|
||
|
return Math.floor(Math.random() * (max - min + 1) ) + min;
|
||
|
}
|
||
|
|
||
|
// START OF MATRIX CODE
|
||
|
// function to stop the matrix (delete the canvas and recreate it)
|
||
|
function stopmatrix(){
|
||
|
document.getElementById('matrixcanvas').remove()
|
||
|
let nc = document.createElement('canvas')
|
||
|
nc.setAttribute('id','matrixcanvas')
|
||
|
nc.style.width = '100%'
|
||
|
nc.style.height ='0%'
|
||
|
document.body.appendChild(nc)
|
||
|
}
|
||
|
//function to start the matrix (start the dropdown of letters etc.)
|
||
|
function matrix(color){
|
||
|
let tcolor = ''
|
||
|
if(color==='cyan'){
|
||
|
tcolor ='#0ff'
|
||
|
}
|
||
|
else if(color==='blue'){
|
||
|
tcolor='#00f'
|
||
|
}
|
||
|
else if(color==='magenta'){
|
||
|
tcolor='#f0f'
|
||
|
}
|
||
|
else if(color==='red'){
|
||
|
tcolor='#f00'
|
||
|
}
|
||
|
else if(color==='green'){
|
||
|
tcolor='#0f0'
|
||
|
}
|
||
|
else if(color==='yellow'){
|
||
|
tcolor='#ff0'
|
||
|
}
|
||
|
else if(color==='white'){
|
||
|
tcolor='#fff'
|
||
|
}
|
||
|
else if(color==='purple'){
|
||
|
tcolor='#303'
|
||
|
}
|
||
|
else{
|
||
|
tcolor= '#0f0'
|
||
|
}
|
||
|
var canvas = document.getElementById('matrixcanvas'),
|
||
|
ctx = canvas.getContext('2d');
|
||
|
|
||
|
canvas.style.height = '100%'
|
||
|
canvas.width = window.innerWidth;
|
||
|
canvas.height = window.innerHeight;
|
||
|
var letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890&éèçà@æ€ŧøþ@ßðđŋłĸħłµł»¢µ$£ù%^¨';
|
||
|
letters = letters.split('');
|
||
|
|
||
|
|
||
|
var fontSize = 10,
|
||
|
columns = canvas.width / fontSize;
|
||
|
|
||
|
|
||
|
var drops = [];
|
||
|
for (var i = 0; i < columns; i++) {
|
||
|
drops[i] = 1;
|
||
|
}
|
||
|
|
||
|
|
||
|
function draw() {
|
||
|
ctx.fillStyle = 'rgba(0, 0, 0, .1)';
|
||
|
ctx.fillRect(0, 0, canvas.width, canvas.height);
|
||
|
for (var i = 0; i < drops.length; i++) {
|
||
|
var text = letters[Math.floor(Math.random() * letters.length)];
|
||
|
ctx.fillStyle = tcolor;
|
||
|
ctx.fillText(text, i * fontSize, drops[i] * fontSize);
|
||
|
drops[i]++;
|
||
|
if (drops[i] * fontSize > canvas.height && Math.random() > .95) {
|
||
|
drops[i] = 0;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
setInterval(draw, 33);
|
||
|
}
|
||
|
// END OF MATRIX CODE
|
||
|
|
||
|
// function like the time.sleep in python
|
||
|
function sleep(s) {
|
||
|
let ms = s*1000
|
||
|
return new Promise(resolve => setTimeout(resolve, ms));
|
||
|
}
|
||
|
// function like random.choice() in python
|
||
|
function randchoice(list){
|
||
|
console.log(list[randint(0,list.length-1)])
|
||
|
return list[randint(0,list.length-1)]
|
||
|
}
|
||
|
|
||
|
// TODO : var historic = []
|
||
|
|
||
|
// lastx = the last input do (the input you get when you press up arrow) to replace with an historic
|
||
|
var lastx = ""
|
||
|
|
||
|
// the function to execute when up arrow pressed
|
||
|
function historic(){
|
||
|
// TODO : terminpt.value=historic[historic.lengh-1-index]
|
||
|
document.getElementById('terminpt').value = lastx // to replace with an historic (line above) , set the input value (content) to the last input executed
|
||
|
}
|
||
|
// when a key is down
|
||
|
onkeydown = function(e){
|
||
|
// if the key is the tab key (keyCode 9)
|
||
|
if(e.keyCode==9){
|
||
|
// execute the autocomplete function
|
||
|
autocomplete()
|
||
|
}
|
||
|
}
|
||
|
// when a key is up
|
||
|
onkeyup = function(e){
|
||
|
// if the key is enter key (keyCode 13)
|
||
|
if(e.keyCode==13){
|
||
|
// set the inputcomplete variable to true to inform the terminput function that the user press enter
|
||
|
inputcomplete = true
|
||
|
}
|
||
|
// if the input is up arrow key (keyCode 38)
|
||
|
else if(e.keyCode == 38){
|
||
|
// execute the historic function
|
||
|
historic()
|
||
|
}
|
||
|
}
|
||
|
// function like the print() function in python, add the text to the #termcontent div
|
||
|
function termshowhtml(str){
|
||
|
let tc = document.getElementById('termcontent')
|
||
|
let nchild = document.createElement('div')
|
||
|
nchild.innerHTML = str
|
||
|
tc.appendChild(nchild)
|
||
|
document.getElementById('termcontent').scrollTo(0,document.getElementById('termcontent').scrollHeight)
|
||
|
}
|
||
|
function termshow(str){
|
||
|
str = str.replace('<','<')
|
||
|
str= str.replace('>','>')
|
||
|
str= str.replace('&infg&','<')
|
||
|
str = str.replace('&supd&','>')
|
||
|
termshowhtml(str.replace('\n','<br>'))
|
||
|
}
|
||
|
// the input() function of python, check if the input is complete (see the inputcomplete variable and the onkeyup (e.keyCode == 13) execution
|
||
|
function terminput(str){
|
||
|
let tiname = document.getElementById('inptname')
|
||
|
tiname.innerHTML = str
|
||
|
let inpt = document.getElementById('terminpt')
|
||
|
if(inputcomplete){
|
||
|
console.log(inpt.value)
|
||
|
inputcomplete=false
|
||
|
let resultat = inpt.value
|
||
|
inpt.value = ''
|
||
|
return resultat
|
||
|
}
|
||
|
else{
|
||
|
//return 'ERR' if the input isn't complete
|
||
|
return 'ERR'
|
||
|
}
|
||
|
}
|
||
|
// replace this list with the list of your commands
|
||
|
var list_of_command = ['ls','ssh','pbk','exit','theme','matrix','hack','attack','save','import','help','exit']
|
||
|
// autocomplete when tab press show all the command starting with the value in your input
|
||
|
function autocomplete(){
|
||
|
let inptval = document.getElementById('terminpt').value
|
||
|
termshow('List of available commands starting with " '+inptval+' " :')
|
||
|
for(let i = 0; i<list_of_command.length; i++){
|
||
|
if(list_of_command[i].startsWith(inptval)){
|
||
|
termshow('- '+list_of_command[i])
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
// x= the current input
|
||
|
var x = ""
|
||
|
|
||
|
// START OF FUNCTIONS TO USE WITHOUT INTERACT WITH THE INTERFACE
|
||
|
// Send the current input (function to set the inputcomplete as true)
|
||
|
function sendinput(){inputcomplete=true}
|
||
|
// setinput(str) function to set the value of the input from function
|
||
|
function setinput(str){document.getElementById('terminpt').value=str}
|
||
|
// execcommand(str) set input and send input
|
||
|
function execcommand(str){setinput(str); sendinput()}
|
||
|
// last() historic and sendinput
|
||
|
function last(){historic(); sendinput()}
|
||
|
// autoc setinput, autocomplete
|
||
|
function autoc(str){setinput(str); autocomplete();}
|
||
|
// END OF FUNCTIONS TO USE WITHOUT INTERACT WITH THE INTERFACE
|
||
|
// END OF THE API CODE
|
||
|
|