Skip to content

Instantly share code, notes, and snippets.

@brianpeiris
Created February 6, 2013 03:30
Show Gist options
  • Save brianpeiris/4720005 to your computer and use it in GitHub Desktop.
Save brianpeiris/4720005 to your computer and use it in GitHub Desktop.
Librarify: A bookmarklet that injects JavaScript libaries into any page. Based on Rey Bango's jQuerify: http://blog.reybango.com/2010/09/02/how-to-easily-inject-jquery-into-any-web-page/
// Librarify: A bookmarklet that injects JavaScript libaries into any page.
// Based on Rey Bango's jQuerify: http://blog.reybango.com/2010/09/02/how-to-easily-inject-jquery-into-any-web-page/
javascript: (function (libname) {
var
libraries = {
jquery: {
url: '//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js',
libvar: '$'
},
underscore: {
url: '//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.4/underscore-min.js',
libvar: '_'
}
},
library = libraries[libname.toLowerCase()],
showMessage = function (message) {
var
messageElem = document.createElement('div'),
body = document.getElementsByTagName('body')[0];
messageElem.style.position = 'fixed';
messageElem.style.width = '400px';
messageElem.style.marginLeft = '-200px';
messageElem.style.top = '0';
messageElem.style.left = '50%';
messageElem.style.padding = '10px';
messageElem.style.zIndex = 1001;
messageElem.style.fontSize = '12pt';
messageElem.style.color = '#222';
messageElem.style.backgroundColor = '#f99';
messageElem.innerHTML = message;
body.appendChild(messageElem);
window.setTimeout(function () {
body.removeChild(messageElem);
}, 2500);
},
getScript = function (url, success) {
var script = document.createElement('script'),
head = document.getElementsByTagName('head')[0],
done = false;
script.src = url;
script.onload = script.onreadystatechange = function () {
if (
!done &&
(!this.readyState || this.readyState === 'loaded' ||
this.readyState === 'complete')
) {
done = true;
success();
}
};
head.appendChild(script);
};
if (!library) {
library = {
url: libname
}
}
getScript(library.url, function () {
var message;
if (library.libvar && typeof window[library.libvar] === 'undefined') {
message = 'Sorry, but ' + libname + ' wasn\'t able to load';
} else {
message = 'This page is now loaded with ' + libname;
}
return showMessage(message);
});
})('%s');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment