Skip to content

Instantly share code, notes, and snippets.

@angel-vladov
Created January 28, 2014 12:00
Show Gist options
  • Save angel-vladov/8666472 to your computer and use it in GitHub Desktop.
Save angel-vladov/8666472 to your computer and use it in GitHub Desktop.
Boilerplate code for a Greasemonkey script which reuses the jQuery from the page it's attached to.
var jQuery, $ = null;
function addJQuery(callback) {
var p = null;
if(window.opera || window.navigator.vendor.match(/Google/)) {
var div = document.createElement("div");
div.setAttribute("onclick", "return window;");
p = div.onclick();
}
else {
p = unsafeWindow;
}
jQuery = $ = p.jQuery.noConflict();
callback();
}
function handler_examplePage1() {
// You can use both $ and jQuery
alert('Page 1');
}
function handler_examplePage2 {
alert('Page 2');
}
// Apply script modifications
(function(href) {
var pageModifiers = {
'example/page-1': handler_examplePage1, // Will be called if page URL contains example/page2
'example/page-2': handler_examplePage2
}
for(var pageId in pageModifiers) {
if(href.lastIndexOf(pageId) > 0) {
addJQuery(pageModifiers[pageId]);
return;
}
}
})(window.location.href)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment