Last Updated: February 25, 2016
·
995
· yoannm

Just make a bookmarklet

I love to make random snippets on random web pages using only the console.

But sometimes, there's too much code to organize, or you would like to keep it for later use.

My solution is to use a bookmarklet for this kind of scripts.

Here's my bk's template :

javascript: (function() {
  var script = document.createElement('script'),
    head = document.getElementsByTagName('head')[0],
    done = false,
    abla = setTimeout(function(){
      alert('Something went wrong, try a non https:// page.');
    }, 2000);
    script.src = [YOUR_SCRIPT_URL]+ (+new Date());
    script.onload=script.onreadystatechange = function(){
      clearTimeout(abla);
      if ( !done && 
                (!this.readyState || 
                this.readyState == 'loaded' || 
                this.readyState == 'complete') 
           ) {
        done=true;
        script.onload = script.onreadystatechange = null;
        head.removeChild(script);
      };
    };
    head.appendChild(script);
})();

Don't forget, do not mix https:// page with your http:// script.
It will not run.
Also, you don't have to upload your script on a server, you can link it via your local IP.

It's magic and handy.

1 Response
Add your response

For a bookmaklet I'd rather do something smaller than that, even if that doesn't throw an alert :

javascript:(function(d,u,s){s=d.createElement("script"),s.src=u+"?"+(+new Date()),d.body.appendChild(s)})(document,"//domain.com/your/url")
over 1 year ago ·