📄 masterapi.js
字号:
var MasterAPI = {
version: '0.1.1a',
author: 'Petko Petkov | pdp (architect)',
homepage: 'http://www.gnucitizen.org'};
MasterAPI.Shell = {};
MasterAPI.Shell.createJavaScriptShell = function (console) {
var self = this;
this.console = console;
this.scope = {__builtins__: {
dir: function (obj) {
var obj = obj?obj:self.scope;
var items = [];
for (item in obj)
items.push(item);
return items;
},
hist: function () {
return self.console.history;
},
print: function (obj) {
var type = typeof(obj);
if (type == 'object' && obj.constructor == Array) {
self.console.write("\n[");
for (index = 0; index < obj.length; index++) {
var prefix = ' ';
var suffix = ",\n";
if (index == 0)
prefix = '';
if (index == obj.length - 1)
suffix = '';
self.console.write(prefix + "'" + obj[index] + "'" + suffix);
}
self.console.write("]");
} else {
self.console.write("\n" + obj);
}
},
clear: function () {
self.console.clear();
},
reset: function () {
var builtins = self.scope.__builtins__;
self.scope = {__builtins__: builtins, __globals__: window, _: ''};
},
load: function (url) {
var script = document.createElement('script');
script.src = url;
script.type = 'text/javascript';
script.defer = true;
self.console.parentNode.insertBefore(script, console);
}
}, __globals__: window, _: ''};
this.oninput = function (input) {
try {
with (self.scope) with (self.scope.__builtins__) {
var obj = self.scope.eval(input + ';');
if (obj)
self.scope.__builtins__.print(obj);
self.scope['_'] = obj;
}
} catch (e) {
self.console.write("\n" + e);
}
self.console.write("\n>> ");
};
this.console.write(">> ");
this.console.oninput = this.oninput;
};
MasterAPI.Console = {};
MasterAPI.Console.createInteractiveConsole = function (textarea) {
var console = (textarea == undefined)?document.createElement('textarea'):textarea;
console.lastCaretPosition = 0;
console.history = [];
console.history.reset = function () {
this.position = (this.length == 0)?0:this.length;
};
console.history.up = function () {
if (this.length == 0)
return '';
this.position = (this.position + 1 >= this.length)?this.length - 1:this.position + 1;
return this[this.position];
};
console.history.down = function () {
if (this.length == 0)
return '';
this.position = (this.position <= 0)?0:this.position - 1;
return this[this.position];
};
console.history.reset();
console.clear = function () {
this.value = '';
this.lastCaretPosition = 0;
this.setCaretPosition(this.lastCaretPosition);
};
console.write = function (str) {
this.value += str;
this.lastCaretPosition = this.value.length;
this.scrollTop = this.scrollHeight;
};
console.getCaretPosition = function () {
if (document.selection) {
this.focus();
var sel = document.selection.createRange();
sel.moveStart('character', -this.value.length);
return sel.text.length;
} else if (this.selectionStart || this.selectionStart == '0') {
return this.selectionStart;
}
return 0;
};
console.setCaretPosition = function (pos) {
if(this.setSelectionRange) {
this.focus();
this.setSelectionRange(pos,pos);
} else if (this.createTextRange) {
var range = this.createTextRange();
range.collapse(true);
range.moveEnd('character', pos);
range.moveStart('character', pos);
range.select();
}
};
console.onfocus = function () {
this.setCaretPosition(this.lastCaretPosition);
};
console.onkeydown = function (e) {
if (!e) e = window.event;
if ((e.keyCode == 8 || e.keyCode == 37) && this.getCaretPosition() <= this.lastCaretPosition) {
return false;
} else if (!e.shiftKey && e.keyCode == 13) {
this.setCaretPosition(this.value.length);
var command = this.value.substring(this.lastCaretPosition, this.value.length);
console.oninput(command);
this.history.push(command);
this.history.reset();
this.scrollTop = this.scrollHeight;
return false;
} else if (e.keyCode == 38) {
this.value = this.value.substring(0, this.lastCaretPosition) + this.history.down();
this.scrollTop = this.scrollHeight;
return false;
} else if (e.keyCode == 40) {
this.value = this.value.substring(0, this.lastCaretPosition) + this.history.up();
this.scrollTop = this.scrollHeight;
return false;
}
return true;
};
console.oninput = function (input) {
};
return console;
};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -