As some of you may know, Google Chrome has put some severe limitation on Greasemonkey scripts.
Chromium does not support
@require
,@resource
,unsafeWindow
,GM_registerMenuCommand
,GM_setValue
, orGM_getValue
.
Without require, I can't find a way to include the jQuery library in Greasemonkey script under Google Chrome.
Does anybody have some advice in this matter?
Use jQuery without fear of conflicts, by calling
jQuery.noConflict(true)
. Like so:But, For cross-browser scripts, why not take advantage of a nice, fast, local copy of jQuery, when you can?
The following works as a Chrome userscript and a Greasemonkey script, and it uses the nice local
@require
copy of jQuery, if the platform supports it.Easier solution: cut+paste the contents of jquery.min.js into the top of your user script. Done.
I found various problems with the recommended answers. The addJQuery() solution works on most pages but has bugs on many. If you run into issues just copy+paste the jquery contents into your script.
There's a really easy way to get around including a full copy of jQuery for Chrome scripts when those scripts don't actually use any privileged features (GM_* functions, etc)...
Simply insert the script itself into the page DOM and execute! The best part is that this technique works just as well on Firefox+Greasemonkey, so you can use the same script for both:
(unapologetically stolen from Shog9 on meta.stackoverflow since he didn't move it here, and I have to delete the meta post..)
I wonder if you couldn't rely on
document.defaultView.jQuery
in your GM script ala:The simple way is using
required
keyword:Another approach would be to modify your script to load jQuery manually. Example from http://joanpiedra.com/jquery/greasemonkey/:
EDIT: DRATS! After testing it appears this code does not work since Google Chrome runs userscripts/extensions in a separate scope/process from the actual webpage. You can download the jQuery code using an XmlhttpRequest and then Eval it, but you have to host the code on a server that allows Cross-Origin Resource Sharing using the
Access-Control-Allow-Origin: *
header. Sadly NONE of the current CDNs with jQuery support this.