-
Notifications
You must be signed in to change notification settings - Fork 9
/
console.js
323 lines (280 loc) · 9.35 KB
/
console.js
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
(function($){
$.wpConsole = function() {
var self = this;
self.queries = [];
self.counter = 0;
self.historyIndex = 0;
self.init = function() {
self.version = WP_CONSOLE_VERSION;
self.url = WP_CONSOLE_URL;
self.secret = WP_CONSOLE_SECRET;
self.root = WP_ROOT_PATH;
// create shell div
self.shell = $("<div id='shell'></div>").appendTo($("#wrapper"));
// listen for clicks on the shell
self.shell.click(function() {
self.shell.find("input.current").focus();
});
// listen for key presses (up, down, tab and ctrl+l)
$(document).keyup(function(e) {
var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
var input = self.shell.find("input.current:last");
switch (key) {
case 38: // up
var lastQuery = self.queries[self.historyIndex];
if (typeof lastQuery != "undefined") {
self.historyIndex--;
input.val(lastQuery).focus();
}
// no negative history allowed
if (self.historyIndex < 0) {
self.historyIndex = 0;
}
break;
case 40: // down
var nextQuery = self.queries[self.historyIndex+1];
if (typeof nextQuery != "undefined") {
self.historyIndex++;
input.val(nextQuery).focus();
} else {
// put it at the end
self.historyIndex = self.queries.length - 1;
input.val("");
}
break;
case 76: // l
if (e.ctrlKey) {
self.clear();
}
break;
case 65: // a
if (e.ctrlKey) {
input.setCursorPosition(0);
}
break;
case 69: // e
if (e.ctrlKey) {
input.setCursorPosition(input.val().length);
}
} // switch
});
$(document).keydown(function(e) {
var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
var input = self.shell.find("input.current:last");
switch (key) {
case 38:
case 40:
e.preventDefault();
break;
case 9: // tab
var lastval = input.val();
e.preventDefault(); // don't do browser default action
self.postJSON("complete", lastval, {
success: function(j) {
if (self.check(j)) {
if (j.length == 0) {
return;
} else if (j.length == 1) {
input.val(j).focus();
} else {
// print 3-column listing of array values
buffer_to_longest(j);
while (j.length > 0) {
var line = j.splice(0,3);
self.print(line.join(" "));
}
self.doPrompt();
self.shell.find("input.current:last").val(lastval);
}
}
}
});
break;
}
});
// reload the session stuff before getting started
self.reload();
self.about();
self.doPrompt();
}
self.doPrompt = function(prompt) {
self.counter++;
// default prompt to >> unless passed in as argument
prompt = (prompt) ? prompt : ">>";
// append prompt to shell
var $row = $("<div class='row' id='" + self.counter + "'></div>");
var $prompt = $("<span class='prompt'>" + prompt + "</span>");
var $form = $("<form></form>");
var $input = $("<input class='current' type='text' />");
$form.append( $input );
$row.append( $prompt ).append( $form );
self.shell.append( $row );
// set width and focus input
var input_width = self.shell.width() - 50;
$input.width( input_width ).focus();
// listen for submit
$form.submit(function(e) {
var val = $input.val();
e.preventDefault();
$input.removeClass("current");
// save in history and handle accordingly
self.historyIndex = self.queries.length;
self.queries[self.historyIndex] = val;
switch(val) {
case "clear": case "c":
self.clear();
break;
case "help": case "?":
self.print("\nWhat's New:\n" +
" Keyboard shortcuts: ctrl+l = clear, ctrl+a = goto start, ctrl+e = goto end\n" +
" Admin functions: add_user(), wp_delete_user(), and friends!\n" +
"\n" +
"Special Commands:\n" +
" clear (c) = clears the console output\n" +
" help (?) = prints this help text\n" +
" reload (r) = flushes all variables and partial statements\n" +
"\n" +
"Why are all my objects of type stdClass?\n" +
" Your class needs to implement the __set_state() static method to\n" +
" be restored properly. You should Google it.");
self.doPrompt();
break;
case "reload": case "r":
self.reload(true);
break;
default:
self.postJSON("query", val, {
success: function(j) {
// if result is not an error
if (self.check(j)) {
// print output and return value if they exist
if (typeof j.rval != "undefined") {
self.print("=> " + j.rval);
}
if (typeof j.output != "undefined") {
if (j.output == "partial") {
var p = "..";
self.print("");
} else {
self.print(j.output);
}
}
}
self.doPrompt((typeof p != "undefined") ? p : null);
},
error: function() {
self.error("Most likely syntax. Forget the semicolon? If not, try 'reload' and re-execute");
self.doPrompt();
}
});
} // end switch
}); // end form.submit
}
self.clear = function() {
self.$header.siblings().empty();
self.counter = 0;
self.doPrompt();
}
self.reload = function(show_message) {
var callbacks = {};
if (show_message === true) {
callbacks.success = function(j) {
self.print(j.output);
self.doPrompt();
}
}
return self.postJSON("reload", callbacks);
}
self.postJSON = function(page, value, callbacks, additional_data) {
var request = {
type: "POST",
dataType: "json",
data: {
"root": self.root
}
};
var key = null;
if (typeof(value) == "object") {
additional_data = callbacks;
callbacks = value;
}
if (callbacks) request = $.extend(request, callbacks);
if (additional_data) request = $.extend(request, additional_data);
switch(page) {
case "query":
key = "query";
break;
case "complete":
key = "partial";
break;
}
request.url = self.url + page + ".php";
if (key) {
request.data[key] = value;
request.data.signature = hex_hmac_sha1( self.secret, value );
} else {
request.data.reload = true;
}
return $.ajax(request);
}
self.about = function() {
self.$header = $("<div id='header'>" +
"WordPress Console [" + self.version + "] by " +
"<a target='_blank' href='http://jerodsanto.net'>Jerod Santo</a>" +
"</div>");
self.shell.append(self.$header);
}
self.print = function(string) {
// Using text() escapes HTML to output visible tags
var result = $("<pre></pre>").text(string);
self.shell.append( $("<div class='result'></div>").append(result) );
}
self.error = function(string) {
self.shell.append("<div class='err'>Error: " + string + "</div>");
}
self.check = function(json) {
// make sure json result is not an error
if (typeof json.error != "undefined") {
self.error(json.error);
return false;
} else {
return true;
}
}
// HELPER FUNCTIONS
function buffer_to_longest(array) {
var longest = array[0].length;
for (var i=1; i < array.length; i++) {
if (array[i].length > longest)
longest = array[i].length;
};
for (var i=0; i < array.length; i++) {
array[i] = pad(array[i],longest);
};
return array;
}
function pad(string,length) {
while (string.length < length) {
string += " ";
}
return string;
}
self.init();
}
// thanks to: http://stackoverflow.com/questions/499126/jquery-set-cursor-position-in-text-area
$.fn.setCursorPosition = function(pos) {
var $self = $(this);
if ($self.get(0).setSelectionRange) {
$self.get(0).setSelectionRange(pos, pos);
} else if ($self.get(0).createTextRange) {
var range = $self.get(0).createTextRange();
range.collapse(true);
range.moveEnd("character", pos);
range.moveStart("character", pos);
range.select();
}
}
$(function() {
window.wpConsole = new $.wpConsole();
});
})(jQuery);