5 _win, // a top-level context
14 var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"].
15 getService(Components.interfaces.nsIWindowMediator);
16 var enumerator = wm.getEnumerator('eg_menu');
17 while(enumerator.hasMoreElements()) {
18 targetwindow = enumerator.getNext();
19 list.push(targetwindow);
24 function get_tab(a,b) {
29 if (typeof b == 'undefined') {
37 var tabs = win.document.getElementById('main_tabs');
38 var panels = win.document.getElementById('main_panels');
39 return { 'name' : tabs.childNodes[idx].getAttribute('label'), 'content' : panels.childNodes[idx].firstChild.contentWindow };
44 _in.blur(); // Needed for Mozilla to scroll correctly.
50 _in = document.getElementById("input");
51 _out = document.getElementById("output");
55 if (opener && !opener.closed)
57 println("Using bookmarklet version of shell: commands will run in opener's context.", "message");
63 recalculateInputHeight();
70 _win.print = shellCommands.print;
74 // Unless the user is selected something, refocus the textbox.
75 // (requested by caillon, brendan, asa)
76 function keepFocusInTextbox(e)
78 var g = e.srcElement ? e.srcElement : e.target; // IE vs. standard
82 var t = g.tagName.toUpperCase();
83 if (t=="A" || t=="INPUT")
86 if (window.getSelection) {
88 if (String(window.getSelection()))
91 else if (document.getSelection) {
93 if (document.getSelection())
98 if ( document.selection.createRange().text )
105 function inputKeydown(e) {
106 // Use onkeydown because IE doesn't support onkeypress for arrow keys
108 //alert(e.keyCode + " ^ " + e.keycode);
110 if (e.shiftKey && e.keyCode == 13) { // shift-enter
111 // don't do anything; allow the shift-enter to insert a line break as normal
112 } else if (e.keyCode == 13) { // enter
113 // execute the input on enter
114 try { go(); } catch(er) { alert(er); };
115 setTimeout(function() { _in.value = ""; }, 0); // can't preventDefault on input, so clear it later
116 } else if (e.keyCode == 38) { // up
117 // go up in history if at top or ctrl-up
118 if (e.ctrlKey || caretInFirstLine(_in))
120 } else if (e.keyCode == 40) { // down
121 // go down in history if at end or ctrl-down
122 if (e.ctrlKey || caretInLastLine(_in))
124 } else if (e.keyCode == 9) { // tab
126 setTimeout(function() { refocus(); }, 0); // refocus because tab was hit
129 setTimeout(recalculateInputHeight, 0);
134 function caretInFirstLine(textbox)
136 // IE doesn't support selectionStart/selectionEnd
137 if (textbox.selectionStart == undefined)
140 var firstLineBreak = textbox.value.indexOf("\n");
142 return ((firstLineBreak == -1) || (textbox.selectionStart <= firstLineBreak));
145 function caretInLastLine(textbox)
147 // IE doesn't support selectionStart/selectionEnd
148 if (textbox.selectionEnd == undefined)
151 var lastLineBreak = textbox.value.lastIndexOf("\n");
153 return (textbox.selectionEnd > lastLineBreak);
156 function recalculateInputHeight()
158 var rows = _in.value.split(/\n/).length
159 + 1 // prevent scrollbar flickering in Mozilla
160 + (window.opera ? 1 : 0); // leave room for scrollbar in Opera
162 if (_in.rows != rows) // without this check, it is impossible to select text in Opera 7.60 or Opera 8.0.
166 function println(s, type)
170 var newdiv = document.createElement("div");
171 newdiv.appendChild(document.createTextNode(s));
172 newdiv.className = type;
173 _out.appendChild(newdiv);
178 function printWithRunin(h, s, type)
180 var div = println(s, type);
181 var head = document.createElement("strong");
182 head.appendChild(document.createTextNode(h + ": "));
183 div.insertBefore(head, div.firstChild);
189 load : function load(url)
191 var s = _win.document.createElement("script");
192 s.type = "text/javascript";
194 _win.document.getElementsByTagName("head")[0].appendChild(s);
195 println("Loading " + url + "...", "message");
198 clear : function clear()
200 var CHILDREN_TO_PRESERVE = 3;
201 while (_out.childNodes[CHILDREN_TO_PRESERVE])
202 _out.removeChild(_out.childNodes[CHILDREN_TO_PRESERVE]);
205 print : function print(s) { println(s, "print"); },
207 // the normal function, "print", shouldn't return a value
208 // (suggested by brendan; later noticed it was a problem when showing others)
211 shellCommands.print(s); // need to specify shellCommands so it doesn't try window.print()!
215 props : function props(e, onePerLine)
218 println("props called with null argument", "error");
222 if (e === undefined) {
223 println("props called with undefined argument", "error");
227 var ns = ["Methods", "Fields", "Unreachables"];
228 var as = [[], [], []]; // array of (empty) arrays of arrays!
229 var p, j, i; // loop variables, several used multiple times
233 for (p = e; p; p = p.__proto__)
235 for (i=0; i<ns.length; ++i)
236 as[i][protoLevels] = [];
242 // Shortcoming: doesn't check that VALUES are the same in object and prototype.
247 for (p = e; p && (a in p); p = p.__proto__)
250 catch(er) { protoLevel = 0; } // "in" operator throws when param to props() is a string
255 if ((typeof e[a]) == "function")
258 catch (er) { type = 2; }
260 as[type][protoLevel].push(a);
263 function times(s, n) { return n ? s + times(s, n-1) : ""; }
265 for (j=0; j<protoLevels; ++j)
266 for (i=0;i<ns.length;++i)
269 ns[i] + times(" of prototype", j),
270 (onePerLine ? "\n\n" : "") + as[i][j].sort().join(onePerLine ? "\n" : ", ") + (onePerLine ? "\n\n" : ""),
275 blink : function blink(node)
277 if (!node) throw("blink: argument is null or undefined.");
278 if (node.nodeType == null) throw("blink: argument must be a node.");
279 if (node.nodeType == 3) throw("blink: argument must not be a text node");
280 if (node.documentElement) throw("blink: argument must not be the document object");
282 function setOutline(o) {
284 if (node.style.outline != node.style.bogusProperty) {
285 // browser supports outline (Firefox 1.1 and newer, CSS3, Opera 8).
286 node.style.outline = o;
288 else if (node.style.MozOutline != node.style.bogusProperty) {
289 // browser supports MozOutline (Firefox 1.0.x and older)
290 node.style.MozOutline = o;
293 // browser only supports border (IE). border is a fallback because it moves things around.
294 node.style.border = o;
299 function focusIt(a) {
305 if (node.ownerDocument) {
306 var windowToFocusNow = (node.ownerDocument.defaultView || node.ownerDocument.parentWindow); // Moz vs. IE
307 if (windowToFocusNow)
308 setTimeout(focusIt(windowToFocusNow.top), 0);
312 setTimeout(setOutline((i%2)?'3px solid red':'none'), i*100);
314 setTimeout(focusIt(window), 800);
315 setTimeout(focusIt(_in), 810);
318 scope : function scope(sc)
322 println("Scope is now " + sc + ". If a variable is not found in this scope, window will also be searched. New variables will still go on window.", "message");
325 mathHelp : function mathHelp()
327 printWithRunin("Math constants", "E, LN2, LN10, LOG2E, LOG10E, PI, SQRT1_2, SQRT2", "propList");
328 printWithRunin("Math methods", "abs, acos, asin, atan, atan2, ceil, cos, exp, floor, log, max, min, pow, random, round, sin, sqrt, tan", "propList");
337 // histList[0] = first command entered, [1] = second, etc.
338 // type something, press up --> thing typed is now in "limbo"
339 // (last item in histList) and should be reachable by pressing
342 var L = histList.length;
351 // Save this entry in case the user hits the down key.
352 histList[histPos] = _in.value;
358 // Use a timeout to prevent up from moving cursor within new text
359 // Set to nothing first for the same reason
363 _in.value = histList[histPos];
364 var caretPos = _in.value.length;
365 if (_in.setSelectionRange)
366 _in.setSelectionRange(caretPos, caretPos);
377 _in.value = histList[histPos];
379 else if (histPos == L-1)
381 // Already on the current entry: clear but save
384 histList[histPos] = _in.value;
392 function tabcomplete()
395 * Working backwards from s[from], find the spot
396 * where this expression starts. It will scan
397 * until it hits a mismatched ( or a space,
398 * but it skips over quoted strings.
399 * If stopAtDot is true, stop at a '.'
401 function findbeginning(s, from, stopAtDot)
404 * Complicated function.
406 * Return true if s[i] == q BUT ONLY IF
407 * s[i-1] is not a backslash.
409 function equalButNotEscaped(s,i,q)
411 if(s.charAt(i) != q) // not equal go no further
414 if(i==0) // beginning of string
417 if(s.charAt(i-1) == '\\') // escaped?
425 for(i=from; i>=0; i--)
427 if(s.charAt(i) == ' ')
430 if(stopAtDot && s.charAt(i) == '.')
433 if(s.charAt(i) == ')')
435 else if(s.charAt(i) == '(')
441 // skip quoted strings
442 if(s.charAt(i) == '\'' || s.charAt(i) == '\"')
444 //dump("skipping quoted chars: ");
445 var quot = s.charAt(i);
447 while(i >= 0 && !equalButNotEscaped(s,i,quot)) {
457 // XXX should be used more consistently (instead of using selectionStart/selectionEnd throughout code)
458 // XXX doesn't work in IE, even though it contains IE-specific code
459 function getcaretpos(inp)
461 if(inp.selectionEnd != null)
462 return inp.selectionEnd;
464 if(inp.createTextRange)
466 var docrange = _win.Shell.document.selection.createRange();
467 var inprange = inp.createTextRange();
468 if (inprange.setEndPoint)
470 inprange.setEndPoint('EndToStart', docrange);
471 return inprange.text.length;
475 return inp.value.length; // sucks, punt
478 function setselectionto(inp,pos)
480 if(inp.selectionStart) {
481 inp.selectionStart = inp.selectionEnd = pos;
483 else if(inp.createTextRange) {
484 var docrange = _win.Shell.document.selection.createRange();
485 var inprange = inp.createTextRange();
486 inprange.move('character',pos);
492 if(_win.Shell.document.getSelection())
493 _win.Shell.document.getSelection() = "";
497 // get position of cursor within the input box
498 var caret = getcaretpos(_in);
502 var dotpos, spacepos, complete, obj;
503 //dump("caret pos: " + caret + "\n");
504 // see if there's a dot before here
505 dotpos = findbeginning(_in.value, caret-1, true);
506 //dump("dot pos: " + dotpos + "\n");
507 if(dotpos == -1 || _in.value.charAt(dotpos) != '.') {
509 //dump("changed dot pos: " + dotpos + "\n");
512 // look backwards for a non-variable-name character
513 spacepos = findbeginning(_in.value, dotpos-1, false);
514 //dump("space pos: " + spacepos + "\n");
515 // get the object we're trying to complete on
516 if(spacepos == dotpos || spacepos+1 == dotpos || dotpos == caret)
518 // try completing function args
519 if(_in.value.charAt(dotpos) == '(' ||
520 (_in.value.charAt(spacepos) == '(' && (spacepos+1) == dotpos))
523 var from = (_in.value.charAt(dotpos) == '(') ? dotpos : spacepos;
524 spacepos = findbeginning(_in.value, from-1, false);
526 fname = _in.value.substr(spacepos+1,from-(spacepos+1));
527 //dump("fname: " + fname + "\n");
529 with(_win.Shell._scope)
531 with(Shell.shellCommands)
535 //dump('fn is not a valid object\n');
538 if(fn == undefined) {
539 //dump('fn is undefined');
542 if(fn instanceof Function)
544 // Print function definition, including argument names, but not function body
545 if(!fn.toString().match(/function .+?\(\) +\{\n +\[native code\]\n\}/))
546 println(fn.toString().match(/function .+?\(.*?\)/), "tabcomplete");
556 var objname = _in.value.substr(spacepos+1,dotpos-(spacepos+1));
557 //dump("objname: |" + objname + "|\n");
559 with(_win.Shell._scope)
567 if(obj == undefined) {
568 // sometimes this is tabcomplete's fault, so don't print it :(
569 // e.g. completing from "print(document.getElements"
570 // println("Can't complete from null or undefined expression " + objname, "error");
574 //dump("obj: " + obj + "\n");
575 // get the thing we're trying to complete
578 if(spacepos+1 == dotpos || spacepos == dotpos)
580 // nothing to complete
581 //dump("nothing to complete\n");
585 complete = _in.value.substr(spacepos+1,dotpos-(spacepos+1));
588 complete = _in.value.substr(dotpos+1,caret-(dotpos+1));
590 //dump("complete: " + complete + "\n");
591 // ok, now look at all the props/methods of this obj
592 // and find ones starting with 'complete'
594 var bestmatch = null;
598 //XXX: making it lowercase could help some cases,
599 // but screws up my general logic.
600 if(a.substr(0,complete.length) == complete) {
602 ////dump("match: " + a + "\n");
603 // if no best match, this is the best match
604 if(bestmatch == null)
609 // the best match is the longest common string
610 function min(a,b){ return ((a<b)?a:b); }
612 for(i=0; i< min(bestmatch.length, a.length); i++)
614 if(bestmatch.charAt(i) != a.charAt(i))
617 bestmatch = bestmatch.substr(0,i);
618 ////dump("bestmatch len: " + i + "\n");
620 ////dump("bestmatch: " + bestmatch + "\n");
623 bestmatch = (bestmatch || "");
624 ////dump("matches: " + matches + "\n");
625 var objAndComplete = (objname || obj) + "." + bestmatch;
626 //dump("matches.length: " + matches.length + ", tooManyMatches: " + tooManyMatches + ", objAndComplete: " + objAndComplete + "\n");
627 if(matches.length > 1 && (tooManyMatches == objAndComplete || matches.length <= 10)) {
629 printWithRunin("Matches: ", matches.join(', '), "tabcomplete");
630 tooManyMatches = null;
632 else if(matches.length > 10)
634 println(matches.length + " matches. Press tab again to see them all", "tabcomplete");
635 tooManyMatches = objAndComplete;
638 tooManyMatches = null;
643 if(dotpos == caret) {
649 _in.value = _in.value.substr(0, sstart)
651 + _in.value.substr(caret);
652 setselectionto(_in,caret + (bestmatch.length - complete.length));
657 function printQuestion(q)
662 function printAnswer(a)
664 if (a !== undefined) {
665 println(a, "normalOutput");
666 shellCommands.ans = a;
670 function printError(er)
672 var lineNumberString;
674 lastError = er; // for debugging the shell
677 // lineNumberString should not be "", to avoid a very wacky bug in IE 6.
678 lineNumberString = (er.lineNumber != undefined) ? (" on line " + er.lineNumber + ": ") : ": ";
679 println(er.name + lineNumberString + er.message, "error"); // Because IE doesn't have error.toString.
682 println(er, "error"); // Because security errors in Moz /only/ have toString.
687 _in.value = question = s ? s : _in.value;
692 histList[histList.length-1] = question;
693 histList[histList.length] = "";
694 histPos = histList.length - 1;
696 // Unfortunately, this has to happen *before* the JavaScript is run, so that
697 // print() output will go in the right place.
699 recalculateInputHeight();
700 printQuestion(question);
703 printError("Target window has been closed.");
707 try { ("Shell" in _win) }
709 printError("The JavaScript Shell cannot access variables in the target window. The most likely reason is that the target window now has a different page loaded and that page has a different hostname than the original page.");
713 if (!("Shell" in _win))
714 initTarget(); // silent
716 // Evaluate Shell.question using _win's eval (this is why eval isn't in the |with|, IIRC).
717 _win.location.href = "javascript:try{ Shell.printAnswer(eval('with(Shell._scope) with(Shell.shellCommands) {' + Shell.question + String.fromCharCode(10) + '}')); } catch(er) { Shell.printError(er); }; setTimeout(Shell.refocus, 0); void 0";