User:Zocky/jsSandBox.js
From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Firefox/Mozilla/Safari: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Internet Explorer: press Ctrl-F5, Opera/Konqueror: press F5.
document.write('<link rel="stylesheet" type="text/css" href="' + 'http://en.wikipedia.org/w/index.php?title=User:Zocky/jsSandBox.css' + '&action=raw&ctype=text/css"/>'); var jsSandBoxAcActive=false; var jsSandBoxAcMatches=[]; var jsSandBoxAcNext=0; var jsSandBoxAcStart=0; var jsSandBoxCode; var jsSandBoxScroll; addOnloadHook (jsSandBoxInit); window.onerror=jsSandBoxOnError; function $(id){return document.getElementById(id)} function jsSandBoxOnError (a,b,c) { try { $('jsSandBoxScroll').innerHTML+= '<div class="jsSandBoxScrollError">' + '<b>' + a + '</b>: '+ b + '(' + c + ')' + '</div>'; $('jsSandBox').style.display='block'; } catch (e) { alert ('<b>' + a + '</b>: '+ b + '(' + c + ')'); } } function jsSandBoxInit() { $('content').innerHTML = '<div id="jsSandBox" style="display:block"><b>results</b> <a href="javascript:jsSandBoxClear()>(clear)</a></div>' + $('content').innerHTML; $('jsSandBox').innerHTML+='<div id="jsSandBoxScroll"></div>'; $('jsSandBox').innerHTML+='<b>code</b> <a href="javascript:jsSandBoxGo(jsSandBoxCode)">(eval)</a>'; $('jsSandBox').innerHTML+='<textarea id="jsSandBoxCode" rows="6"></textarea>'; $('jsSandBox').innerHTML+='<small>ctrl-enter to eval, ctr-space to autocomplete</small>'; jsSandBoxCode=$('jsSandBoxCode'); jsSandBoxScroll=$('jsSandBoxScroll'); $('jsSandBoxCode').addEventListener('keyup',jsSandBoxKey,true); $('t-specialpages').parentNode.innerHTML+='<li><a href="javascript:jsSandBoxToggle()">jsSandBox</a></li>'; } function jsSandBoxToggle() { $('jsSandBox').style.display = $('jsSandBox').style.display=='none' ? 'block' : 'none'; } function jsSandBoxClear() { jsSandBoxScroll.innerHTML=''; } function jsSandBoxGo(target) { $('jsSandBox').style.display='block'; if (target.selectionStart!=target.selectionEnd) { code=target.value.substring(target.selectionStart,target.selectionEnd); } else { code=target.value; } var res = '<div class="jsSandBoxScrollCode" onclick="jsSandBoxRecall(this)">' + code + '</div>'; try { res += '<div class="jsSandBoxScrollResult">' + eval(code) + '</div>'; } catch (err) { res += '<div class="jsSandBoxScrollError">' + '<b>' + err.name + '</b>: ' + err.message + '</div>'; } jsSandBoxScroll.innerHTML +='<div class="jsSandBoxScrollItem">' + res + '</div>'; jsSandBoxScroll.scrollTop=1e12; target.focus(); } function jsSandBoxRecall(old) { jsSandBoxCode.value=old.innerHTML; jsSandBoxCode.focus(); } function jsSandBoxKey(e) { keynum = window.event ? e.keyCode : e.which ; target=e.target; if (e.ctrlKey && keynum==13) { jsSandBoxGo(target);} if (e.ctrlKey && keynum==32) { if (!jsSandBoxAcActive) { var find = target.value.substr(0,target.selectionStart).match(/\w[a-zA-Z0-9\.]*$/); var where=find[0].split(/\./); var thing = window; try { for (var i=0;i<where.length-1;i++) { if (where[i]){thing=thing[where[i]]} } what=where[i]; var j=0; jsSandBoxAcMatches=[]; for (var bit in thing) { if ( bit.substr(0,what.length) == what) { jsSandBoxAcMatches[j]=bit; j++; } } if (j>0) { jsSandBoxAcNext=0; jsSandBoxAcStart=target.selectionStart-what.length; jsSandBoxAcActive=true; } } catch(e){return}; } if(jsSandBoxAcActive) { var top=target.scrollTop; target.value = target.value.substr(0,jsSandBoxAcStart) + jsSandBoxAcMatches[jsSandBoxAcNext] + target.value.substr(target.selectionStart); target.selectionStart=jsSandBoxAcStart+jsSandBoxAcMatches[jsSandBoxAcNext].length; target.selectionEnd=target.selectionStart; jsSandBoxAcNext=(jsSandBoxAcNext+1) % jsSandBoxAcMatches.length; target.scrollTop=top; } } else jsSandBoxAcActive=false; } //<nowiki><pre>