Greasemonkey @require not working in Chrome
I'm trying to add jQuery using Greasemonkey @require
/ method @include
, but it doesn't work. The following error appears:
Uncaught ReferenceError: $ is not defined (repeated 10 times)
This is my example 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 this?
source to share
@ic
is not a valid meta-rule, so it is ignored.
Use @require
if you want to load jQuery into 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 you are using Chrome . Chrome doesn't support the rule @require
. See also:
- GreaseSpot Wiki: Multi-Browser Script
- Stack Overflow: Why is jQuery not working in Chrome custom scripts (Greasemonkey)?
- Stack Overflow: How to use jQuery in Greasemonkey scripts in Google Chrome?
If you need full Greasemonkey support in Chrome, use Tampermonkey .
Eliminate user scripting confusion in Chrome
Chrome does not natively support GreaseMonkey. Whenever a file is downloaded .user.js
, it is converted to a Chrome extension as a Content script .
For more information on user scripting in Chrome, see this documentation .
The user script is literally copied to the extension 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 metablock. When a user script contains a rule @include
, his rule matches
will contain https://*/*
also http://*/*
because of a too loose rule @include
.
The generated content manifest.json
looks like this:
{
"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"
}
source to share