如何在 Greasemonkey 中使用 jQuery?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/859024/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I use jQuery in Greasemonkey?
提问by Keira Nighly
I tried putting this line but it doesn't work:
我试着把这条线,但它不起作用:
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.js
jQuery doesn't work in Greasemonkey at all. Is there other way to use jQuery in Greasemonkey?
jQuery 在 Greasemonkey 中根本不起作用。有没有其他方法可以在 Greasemonkey 中使用 jQuery?
--
——
For all the people who have the same problem, you must upload the file to greasespot then install it from there.
对于所有遇到相同问题的人,您必须将文件上传到油脂点,然后从那里安装。
The Create New Script option wouldn't work!
创建新脚本选项不起作用!
采纳答案by Rob Kennedy
Perhaps you don't have a recent enough version of Greasemonkey. It was version 0.8 that added @require
.
也许您没有足够新的 Greasemonkey 版本。是 0.8 版添加了@require
.
// @require https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js
If you don't have 0.8, then use the technique Joan Piedra describes for manually adding a script
element to the page.
如果您没有 0.8,则使用 Joan Piedra 描述的技术手动向script
页面添加元素。
Between version 0.8 and 0.9, @require
is only processed when the script is first installed.If you change the list of required scripts, you need to uninstall your script and reinstall it; Greasemonkey downloads the required script once at installation and uses a cached copy thereafter.
版本0.8和0.9之间,@require
在第一次安装脚本时才会处理。如果更改了所需脚本列表,则需要卸载脚本并重新安装;Greasemonkey 在安装时下载一次所需的脚本,然后使用缓存副本。
As of 0.9, Greasemonkey behavior has changed (to address a tangentially related issue) so that it now loads the required scripts after every edit; reinstalling the script is no longer necessary.
从 0.9 开始,Greasemonkey 的行为发生了变化(以解决相关问题),因此现在每次编辑后都会加载所需的脚本;不再需要重新安装脚本。
回答by Henrik Heimbuerger
If you want to use jQuery on a site where it is already included, this is the way to go (inspired by BrunoLM):
如果你想在已经包含它的站点上使用 jQuery,这是要走的路(受 BrunoLM 启发):
var $ = unsafeWindow.jQuery;
I know this wasn't the original intent of the question, but it is increasingly becoming a common case and you didn't explicitly exclude this case. ;)
我知道这不是问题的初衷,但它正日益成为一种常见情况,您没有明确排除这种情况。;)
回答by Dan Lew
There's absolutely nothing wrong with including the entirety of jQuery within your Greasemonkey script. Just take the source, and place it at the top of your user script. No need to make a script tag, since you're already executing JavaScript!
在您的 Greasemonkey 脚本中包含整个 jQuery 绝对没有错。只需获取源代码,并将其放在用户脚本的顶部即可。无需制作脚本标签,因为您已经在执行 JavaScript!
The user only downloads the script once anyways, so size of script is not a big concern. In addition, if you ever want your Greasemonkey script to work in non-GM environments (such as Opera's GM-esque user scripts, or Greasekit on Safari), it'll help not to use GM-unique constructs such as @require.
无论如何,用户只下载一次脚本,因此脚本的大小不是一个大问题。此外,如果您希望您的 Greasemonkey 脚本在非 GM 环境中工作(例如 Opera 的 GM 风格用户脚本,或 Safari 上的 Greasekit),最好不要使用 GM 特有的结构,例如 @require。
回答by Ryan
Rob's solution is the right one--use @require
with the jQuery library and be sure to reinstall your script so the directive gets processed.
Rob 的解决方案是正确的——@require
与 jQuery 库一起使用,并确保重新安装您的脚本,以便处理指令。
One thing I think is worth adding is that you can use jQuery normally once you have included it in your script, except for AJAX methods. By default jQuery looks for XMLHttpRequest, which doesn't exist in the Greasemonkey context. I wrote about a workaroundwhere you create a wrapper for GM_xmlhttpRequest (the Greasemonkey version of XHR) and use jQuery's ajaxSetup()
to specify your wrapped version as the default. Once you do this, you can use $.get
and $.post
as usual.
我认为值得补充的一件事是,一旦将 jQuery 包含在脚本中,您就可以正常使用它,但 AJAX 方法除外。默认情况下,jQuery 查找 XMLHttpRequest,它在 Greasemonkey 上下文中不存在。我写了一个解决方法,您可以为 GM_xmlhttpRequest(XHR 的 Greasemonkey 版本)创建一个包装器,并使用 jQueryajaxSetup()
将您的包装版本指定为默认版本。一旦你这样做,你可以使用$.get
和$.post
往常一样。
You may also have problems with jQuery's $.getJSON
because it loads JSONP using <script>
tags. This leads to errors because jQuery defines the callback function in the scope of the Greasemonkey window, and the loaded scripts looks for the callback in the scope of the main window. Your best bet is to use $.get
instead and parse the result with JSON.parse()
.
您可能还会遇到 jQuery 的问题,$.getJSON
因为它使用<script>
标签加载 JSONP 。这会导致错误,因为 jQuery 在 Greasemonkey 窗口范围内定义回调函数,加载的脚本在主窗口范围内查找回调。最好的办法是$.get
改用并用JSON.parse()
.
回答by Conley Owens
Update: As the comment below says, this answer is obsolete.
更新:正如下面的评论所说,这个答案已经过时了。
As everyone else has said, @require only gets run when the script has installed. However, you should note as well that currently jQuery 1.4.* doesn't work with greasemonkey. You can see here for details: http://forum.jquery.com/topic/importing-jquery-1-4-1-into-greasemonkey-scripts-generates-an-error
正如其他人所说,@require 仅在安装脚本后运行。但是,您还应该注意,目前 jQuery 1.4.* 不适用于greasemonkey。您可以在此处查看详细信息:http: //forum.jquery.com/topic/importing-jquery-1-4-1-into-greasemonkey-scripts-generates-an-error
You will have to use jQuery 1.3.2 until things change.
您将不得不使用 jQuery 1.3.2 直到事情发生变化。
回答by Filype
If you are using chrome you have to opt for an alternative as Chromium does not support @require
.
如果您使用的是 chrome,则必须选择替代方案,因为 Chromium 不支持@require
.
Source: The Chromium Project - User scripts
More details and alternatives on How can I use jQuery in Greasemonkey scripts in Google Chrome?
有关如何在 Google Chrome 中的 Greasemonkey 脚本中使用 jQuery 的更多详细信息和替代方法?
回答by Auspex
You can create a new script using the New User Script in Greasemonkey but you have to edit the config.xml file inside the gm_scripts folder.
您可以使用 Greasemonkey 中的新用户脚本创建新脚本,但您必须编辑 gm_scripts 文件夹中的 config.xml 文件。
Your config.xmlfile should have a similar syntax as this:
您的config.xml文件应具有与此类似的语法:
<Script filename="jquery_test.user.js" name="jQuery Test" namespace="http://www.example.com/jQueryPlay/" description="Just a test" enabled="true" basedir="jquery_test">
<Include>http://*</Include>
<Require filename="jquery.js"/>
</Script>
Notice the <Require>
tag.
注意<Require>
标签。
In your script you can use direct jQuery syntax. Make sure you have the require tag in the Greasemonkey header. Here is a Hello World example:
在您的脚本中,您可以使用直接的 jQuery 语法。确保您在 Greasemonkey 标头中有 require 标签。这是一个 Hello World 示例:
// ==UserScript==
// @name Test jQuery
// @namespace http://www.example.com/jQueryPlay/
// @description Just a test
// @include http://*
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.js
// ==/UserScript==
$(document).ready(function() {
alert("Hello world!");
});
Remember that after modifying the config.xmlyou have to restart your browser for Greasemonkey to reload the settings again.
请记住,修改config.xml后,您必须重新启动浏览器,以便 Greasemonkey 重新加载设置。
Also note that you need to copy the jquery.js file to your script directory folder in order for this to work. I tested this, and it only works if you actually copy the file manually there.
另请注意,您需要将 jquery.js 文件复制到脚本目录文件夹才能使其正常工作。我对此进行了测试,它仅在您实际手动复制文件时才有效。
Happy jQuerying!
快乐的jQuery!
回答by baptx
the @require meta does not work when you want to unbind events on a webpage using jQuery, you have to use a jQuery library included in the webpage and then get it in Greasemonkey with var $ = unsafeWindow.jQuery;
How do I unbind jquery event handlers in greasemonkey?
当您想使用 jQuery 在网页上解除绑定事件时,@require 元不起作用,您必须使用网页中包含的 jQuery 库,然后使用如何在 Greasemonkey 中解除绑定 jquery 事件处理程序在Greasemonkey 中获取它var $ = unsafeWindow.jQuery;
?
回答by BrunoLM
You might get Component unavailable
if you import the jQuery script directly.
Component unavailable
如果您直接导入 jQuery 脚本,您可能会得到。
Maybe it's what @Conley was talking about...
也许这就是@Conley 所说的......
You can use
您可以使用
@require http://userscripts.org/scripts/source/85365.user.js
which is an modified version to work on Greasemonkey, and then get the jQuery object
这是在 Greasemonkey 上工作的修改版本,然后获取 jQuery 对象
var $ = unsafeWindow.jQuery;
$("div").css("display", "none");
回答by Dunstkreis
@require
is NOTonly processed when the script is first installed!
On my observations it is proccessed on the first execution time! So you caninstall a script via Greasemonkey's command for creating a brand-new script. The only thing you have to take care about is, that there is no page reload triggered, befor you add the @require
part. (and save the new script...)
@require
是不是第一次安装脚本时,只处理!根据我的观察,它是在第一次执行时处理的!所以你可以通过 Greasemonkey 的命令安装一个脚本来创建一个全新的脚本。您唯一需要注意的是,在添加@require
部件之前没有触发页面重新加载。(并保存新脚本...)