Bookmarklet Creator

Posted by cgp

This bookmarklet creator takes care of creating a bookmarklet.

Features:

  • Usage of url parameters when loading javascript to prevent caching of the target javascript URLs.
  • Loads multiple javascript, css, or even HTML (appends automatically) files.
  • Tracks what has been loaded previously and avoids reloading that was has been already loaded.
  • In the absence of a callback function, always the last
  • Populate popular javascript AJAX libraries from the Google CDN with the click of a button.

Bookmarklet Title:
Bookmark Loader Location:

Download Bookmarklet Loader

Javasript/CSS URLs:http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js
Javascript callback:

Your Bookmarklet Link

String.prototype.startsWith = function(str) {
return this.substring(0, str.length).toLowerCase() == str.toLowerCase();
}
String.prototype.endsWith = function(str) {
return (this.length-str.length)==this.toLowerCase().lastIndexOf(str.toLowerCase());
}

function updateLink() {
var scripts = jQuery('#jsSrc').val().replace(new RegExp('\n$', 'igm'), '').split('\n').join("','");
var src;
var loaderScript = jQuery('#bookmark_loader').val();
var callbackScript = jQuery('#callbackFunc').val().replace(new RegExp('\n$', 'igm'), '');
if (scripts.length == 0) {
src = "javascript:void(function(){"+callbackScript+"}())";
} else {
if (callbackScript.length > 0)
callbackScript = "callback = function() {"+callbackScript+"};";
src = "javascript:void(function(){"+callbackScript+"urls=['"+scripts+"'];var s=document.createElement('script');s.src='"+loaderScript+"?x='+new%20Date().getTime();document.getElementsByTagName('head')[0].appendChild(s);}())";
}

jQuery('#scriptlen').html((502-src.length)+" characters left");
//jQuery("#src").val(src);
jQuery("#bookmark")[0].href = src;
jQuery("#bookmark").html(jQuery('#linkText').val());

}
$(function() {
jQuery("#linkText").keyup(updateLink);
jQuery("#jsSrc").keyup(updateLink);
jQuery("#callbackFunc").keyup(updateLink);

updateLink();
});

References:

  • http://cse-mjmcl.cse.bris.ac.uk/blog/2005/08/18/1124396539593.html
  • http://encosia.com/2008/12/10/3-reasons-why-you-should-let-google-host-jquery-for-you/