abstract | - // * frame.src='javascript:...' not work, if src is different host // gm_startup: in gm context: can access gm environment // * frame: use this script // * no frame: use this script (bookmark is not auto and cannot access gm env) if (GM_getValue('gm_startup')==null) GM_setValue('gm_startup', '/* startup script to run in GM environment */'); if (GM_getValue('gm_startup.enable')==null) GM_setValue('gm_startup.enable', true); // * need "try { }", else the exception will break the flow // * need alert(error), since "try { }" will suppress error in error console if (GM_getValue('gm_startup.enable', true)) try_eval_warn(GM_getValue('gm_startup', )); //location.href = 'javascript:('+encodeURI(function() { // try { eval("eval_frame"); } catch (e) { // if (typeof eval_frame != "undefined") return; // eval_frame = {}; // } //})+')(); void(0)'; //try { unsafeWindow.eval("eval_frame"); } catch (e) { // if (typeof unsafeWindow.eval_frame != "undefined") return; // location.href = 'javascript:eval_frame={};void(0)'; //} // startup: in document context: easy to implement global function // * frame: use this script // * no frame: use this script (bookmark is not auto) // BUG: should warn here? if (GM_getValue('startup')==null) GM_setValue('startup', '/* startup script to run in document environment */' ); if (GM_getValue('startup.enable')==null) GM_setValue('startup.enable', true); // * the exception not break the flow // * error shown in error console if (GM_getValue('startup.enable', true)) location.href = 'javascript:'+encodeURI(GM_getValue('startup', ))+';void(0)'; function string_literal(s) {// * s/["\\]/\\$&/g // LineTerminator is [
\u2028\u2029] // * s/[]/\/g // * s/[
]/\
/g // * s/[\u2028]/\\u2028/g // * s/[\u2029]/\\u2029/g return '"'+s+"'"; } //location.href = 'javascript:try_eval_warn('+ // // * escape() and encodeURIComponent() also encode [+&=] // // * encodeURI() not encode [+&=] // encodeURI(string_literal(GM_getValue('startup', )))+ //'); void(0)'; // eval box // * frame: use this script // * no frame: use location bar // preload // * frame: use this script // * no frame: use bookmark if (top == window) return; // * It also run on framed document //alert('greasemonkey: '+location); // * grease monkey cannot access dom inside frame // * NOTE: this function instance can access greasemonkey object, don't call // this instance from document context function try_eval_warn(val) {try { //var temp = eval(document.getElementById("eval").value); //document.getElementById("eval_result").value = typeof temp + "
" + temp; eval(val); } catch (e) { alert(e); } } // try { ... } will swallow error by default function try_eval(val) {try { eval(val); } catch (e) { /* warn in debug console? */ } } function test_eval(val) {try { eval(val); } catch (e) { return false; } return true; } // define the function within document context //location.href = // 'javascript:if (typeof try_eval_warn=="undefined") try_eval_warn=' + // try_eval_warn + // '; void(0)'; location.href = 'javascript:if (typeof try_eval_warn=="undefined") '+ encodeURI(try_eval_warn)+';void(0)'; location.href = 'javascript:if (typeof try_eval=="undefined") '+ encodeURI(try_eval)+';void(0)'; if (GM_getValue('preload_count')==null) GM_setValue('preload_count', 0); if (GM_getValue('preload[0]')==null) GM_setValue('preload[0]', 'my_label: alert("test")'); if (GM_getValue('preload[1]')==null) GM_setValue('preload[1]', 'test'); if (GM_getValue('preload')==null) GM_setValue('preload', "/* 1 */ alert('test1')
/* 2 */ test2" ); if (GM_getValue('bookmarklet')==null) GM_setValue('bookmarklet', "my bookmarklet
alert('This is sample of bookmarklet.')
"+ "item 2
test2
"+ "
"+ // blank line to make new paragraph "row 2
alert('2')
"+ "...
alert('...')" ); input = document.createElement('input'); input.type = 'text'; input.size = 100; add = document.createElement('input'); add.type = 'button'; add.value = 'clone'; add.setAttribute('onclick', 'form.parentNode.insertBefore(form.cloneNode(true), form.nextSibling);' ); form = document.createElement('form'); form.appendChild(input); form.appendChild(add); form.action = 'javascript:void(0)'; // ensure to not navigate out of page //form.onsubmit = 'try_eval_warn(this.firstChild.value); return false;'; form.setAttribute('onsubmit', // not work if element resizer is used //'try_eval_warn(this.firstChild.value); void(0); return false;' // this work with element resizer 'try_eval_warn(this.elements.item(0).value); void(0); return false;' ); body = document.getElementsByTagName('body').item(0); preload_count = GM_getValue('preload_count', 0); preload = GM_getValue('preload', ); preload = preload=="" ? [] : preload.split("
"); if (preload_count > 0) form.firstChild.value = GM_getValue('preload[0]', ); else if (preload.length > 0) form.firstChild.value = preload[0]; body.insertBefore(form, refChild = body.firstChild); for (var i = 1; i < preload_count; ++i) {form = form.cloneNode(true); form.firstChild.value = GM_getValue('preload['+i+']', ); body.insertBefore(form, refChild); } for (var i = preload_count > 0 ? 0 : 1; i < preload.length; ++i) {form = form.cloneNode(true); form.firstChild.value = preload[i]; body.insertBefore(form, refChild); } var bookmark = GM_getValue('bookmarklet', ).split("
"); var div = document.createElement('div'); body.insertBefore(div, refChild); // * label on must be childNode //var bookmark_form = document.createElement('button'); var bookmark_form = document.createElement('input'); bookmark_form.type = 'button'; for (var i = 0; i < bookmark.length; ++i) {var label = bookmark[i]; if (label == ) { body.insertBefore(div=document.createElement('div'), refChild); continue; } if (++i >= bookmark.length) break; var val = bookmark[i]; bookmark_form = bookmark_form.cloneNode(true); bookmark_form.value = label; //bookmark_form.setAttribute('onclick', val+';return false;void(0)'); // * bookmark_form.script is not visible to document context //bookmark_form.script = val; bookmark_form.setAttribute('onclick', // 'try_eval_warn(script);return false;void(0)' //); bookmark_form.setAttribute('script', val); bookmark_form.setAttribute('onclick', 'try_eval_warn(getAttribute("script"));return false;void(0)' ); div.appendChild(bookmark_form); }
|