Userscripts and most Greasemonkey scripts operate in sandboxes. So, the script needs to either load jQuery (the $
object your code is using) itself, or it needs to inject code to use the target page's version of jQuery.
The best way to use jQuery in a cross-browser script is via this stunningly brilliant method. (^_^)
So, your script would become:
// ==UserScript==
// @name _Mouse-click detect
// @include http://YOUR_SERVER.COM/YOUR_PATH/*
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
// @grant GM_info
// ==/UserScript==
function GM_main ($) {
function removeSth () {
$('a').click (function () {
alert ("warning");
return false;
} );
}
removeSth ();
}
if (typeof GM_info !== "undefined") {
GM_main ($);
}
else {
add_jQuery (GM_main);
}
function add_jQuery (callbackFn, jqVersion) {
var jqVersion = jqVersion || "1.7.2";
var D = document;
var targ = D.getElementsByTagName ('head')[0] || D.body || D.documentElement;
var scriptNode = D.createElement ('script');
scriptNode.src = 'http://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);
}
However, for just Firefox-Greasemonkey and Tampermonkey, you can simplify the script to:
// ==UserScript==
// @name _Mouse-click detect
// @include http://YOUR_SERVER.COM/YOUR_PATH/*
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
// @grant GM_info
// ==/UserScript==
function removeSth () {
$('a').click (function () {
alert ("warning");
return false;
} );
}
removeSth ();