网页Cmd模拟模块
function UselessCmd(){
this.Initialized= false;
this.binded= false;
this.bindFunction= console.log;
this.latestCommand= '';
this.initialize = function(w,title='UselessCmd命令行',favicon='https://scriptcat.org/favicon.ico'){
if(typeof w === 'undefined' || w.toString() !== "[object Window]"){
console.error('UselessCmd: Not a window');
return
}
this.w = w;
w.UselessCmd = this;
w.document.write(`
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta content="max-age=30" http-equiv="Cache-Control">
<link rel="icon" href="`+favicon+`">
<title>`+title+`</title>
<style>
#main{
width:98%;
height:98%;
overflow:auto;
margin:0px;
padding:10px;
background-color:black;
color:white;
}
p{
margin-top:5;
margin-bottom:5;
}
.resa{
display:inline-block;
}
.cmdp{
color:white;
width:80%;
background-color:black;
border:0px;
display:inline-block;
outline:none;
}
</style>
</head>
<body id="main">
<p>UselessCmd模块 v0.1.2 coded by unrival https://scriptcat.org/script-show-page/751</p>
<div id="input">
<a id="host" class="resa">
root>
</a>
<input id="command" type="text" class="cmdp" value="" >
<div style="height:40%;" onclick="window.document.getElementById('command').focus();">
</div>
</div>
</body>
</html>`);
w.addEventListener('keypress',function(e){
let evt = w.event || e;
if (evt.keyCode == 13) {
let value = w.document.getElementById('command').value;
w.document.getElementById('command').value = '';
w.UselessCmd.update('root> '+value);
w.UselessCmd.command(value);
return
}
});
this.Initialized = true;
}
this.command= function(command){
this.latestCommand = command;
if(this.binded){
this.bindFunction.apply(this,[command]);
}
}
this.update=function(info){
if(!this.Initialized){
console.error('UselessCmd: 请先初始化');
return
}
let commandElement = this.w.document.getElementById('command'),
value = commandElement.value,
p = this.w.document.createElement("p"),
main = this.w.document.getElementById('main');
p.innerHTML = info;
main.insertBefore(p,main.childNodes[main.childNodes.length-2]);
commandElement.value=value;
commandElement.scrollIntoView();
commandElement.focus();
}
this.getCommand=function(){
if(!this.Initialized){
console.error('UselessCmd: 请先初始化');
return false
}
let command = this.latestCommand;
this.latestCommand = false;
return command;
}
this.bind=function(def){
this.bindFunction = def;
this.binded = true;
}
this.unBind=function(){
this.bindFunction = console.log;
this.binded = false;
}
};