Repository navigation
Expand file tree
/
Copy pathconsolejs.js
More file actions
75 lines (72 loc) · 2.31 KB
/
Copy pathconsolejs.js
File metadata and controls
75 lines (72 loc) · 2.31 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
"use strict";
var Consolejs = function (win, cb) {
var inp, inptxt, outwin, sub, form, winstyle, send, respond;
send = function (inpt, cb) { //private
// get value and escape some html
var inptc, pos;
inptc = inpt.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
//window.alert(inptc);
outwin.insertAdjacentHTML('beforeend',
"<span class=\"usertxt\">> "
+ inptc
+ "<br /></span>");
pos = outwin.scrollTop;
outwin.scrollTop = pos + outwin.clientHeight;
inp.value = '';
respond(inptc, cb);
};
respond = function (inp, cb) { //private
var pos, resp;
if (typeof cb !== 'undefined') {
resp = cb(inp);
} else {
resp = inp;
}
outwin.insertAdjacentHTML('beforeend',
"<span class=\"bottxt\">"
+ resp
+ "<br /></span>");
pos = outwin.scrollTop;
outwin.scrollTop = pos + outwin.clientHeight;
};
this.backdoor = function (way, inp, cb) { //public
if (way === 'send') {
send(inp, cb);
} else if (way === 'respond') {
respond(inp, cb);
}
};
winstyle = window.getComputedStyle(win);
outwin = document.createElement("div");
outwin.setAttribute("id", "outwin");
outwin.setAttribute("class", 'consolejs');
outwin.style.width = winstyle.getPropertyValue("width");
outwin.style.height = winstyle.getPropertyValue("height");
outwin.style.overflow = 'auto';
outwin.style.wordWrap = 'normal';
inp = document.createElement("input");
inp.setAttribute("id", 'inp');
inp.setAttribute("autofocus", 'true');
inp.setAttribute("autocomplete", 'off');
inp.setAttribute("class", 'consolejs');
inp.style.borderTopWidth = '1px';
inp.style.borderLeftWidth = '0px';
inp.style.borderRightWidth = '0px';
inp.style.width = outwin.style.width;
inp.style.overflow = 'auto';
inp.style.wordWrap = 'normal';
form = document.createElement('form');
form.setAttribute("onsubmit", 'return false;');
form.appendChild(inp);
//kill the existing content and replace with our own
respond(win.innerHTML);
win.innerHTML = '';
win.appendChild(outwin);
win.appendChild(form);
form.addEventListener('submit', function () { send(inp.value, cb); }, false);
return this;
};