3

I'm trying to add jQuery using Greasemonkey's @require / @include method, but it doesn't work. The following error shows up:

Uncaught ReferenceError: $ is not defined (repeated 10 times)

This is my sample code:

// ==UserScript==
// @description Bored, really bored.
// @name MatteoSample
// @namespace MatteoSampleNamespace
// @include *
// @include       http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js
// ==/UserScript==

$(document).slideUp();

How can I fix it?

Rob W
  • 341,306
  • 83
  • 791
  • 678
Matteo Mosconi
  • 244
  • 5
  • 19

2 Answers2

10

@ic is not a valid meta-rule, so it's ignored.
Use @require if you want to load jQuery in your user script.

// ==UserScript==
// @name       Foo
// @namespace  Bar
// @include    *
// @require    http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js
// ==/UserScript==

EDIT: In the comments, you said that you're using Chrome. Chrome does not support the @require rule. See also:

If you want full Greasemonkey support in Chrome, use Tampermonkey.

Clearing up confusion about User scripts in Chrome

Chrome does not natively support GreaseMonkey. Whenever a .user.js file is loaded, it's converted to a Chrome extension in form of a Content script.

For more information on User scripts in Chrome, see this documentation.

The User script is literally copied into the extension's directory:

// ==UserScript==
// @name       Foo
// @namespace  Bar
// @include    *
// @require    http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js
// ==/UserScript==
alert(typeof $)

A manifest.json file is created, based on the meta block. When the User script contains a @include rule, its matches rule will contain https://*/* and http://*/*, because of the too loose @include rule.

The contents of the generated manifest.json looks like:

{
   "content_scripts": [ {
      "exclude_globs": [  ],
      "include_globs": [ "*" ],
      "js": [ "script.js" ],
      "matches": [ "http://*/*", "https://*/*" ],
      "run_at": "document_idle"
   } ],
   "converted_from_user_script": true,
   "description": "",
   "key": "+.... some key ...=",
   "name": "Foo",
   "version": "1.0"
}
Community
  • 1
  • 1
Rob W
  • 341,306
  • 83
  • 791
  • 678
  • Sorry, it's an error i met while pasting. i was using @include, sorry! – Matteo Mosconi Mar 20 '12 at 17:17
  • 3
    @MatteoMosconi You should not use `@include`, but **`@require`** to load externals. – Rob W Mar 20 '12 at 17:19
  • sorry, even trying using your meta block i see no changes :'( – Matteo Mosconi Mar 20 '12 at 17:35
  • thanks. I'm using the latest version of Chrome: it should alredy have greasemonkey on it. After i installed your extension ( and removed the mine ) i see some "undefined" alerts in each site. But unfortunally, i still see the "Uncaught ReferenceError: $ is not defined (anonymous function) (anonymous function)". - What should i do to solve this? Thank you .. – Matteo Mosconi Mar 20 '12 at 20:02
  • What's bad? Chrome now have greasmonkey built in! – Matteo Mosconi Mar 20 '12 at 20:55
  • 1
    @MatteoMosconi That is wrong. See my updated answer for more details. – Rob W Mar 20 '12 at 21:08
1

chrome doesn't support greasemonkey scripts, rather, specific greasemonkey commands (@require, GM_ etc)

to get that stuff working, install chrome addon "TamperMonkey"

RozzA
  • 609
  • 2
  • 9
  • 25