如何在Greasemonkey脚本中包含远程javascript文件? [英] How do I include a remote javascript file in a Greasemonkey script?
问题描述
我正在尝试编写一个Greasemonkey脚本,并且想使用jQuery库来这样做,但是我不太确定如何从网址中包含jQuery来滚动.
I'm trying to write a Greasemonkey script, and would like to use the jQuery library to do so, but I'm not quite sure how I would include jQuery from a web address to get rolling.
我将如何将jQuery(来自Google的网络服务器)包含在油脂猴子脚本中,这样我就可以了:
$(document).ready(function(){
// Greasemonkey stuff here
});
我希望从此来源获取它:
I'd prefer to get it from this source:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript" charset="utf-8"></script>
更新:感谢您的帮助,答案非常有用.但是,我确实更多地利用了我的GoogleFu,并遇到了以下解决方案: http://joanpiedra.com/jquery/greasemonkey/
Update: Thanks for the help, the answers were very informative. I did, however, utilize my GoogleFu a little more and came across this solution: http://joanpiedra.com/jquery/greasemonkey/
工作就像一个魅力..只需将源代码更新到Google的jQuery托管版本即可完成.
Works like a charm.. just update the source to google's hosted version of jQuery to complete.
推荐答案
在最新版本的oilsmonkey中,推荐的方法是使用@require注释标记.
The recommended way in recent versions of greasemonkey is to use the @require comment tag.
例如
// ==UserScript==
// @name Hello jQuery
// @namespace http://www.example.com/
// @description jQuery test script
// @include *
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js
// ==/UserScript==
但是...请注意,jQuery 1.4.1和1.4.2与该方法不兼容
感谢Paul Tarjan指出了这一点.请参见 jQuery论坛线程.
Thanks Paul Tarjan, for pointing this out. See jQuery forum thread.
也请注意这些@require语义
在安装用户脚本时,Greasemonkey将下载并保留远程文件的本地缓存副本,几乎可以立即读取该副本.缓存的副本与已安装的用户脚本保存在同一文件夹中.不会监视远程文件的更改.
At the time that the user script is installed, Greasemonkey will download and keep a locally cached copy of the remote file that can be read almost instantly. The cached copy is kept in the same folder as your installed user-script. The remote file is not monitored for changes.
请注意,在编写此答案时,仅在安装时读取此@require标记.如果您编辑现有的用户脚本以添加此标记,它将被忽略.您需要卸载并重新安装用户脚本以获取更改.
Please be aware that, at the time of writing this answer, this @require tag is only read at install-time. If you edit an existing user script to add this tag, it will be ignored. You need to uninstall and re-install your user script to pick up the change.
这篇关于如何在Greasemonkey脚本中包含远程javascript文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!