views:

2388

answers:

5

I've seen the other question on here about loading jQuery in a Greasemonkey. Having tried that method, with this require statement inside my ==UserScript== tags:

// @require    http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js

I still get the following error message in Firefox's error console:

Error: Component is not available
Source File: file:///Users/greg/Library/Application%20Support/
       Firefox/Profiles/xo9xhovo.default/gm_scripts/myscript/jquerymin.js
Line: 36

This stops my greasemonkey code from running. I've made sure I included the @require for jQuery and saved my js file before installing it, as required files are only loaded on installation.

Code:

// ==UserScript==
// @name           My Script
// @namespace      http://www.google.com
// @description    My test script
// @include        http://www.google.com
// @require        http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js
// ==/UserScript==

GM_log("Hello");

I have Greasemonkey 0.8.20091209.4 installed on Firefox 3.5.7 on my Macbook Pro, Leopard (10.5.8). I've cleared my cache (except cookies) and have disabled all other plugins except Flashblock 1.5.11.2, Web Developer 1.1.8 and Adblock Plus 1.1.3.

My config.xml with my Greasemonkey script installed:

<UserScriptConfig>
<Script filename="myscript.user.js" name="My Script" 
 namespace="http://www.google.com" description="My test script" enabled="true" 
 basedir="myscript">
    <Include>http://www.google.com&lt;/Include&gt;
    <Require filename="jquerymin.js"/>
</Script>

I can see jquerymin.js sat in the gm_scripts/myscript/ directory.

Additionally, is it common for this error to occur in the console when installing a Greasemonkey script?

Error: not well-formed
Source File: file:///Users/Greg/Documents/myscript.user.js
Line: 1, Column: 1
Source Code:
   // ==UserScript==
A: 

The @require attribute doesn't work correctly in Greasemonkey and jQuery...this same error can occur in FireBug as well.

An alternative is to include jQuery in the page via Greasemonkey by creating the script tag. Here's how to do that.

Nick Craver
Thanks, I've seen that link - I'd like to know why @require is a documented method but isn't working for me.http://www.keyvan.net/2008/10/greasemonkey-jquery/
Greg K
We are using Greasemonkey with the @require attribute for JQuery and it works just fine for us.
Anders
+3  A: 

Ok, so i looked into this a bit more deeper. I used your script exactly, but used our JQuery version, making it look like this:

// ==UserScript==
// @name           My Script
// @namespace      http://www.google.com
// @description    My test script
// @include        http://www.google.se/*
// @include        http://www.dn.se/*
// @require        http://myserver/jquery-1.3.2.js
// ==/UserScript==

GM_log("Hello");

This works just fine for me, my guess, the JQuery up on the google api is missing some functions. Because this code above, works just fine. Also note the /* at the end of each url, please include that.

Try another JQuery and change the urls and it should world properly.

Anders
Thanks, I changed my require URL to http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js and it works!
Greg K
A: 

Not entirely true, it seems like jQuery 1.4 tries to detect something using a call that just doesn't work in the greasemonkey environment. @require does normally work as it should.

So reverting to 1.3.2 does do the trick, but I'd rather find a solution that lets me use 1.4.

btw, I use this, slightly different:

// @require http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js
Johan
+3  A: 

I found a non-ideal way to use it with jQuery 1.4.1 -- this seems to fix it. It's the new browser sniffing that seems to "break" it.

jquery-1.4.1.min.js:

  [old]  36: var o=r.createElement("div");n="on"+n;var m=n in o;
  [new]  36: var o=r.createElement("div");n="on"+n;var m=true;

jquery-1.4.1.js

  [old] 934: var isSupported = (eventName in el);
  [new] 934: var isSupported = true;
lchau
+3  A: 

I was stumbling around trying to deal with this issue with GM 0.8 and jquery 1.4.2 and found this: http://forum.jquery.com/topic/importing-jquery-1-4-1-into-greasemonkey-scripts-generates-an-error

It looks to me like the definitive answer to the question and how to work around it. The workaround worked for me.

oeuftete
Thanks for linking that, going forward this is the definitive answer (ichau also mentioned the same fix but without a link).At the time I asked this question, 1.4 was only just out and I was using 1.3.2 for which Anders answer addressed my issue.
Greg K