Greasemonkey @require在Chrome中无法使用 [英] Greasemonkey @require does not work in Chrome
问题描述
我尝试使用Greasemonkey的 @require
/ @include
方法添加jQuery,但它没有工作。出现以下错误:
未捕获的ReferenceError:$未定义(重复10次)
$ c
$ b 这是我的示例代码:
// == UserScript ==
// @description无聊,真的很无聊。
// @name MatteoSample
// @namespace MatteoSampleNamespace
// @include *
// @include http://ajax.googleapis.com/ajax/libs/jquery /1.3.2/jquery.min.js
// == / UserScript ==
$(document).slideUp();
我该如何解决它?
code> @ic 不是有效的元规则,因此它被忽略。
使用 @require
如果你想在你的用户中加载jQuery脚本。 // == UserScript ==
// @name Foo
// @namespace Bar
// @include *
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js
// = = / UserScript ==
编辑:在评论中,您表示您使用铬即可。 Chrome不支持 @require
规则。 另见:
- GreaseSpot Wiki:跨浏览器用户脚本
- 堆栈溢出:
- 堆栈溢出:
如果您想在Chrome中使用全面的Greasemonkey支持,请使用 Tampermonkey 。
清除Chrome中用户脚本的混淆
Chrome本身不会 支持GreaseMonkey。无论何时加载 .user.js
文件,它都会以内容脚本。
有关Chrome中用户脚本的更多信息,请参阅本文档。
用户脚本直接复制到扩展的目录中:
// == UserScript ==
// @name Foo
// @namespace Bar
// @include *
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min。 js
// == / UserScript ==
alert(typeof $)
生成的 manifest.json
的内容如下所示:
$ b {
content_scripts:[{
exclude_globs:[],
include_globs:[ *],
js:[script.js],
matches:[http:// * / *,https:// * / *],
run_at:document_idle
}],
converted_from_user_script:true,
description:,
key:+。 ...一些关键... =,
name:Foo,
version:1.0
}
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?
解决方案 @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:
- GreaseSpot Wiki: Cross-browser userscripting
- Stack Overflow: Why doesn't jQuery work in Chrome user scripts (Greasemonkey)?
- Stack Overflow: How can I use jQuery in Greasemonkey scripts in Google Chrome?
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"
}
这篇关于Greasemonkey @require在Chrome中无法使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文