Skip to content

Instantly share code, notes, and snippets.

@BrynM
Created August 10, 2012 09:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BrynM/3313005 to your computer and use it in GitHub Desktop.
Save BrynM/3313005 to your computer and use it in GitHub Desktop.
Chrome JavaScript Scratchpad Bookmarklet
(function () {
function bpmv_quick_scratch_pad () {
var scrId = 'bpmv_script_from_marklet'
, aTagId = 'scratch_pad_marklet_linky'
, scr = document.getElementById( scrId )
, aTag = document.getElementById( aTagId )
, bod = document.getElementsByTagName( 'body' )[0]
, oldCode = '';
function scratch_not_tab ( ev ) {
var aTab = 9;
if( ev.keyCode == aTab ) {
ev.target.value += "\t";
if( ev.preventDefault ) {
ev.preventDefault();
}
return false;
}
}
function make_scratch_pad () {
var wrapId = 'scratch_pad_marklet_wrapper'
, padId = 'scratch_pad_textarea'
, runId = 'run_scratch_pad'
, wrap = document.getElementById( wrapId )
, pad = document.getElementById( padId )
, run = document.getElementById( runId )
, bod = document.getElementsByTagName( 'body' )[0];
if ( !wrap ) {
wrap = document.createElement('div');
wrap.style.display = 'block';
wrap.id = wrapId;
bod.appendChild( wrap );
if ( !pad && wrap ) {
pad = document.createElement( 'textarea' );
pad.style.display = 'block';
pad.style['min-width'] = '800px';
pad.style['min-height'] = '250px';
pad.id = padId;
if ( typeof(bpmv) == 'object' ) {
oldCode = bpmv.cook( 'scratch_pad_string' );
if ( bpmv.str(oldCode) ) {
pad.value = oldCode;
}
}
if( wrap.addEventListener ) {
wrap.addEventListener( 'keydown', scratch_not_tab, false );
} else if( wrap.attachEvent ) {
wrap.attachEvent( 'onkeydown', scratch_not_tab );
}
wrap.appendChild( pad );
}
if ( !run && wrap ) {
run = document.createElement( 'button' );
run.id = runId;
run.innerText = 'Run JS';
pad.style.margin = '0 10px';
run.onclick = function () {
var scratch = document.getElementById( padId ).value;
console.log( scratch );
if ( ( typeof(scratch) == 'string' ) && ( scratch.length > 0 ) ) {
if ( typeof(bpmv) == 'object' ) {
bpmv.cook( 'scratch_pad_string', scratch );
}
eval(scratch);
}
};
wrap.appendChild( run );
}
if ( !aTag && wrap ) {
aTag = document.createElement( 'a' );
aTag.id = aTagId;
aTag.innerText = 'Drag this link to add bookmarklet...';
wrap.appendChild( aTag );
}
}
if ( aTag ) {
aTag.href = 'javascript:(function(){'+bpmv_quick_scratch_pad.toString()+'\nbpmv_quick_scratch_pad();})()';
}
}
if ( !scr ) {
scr = document.createElement('script');
scr.id = scrId ;
scr.type = 'text/javascript';
( scr.src = ('https:' == document.location.protocol ? 'https' : 'http') + '://raw.github.com/BrynM/bpmv/master/bpmv.js' );
scr.onload = function () {
make_scratch_pad();
};
bod.appendChild( scr );
} else {
make_scratch_pad();
}
}
bpmv_quick_scratch_pad();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment