Skip to content

Instantly share code, notes, and snippets.

@slackorama
Created June 21, 2011 20:58
Show Gist options
  • Save slackorama/1038885 to your computer and use it in GitHub Desktop.
Save slackorama/1038885 to your computer and use it in GitHub Desktop.
javascript:(function()%20{if%20(window.__JQUERY_CONSOLE__)%20{window.__JQUERY_CONSOLE__.toggle();}%20else%20{function%20init($)%20{function%20HistoryManager()%20{this.curr%20=%20-1;this.entries%20=%20[];};HistoryManager.prototype%20=%20{push:%20function(item)%20{if%20(this.entries.length%20&&%20this.entries[0]%20==%20item)%20return;if%20(item.match(/^\s*$/))%20return;this.entries.unshift(item);this.curr%20=%20-1;},scroll:%20function(direction)%20{var%20moveTo%20=%20this.curr%20+%20(direction%20==%20%27prev%27%20?%201%20:%20-1);if%20(moveTo%20>=%200%20&&%20moveTo%20<%20this.entries.length)%20{this.curr%20=%20moveTo;return%20this.entries[this.curr];}%20else%20if%20(moveTo%20==%20-1)%20{this.curr%20=%20moveTo;return%20%27%27;}%20else%20{return%20null;}}};var%20context%20=%20{},history%20=%20new%20HistoryManager(),$drag%20=%20$(%27<div/>%27).css({backgroundColor:%20%27#e0e0e0%27,%20border:%20%271px%20solid%20#a0a0a0%27,%20fontSize:%20%2711px%27,%20fontFamily:%20%27sans-serif%27,%20lineHeight:%201,%20padding:%20%275px%27,%20marginBottom:%20%277px%27,%20cursor:%20%27pointer%27,%20textAlign:%20%27right%27}).html($(%27<a/>%27).text(%27[X]%27).click(function()%20{%20window.__JQUERY_CONSOLE__.hide();%20})),$log%20=%20$(%27<div/>%27).css({fontSize:%20%2711px%27,%20fontFamily:%20%27monospace%27,%20color:%20%27white%27,%20marginBottom:%20%277px%27,%20overflow:%20%27auto%27,%20height:%20%27120px%27,%20border:%20%271px%20solid%20#a0a0a0%27,%20padding:%20%275px%27,%20textAlign:%20%27left%27}),$input%20=%20$(%27<input%20type=%22text%22%20/>%27).css({border:%20%271px%20solid%20#a0a0a0%27,%20padding:%20%273px%27,%20width:%20%27444px%27,%20fontSize:%20%2711px%27}),$dummy%20=%20$(%27<div/>%27);function%20evalIt(cmd)%20{window.__JQUERY_CONSOLE__.appendTo($dummy);var%20result;try%20{if%20(cmd%20==%20%27reset!%27)%20{context%20=%20{};result%20=%20true;}%20else%20{if%20(cmd.match(/^\$%20/))%20cmd%20=%20%22$(%27%22%20+%20cmd.substring(2)%20+%20%22%27);%22;else%20cmd%20=%20%27(%27%20+%20cmd%20+%20%27)%27;var%20result%20=%20eval(cmd);}}%20finally%20{window.__JQUERY_CONSOLE__.appendTo(document.body);$input[0].focus();}if%20(typeof%20result%20!=%20%27undefined%27)%20{window._%20=%20result;if%20(typeof%20result%20==%20%27object%27%20&&%20result.selector)window._$%20=%20result;}return%20result;};function%20format(value)%20{if%20(value%20===%20null)%20{return%20%27null%27;}%20else%20if%20(typeof%20value%20==%20%27undefined%27)%20{return%20%27undefined%27;}%20else%20if%20(typeof%20value%20==%20%27string%27)%20{return%20%27%22%27%20+%20value.replace(%27%22%27,%20%27\\%22%27)%20+%20%27%22%27;}%20else%20if%20(typeof%20value%20==%20%27function%27%20||typeof%20value%20==%20%27number%27%20||value%20instanceof%20RegExp%20||value%20===%20true%20||%20value%20===%20false)%20{return%20value.toString();}%20else%20if%20(value.selector)%20{return%20%22<jQuery%20selector:%20%27%22%20+%20value.selector%20+%20%22%27%20length:%20%22%20+%20value.length%20+%20%22>%22;}%20else%20if%20(value%20instanceof%20Array)%20{return%20%27[%27%20+%20$.map(value,%20format).join(%27,%20%27)%20+%20%27]%27;}%20else%20if%20(value%20instanceof%20Date)%20{return%20%22@%22%20+%20value.getFullYear()%20+%22-%22%20+%20value.getMonth()%20+%22-%22%20+%20value.getDate()%20+%22T%22%20+%20value.getHours()%20+%22:%22%20+%20value.getMinutes()%20+%22:%22%20+%20value.getSeconds()%20+%22.%22%20+%20value.getMilliseconds();}%20else%20{var%20o%20=%20[];for%20(var%20k%20in%20value)%20o.push(k%20+%20%27:%20%27%20+%20format(value[k]));return%20%27{%27%20+%20o.join(%27,%20%27)%20+%20%27}%27;}};function%20append(text,%20color)%20{$log.append($(%27<div/>%27).css({%27color%27:%20color%20||%20%27black%27,%20margin:%200,%20padding:%200}).text(text));$log[0].scrollTop%20=%20$log[0].scrollHeight;};var%20dragging%20=%20null;$drag.mousedown(function(evt)%20{dragging%20=%20[evt.pageX%20-%20$container[0].offsetLeft,evt.pageY%20-%20$container[0].offsetTop];}).mouseup(function()%20{dragging%20=%20null;});$(document).mousemove(function(evt)%20{if%20(dragging)$container.css({left:%20evt.pageX%20-%20dragging[0],%20top:%20evt.pageY%20-%20dragging[1]});});$input.keydown(function(evt)%20{var%20valid%20=%20{38:%20%27prev%27,%2040:%20%27next%27};if%20(evt.keyCode%20in%20valid)%20{var%20curr%20=%20history.scroll(valid[evt.keyCode]);if%20(curr%20!==%20null)%20$input.val(curr);}});$input.keypress(function(evt)%20{if%20(evt.keyCode%20==%2013)%20{try%20{var%20cmd%20=%20this.value;append(%27>%20%27%20+%20cmd);append(format(evalIt.call(context,%20cmd)));}%20catch%20(e)%20{append(e.toString(),%20%27#ff0000%27);}%20finally%20{history.push(cmd);this.value%20=%20%27%27;}}});var%20pos%20=%20($.browser.msie%20&&%20$.browser.version%20<%207)%20?%20%27absolute%27%20:%20%27fixed%27;var%20$container%20=%20$(%27<div/>%27).css({backgroundColor:%20%27white%27,%20padding:%20%277px%27,%20position:%20pos,%20opacity:%200.9,top:%20%2710px%27,%20right:%20%2710px%27,%20width:%20%27450px%27,%20border:%20%271px%20solid%20black%27,zIndex:%2099999}).appendTo(document.body);$container.append($drag).append($log).append($input);$input[0].focus();append(%27jQuery%20console%20initialised!%27,%20%27green%27);append(%27(using%20jQuery%20version%20%27%20+%20$.fn.jquery%20+%20%27)%27);window.__JQUERY_CONSOLE__%20=%20$container;};if%20(typeof%20jQuery%20==%20%27undefined%27%20||%20!jQuery.fn.jquery.match(/^1\.3/))%20{var%20e%20=%20document.createElement(%27script%27),%20jq%20=%20null;e.onload%20=%20function()%20{%20jq%20=%20jQuery;%20jQuery.noConflict(true);%20init(jq);%20};e.setAttribute(%27type%27,%20%27text/javascript%27);e.setAttribute(%27src%27,%20%27http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js%27);document.body.appendChild(e);}%20else%20{init(jQuery);}}})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment