Jump to content

User:Zocky/jsSandBox.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
document.write('<'+'script type="text/javascript" src="'
                + 'http://en.wikipedia.org/w/index.php?title=User:Zocky/Tools.js'
                + '&action=raw&ctype=text/javascript&dontcountme=s"></'+'script>');
 
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&dontcountme=s">');

var jsSandBoxAcActive=false;
var jsSandBoxAcMatches=[];
var jsSandBoxAcNext=0;
var jsSandBoxAcStart=0; 
var jsSandBoxCode;
var jsSandBoxScroll;

$ (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()
{
  $('t-specialpages').parentNode.innerHTML+='<li><a href="javascript:jsSandBoxToggle()">jsSandBox</a></li>';
  $('content').innerHTML = '<div id="jsSandBox" style="display:none"></div>' + $('content').innerHTML;

  s='<table id="jsSandBoxTable"><tr><td id="jsSandBoxLeft"><b>results</b> <a href="javascript:jsSandBoxClear()">(clear)</a>'
  + '<div id="jsSandBoxScroll"></div>'
  + '<b>code</b> <a href="javascript:jsSandBoxGo(jsSandBoxCode)">(eval)</a>'
  + '<textarea id="jsSandBoxCode" rows="6"></textarea>'
  + '<small>ctrl-enter to eval, ctr-space or tab to autocomplete</small></td>'
  + '<td id="jsSandBoxRight"><a id="jsSandBoxDisplayToggle" href="javascript:jsSandBoxDisplayToggle()">&lt;&lt;</a>'
  + '<div id="jsSandBoxDisplay" style="display:none">'
  + '<input id="jsSandBoxDisplayUrl" value="http://'+window.location.host+'/wiki/"><br>'
  + '<input id="jsSandBoxDisplayXpath" value="/div">'
  + '<input id="jsSandBoxDisplayGo" type="button" value="Go"'
  + '  onClick="$X.load($(\'jsSandBoxDisplayScroll\'),$(\'jsSandBoxDisplayUrl\').value,$(\'jsSandBoxDisplayXpath\').value)">'
  + '<div id="jsSandBoxDisplayScroll"></div></div></td></tr></table>';

  $('jsSandBox').innerHTML=s;

  jsSandBoxCode=$('jsSandBoxCode');
  jsSandBoxScroll=$('jsSandBoxScroll');
  $E.hook(jsSandBoxCode,'keydown',jsSandBoxKey);
  if($('wpTextbox1')) $E.hook($('wpTextbox1'),'keydown',jsSandBoxKey);
}

function jsSandBoxToggle() 
{
  $('jsSandBox').style.display = $('jsSandBox').style.display=='none' ? 'block' : 'none';
}

function jsSandBoxDisplayToggle()
{
  $('jsSandBoxDisplayToggle').innerHTML = $('jsSandBoxDisplay').style.display=='none' ? '&gt;&gt;' : '&lt;&lt;';
  $('jsSandBoxDisplay').style.display = $('jsSandBoxDisplay').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=jsSandBoxScroll.scrollHeight;
  target.focus();
}

function jsSandBoxRecall(old)
{
  jsSandBoxCode.value=old.innerHTML;
  jsSandBoxCode.focus();
}


function jsSandBoxKey(e) 
{
  keynum = window.event ? e.keyCode : e.which ;

  target=$E.target(e); 

  if (target.id=='jsSandBoxCode' && e.ctrlKey && keynum==13) { jsSandBoxGo(target);}
  if (target.selectionStart && (e.ctrlKey && keynum==32 || keynum==9))
  {
    e.preventDefault();
    if (!jsSandBoxAcActive)
    {
      var find = target.value.substr(0,target.selectionStart).match(/[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>