Skip to content

Instantly share code, notes, and snippets.

@hansifer
Last active August 16, 2016 02:25
Show Gist options
  • Save hansifer/62ea8991ebf8017e12d7f80653558e6f to your computer and use it in GitHub Desktop.
Save hansifer/62ea8991ebf8017e12d7f80653558e6f to your computer and use it in GitHub Desktop.
Manually inject a JS lib into a web page with load status feedback, either via devtools or a bookmarklet. Handy use case: facilitate devtools-based ad hoc scrapes with jQuery.
// N: lib name (eg, 'jQuery', 'Lodash')
// P: lib window property (eg, 'jQuery', '_')
// U: lib URL (eg, '//code.jquery.com/jquery-3.1.0.min.js', '//cdn.jsdelivr.net/lodash/4.15.0/lodash.min.js')
// for bookmarklet, prepend with "javascript:"
/*javascript:*/((w,d,e,N,P,U)=>{N='jQuery';P='jQuery';U='//code.jquery.com/jquery-3.1.0.min.js';if(w[P]){m(N+' already loaded','#c8af00')}else{m('Waiting on '+N+' load...');(e=d.createElement('script')).src=U;w._llt_script=w.setTimeout(()=>{m(N+' load failed','#d80e0e');delete w._llt_script},2e3);e.onload=()=>{w.clearTimeout(w._llt_script);delete w._llt_script;if(w[P]){m(N+' loaded','#117e61')}else{m(N+' load failed','#d80e0e')}};d.getElementsByTagName('head')[0].appendChild(e)}function m(t,c,i,e,s){w.clearTimeout(w._llt_status);if(!(e=d.getElementById(i='_ll_load_status'))){(e=d.createElement('div')).id=i;(s=e.style).position='fixed';s.right=s.bottom='5px';s.zIndex='2147483647';s.padding='20px 30px';s.opacity='0.9';s.color='#fff';s.fontSize='20px';(d.body||d.documentElement).appendChild(e)}e.textContent=t;e.style.background=c||'#000';w._llt_status=w.setTimeout(()=>{e.parentElement.removeChild(e);delete w._llt_status},3000)}})(this,document)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment