views:

337

answers:

3

I'm unable to get this script to work as an extension in google chrome. No alert shows up. normal javascript alerts work.

// ==UserScript==
// @name           voip
// @namespace      1
// @description    voip
// @include        *
// @require        http://jquery.com/src/jquery-latest.js
// ==/UserScript==

$(document).ready(function() {  
        alert("Hello world!");
});
+2  A: 

Greasemonkey support in Chrome does not include require statements. You'd be better off creating an extension rather than a Greasemonkey script.

That, or you could use the Google API to load jQuery.

Arda Xi
would doing document.getElementsByTagName('head')[0].appendChild(GM_JQ); work?
David Grassi
If GM_JQ is a script element containing a link to jQuery, sure that'll work.
Arda Xi
+2  A: 

Easy solution (if viable for you) would be to just copy paste the minified version of jQuery into your greasemonkey script.

// ==UserScript==
// @name           voip
// @namespace      1
// @description    voip
// @include        *
// ==/UserScript==
//jQuery 1.4.2 minified code
(function(A,w){function ma(){if(!c.isReady){try{s.documentElement.doScroll...
....
A.jQuery=A.$=c})(window);
//your code
$(document).ready(function() {  
    alert("Hello world!");
});
jitter
It would be more efficient to have the jQuery code in cache once than in every script that uses it.
Arda Xi
I know that. Do you have a better solution?
jitter
Are you sure this works? I tried, but never got the "Hello World" alert. (FireFox 3.6)
Tobbe
No I'm not sure this works. And I didn't try. Btw. this solution was meant for chrome. Not for firefox where `@require` is supported anyway
jitter
+3  A: 

This is a nice article: How to play nicely with jQuery and Greasemonkey

The method explained works for chrome as well.

Update:

I came up with a better method that works on all browsers, which you can read about here.

Erik Vold