Skip to content

Instantly share code, notes, and snippets.

@spalger
Created September 2, 2013 22:40
Show Gist options
  • Save spalger/6417923 to your computer and use it in GitHub Desktop.
Save spalger/6417923 to your computer and use it in GitHub Desktop.
Inject jQuery and lodash into the current tab via a bookmarklet. To use, just create a new bookmark and use this as the URL.
javascript: (function () {
var el = document.createElement('pre'),
b = document.getElementsByTagName('body')[0],
otherjQuery = false,
msg = '',
libs = [
function loadjQuery() {
if (typeof jQuery != 'undefined') {
showMsg('This page already using jQuery v' + jQuery.fn.jquery);
} else {
if (typeof $ == 'function') {
otherjQuery = true;
}
getScript('//cdnjs.cloudflare.com/ajax/libs/jquery/2.0.3/jquery.js', function () {
if (typeof jQuery == 'undefined') {
showMsg('Sorry, but jQuery wasn\'t able to load')
} else {
showMsg('This page is now jQuerified with v' + jQuery.fn.jquery + otherjQuery ? ' and noConflict(). Use $jq(), not $().' : '');
}
});
}
},
function loadLodash() {
if (typeof _ != 'undefined') {
showMsg('This page already using lodash v' + _.VERSION);
}
getScript('//cdnjs.cloudflare.com/ajax/libs/lodash.js/1.3.1/lodash.min.js', function () {
showMsg('This page is now has lodash');
});
}
];
el.style.position = 'fixed';
el.style.top = '0';
el.style.padding = '5px 10px';
el.style.zIndex = 9001;
el.style.fontSize = '12px';
el.style.color = '#222';
el.style.backgroundColor = '#f99';
function getScript(url, success) {
var script = document.createElement('script');
script.src = url;
var head = document.getElementsByTagName('head')[0],
done = false;
script.onload = script.onreadystatechange = function () {
if (!done && (!this.readyState || this.readyState == 'loaded' || this.readyState == 'complete')) {
done = true;
success();
script.onload = script.onreadystatechange = null;
head.removeChild(script);
}
};
head.appendChild(script);
}
function showMsg(msg) {
if (!document.body.contains(el)) {
b.appendChild(el);
}
el.appendChild(document.createTextNode(msg + '\n'));
window.setTimeout(function () {
if (typeof jQuery == 'undefined') {
b.removeChild(el);
} else {
jQuery(el).fadeOut('slow', function () {
jQuery(this).remove();
});
if (otherjQuery) {
$jq = jQuery.noConflict();
}
}
}, 2500);
}
libs.forEach(function (init) {
init();
})
})();
@vitalybe
Copy link

Works great! (made by http://mrcoles.com/bookmarklet/)

In a form of a bookmarket:

javascript:(function()%7B(function%20()%20%7Bvar%20el%20%3D%20document.createElement('pre')%2Cb%20%3D%20document.getElementsByTagName('body')%5B0%5D%2CotherjQuery%20%3D%20false%2Cmsg%20%3D%20''%2Clibs%20%3D%20%5Bfunction%20loadjQuery()%20%7Bif%20(typeof%20jQuery%20!%3D%20'undefined')%20%7BshowMsg('This%20page%20already%20using%20jQuery%20v'%20%2B%20jQuery.fn.jquery)%3B%7D%20else%20%7Bif%20(typeof%20%24%20%3D%3D%20'function')%20%7BotherjQuery%20%3D%20true%3B%7DgetScript('%2F%2Fcdnjs.cloudflare.com%2Fajax%2Flibs%2Fjquery%2F2.0.3%2Fjquery.js'%2C%20function%20()%20%7Bif%20(typeof%20jQuery%20%3D%3D%20'undefined')%20%7BshowMsg('Sorry%2C%20but%20jQuery%20wasn%5C't%20able%20to%20load')%7D%20else%20%7BshowMsg('This%20page%20is%20now%20jQuerified%20with%20v'%20%2B%20jQuery.fn.jquery%20%2B%20otherjQuery%20%3F%20'%20and%20noConflict().%20Use%20%24jq()%2C%20not%20%24().'%20%3A%20'')%3B%7D%7D)%3B%7D%7D%2Cfunction%20loadLodash()%20%7Bif%20(typeof%20_%20!%3D%20'undefined')%20%7BshowMsg('This%20page%20already%20using%20lodash%20v'%20%2B%20_.VERSION)%3B%7DgetScript('%2F%2Fcdnjs.cloudflare.com%2Fajax%2Flibs%2Flodash.js%2F1.3.1%2Flodash.min.js'%2C%20function%20()%20%7BshowMsg('This%20page%20is%20now%20has%20lodash')%3B%7D)%3B%7D%5D%3Bel.style.position%20%3D%20'fixed'%3Bel.style.top%20%3D%20'0'%3Bel.style.padding%20%3D%20'5px%2010px'%3Bel.style.zIndex%20%3D%209001%3Bel.style.fontSize%20%3D%20'12px'%3Bel.style.color%20%3D%20'%23222'%3Bel.style.backgroundColor%20%3D%20'%23f99'%3Bfunction%20getScript(url%2C%20success)%20%7Bvar%20script%20%3D%20document.createElement('script')%3Bscript.src%20%3D%20url%3Bvar%20head%20%3D%20document.getElementsByTagName('head')%5B0%5D%2Cdone%20%3D%20false%3Bscript.onload%20%3D%20script.onreadystatechange%20%3D%20function%20()%20%7Bif%20(!done%20%26%26%20(!this.readyState%20%7C%7C%20this.readyState%20%3D%3D%20'loaded'%20%7C%7C%20this.readyState%20%3D%3D%20'complete'))%20%7Bdone%20%3D%20true%3Bsuccess()%3Bscript.onload%20%3D%20script.onreadystatechange%20%3D%20null%3Bhead.removeChild(script)%3B%7D%7D%3Bhead.appendChild(script)%3B%7Dfunction%20showMsg(msg)%20%7Bif%20(!document.body.contains(el))%20%7Bb.appendChild(el)%3B%7Del.appendChild(document.createTextNode(msg%20%2B%20'%5Cn'))%3Bwindow.setTimeout(function%20()%20%7Bif%20(typeof%20jQuery%20%3D%3D%20'undefined')%20%7Bb.removeChild(el)%3B%7D%20else%20%7BjQuery(el).fadeOut('slow'%2C%20function%20()%20%7BjQuery(this).remove()%3B%7D)%3Bif%20(otherjQuery)%20%7B%24jq%20%3D%20jQuery.noConflict()%3B%7D%7D%7D%2C%202500)%3B%7Dlibs.forEach(function%20(init)%20%7Binit()%3B%7D)%7D)()%7D)()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment