Skip to content

Instantly share code, notes, and snippets.

@bouil
Last active December 16, 2015 22:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bouil/5509113 to your computer and use it in GitHub Desktop.
Save bouil/5509113 to your computer and use it in GitHub Desktop.
Empty userscript using jQuery compatible with both Firefox (Greasemonkey) and Google Chrome (with Tampermonkey)
// ==UserScript==
// @name My Script
// @namespace com.example
// @description Script description
// @include https://*.example.com/url/*
// @version 0.1
// @require https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js
// @grant GM_info
// ==/UserScript==
var debug = false;
var start = function (jQuery) {
// do your stuff
};
/*
http://stackoverflow.com/questions/2246901/how-can-i-use-jquery-in-greasemonkey-scripts-in-google-chrome/12751531#12751531
*/
if (typeof jQuery === "function") {
if (debug){
console.log ("Running with local copy of jQuery!");
}
start(jQuery);
}
else {
if (debug){
console.log ("Fetching jQuery from some 3rd-party server.");
}
add_jQuery (start, "1");
}
function add_jQuery (callbackFn, jqVersion) {
var jqVersion = jqVersion || "1";
var D = document;
var targ = D.getElementsByTagName ('head')[0] || D.body || D.documentElement;
var scriptNode = D.createElement ('script');
scriptNode.src = 'https://ajax.googleapis.com/ajax/libs/jquery/'
+ jqVersion
+ '/jquery.min.js'
;
scriptNode.addEventListener ("load", function () {
var scriptNode = D.createElement ("script");
scriptNode.textContent =
'var gm_jQuery = jQuery.noConflict (true);\n'
+ '(' + callbackFn.toString () + ')(gm_jQuery);'
;
targ.appendChild (scriptNode);
}, false);
targ.appendChild (scriptNode);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment