Skip to content

Instantly share code, notes, and snippets.

@msgodf
Created October 16, 2013 06:22
Show Gist options
  • Save msgodf/7003379 to your computer and use it in GitHub Desktop.
Save msgodf/7003379 to your computer and use it in GitHub Desktop.
Bookmarklet to create a eval'ing text area and bootstrapping code to provide some styling/formatting niceties.
try {
var RIGHTCURLYBRACE="}",LEFTCURLYBRACE="{";
// bookmarklet to kick things off
//javascript:function%20runCode(e)%20%7Be.preventDefault();eval(document.getElementById(%22codearea%22).value);return%20false;%7D;(function()%7Ba=document.createElement(%22div%22);b=document.body.appendChild(a);b.style.background=%22%23eeeeee%22;b.style.width=%22100%25%22;b.style.height=%22100px%22;b.innerHTML='%3Ctextarea%20id=%22codearea%22%3E%3C/textarea%3E%3Cbutton%20id=%22runcodebutton%22%3ERun%3C/button%3E';document.getElementById('runcodebutton').addEventListener(%22click%22,runCode);a.style.position=%22absolute%22;a.style.top=%220px%22;document.getElementById(%22codearea%22).value=window.localStorage.getItem(%22codeMSG%22);a.style.zIndex=%221000%22%7D)();
function bootstrap() {
// get ourself
var codeArea=document.getElementById("codearea");
function trim(text) {
text=text.replace(/(^\s*)|(\s*$)/gi,"");
text=text.replace(/[ ]{2,}/gi," ");
text=text.replace(/\n /,"\n");
return text;
}
function indent(text) {
var lines=text.split("\n");
var currentDepth=0;
for(var i=0;i<lines.length;++i) {
var line=lines[i];
// have to use constants to avoid the line containing the token itself getting split
currentDepth-=line.split(RIGHTCURLYBRACE).length-1;
if(currentDepth>0){
line=Array(currentDepth+1).join(" ")+line;
lines[i]=line;
}
currentDepth+=line.split(LEFTCURLYBRACE).length-1;
}
return lines.join("\n");
}
codeArea.value=trim(codeArea.value);
//thisArea.value=thisArea.value.replace(/{/g,"{\n");
// thisArea.value=thisArea.value.replace(/}/g,"} \n");
//thisArea.value=thisArea.value.replace(/;/g,";\n");
var runButton=document.getElementById("runcodebutton");
runButton.style.width="100%";
runButton.style.height="80px";
runButton.style.border="1px solid black";
runButton.style.backgroundColor="#ffefbb";
runButton.style.fontSize="40px";
runButton.style.position="absolute";
runButton.style.top=0;
// ensure style
codeArea.style.width="100%";
codeArea.style.height="550px";
codeArea.style.fontFamily="courier new";
codeArea.style.fontSize="20px";
codeArea.style.position="absolute";
codeArea.style.top="80px";
// disable auto capitalisation and other 'helpful' things
codeArea.autocapitalize="off";
codeArea.autocorrect="off";
codeArea.autocomplete="off";
codeArea.spellcheck="false";
// flash the background blue for 100ms at end of function
codeArea.style.backgroundColor="grey";
function resetBackground(){
codeArea.style.backgroundColor="white";
}
setTimeout(resetBackground,100);
// auto reindents the code
codeArea.value=indent(codeArea.value);
// Save ourselves!
window.localStorage.setItem("codeMSG",codeArea.value);
alert(localStorage.items);
}
bootstrap();
} catch(e){
alert(e);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment