GreaseMonkey脚本里想使用jquery库该怎么办呢? 参考这几篇文章
- http://joanpiedra.com/jquery/greasemonkey/
- http://abeautifulsite.net/notebook/90
- http://greasemonkey.mozdev.org/authoring.html
两种方案:
- 脚本加载后在html的head里插入一个链接到外部jquery的script标签
- 利用greasemonkey的require标签
但是如果只是利用新建用户脚本,require一直没有生效,无法载入外部脚本,参考这个:
Surely in the year or more since the first betas of v0.8 came out it's no
longer necessary to do this? You can instead just specify any files you want
to get included in the metadata e.g. // @require
http://jqueryjs.googlecode.com/files/jquery-1.3.2.js which will execute that
file just before your script runs. There's a bit of a big hole in GM though
as if you use the "New User Script" option in menu then this doesn't work at
all - GM only imports anything referenced by @require the first time you
install the script, and the "New User Script" option creates and installs a
default user script for you, so even if you add the @require it fails. It's
not much harder to create a .user.js file manually and browse to it... took
me ages to find out I needed to though!
解决就是,“安装本地的脚本”, 你的脚本在/home/xx/a.user.js, 你就把这个脚本拖动到浏览器窗口,就会弹出安装提示,安装即可。 一个测试的脚本
// ==UserScript==
// @name jQuery
// @namespace www.notsobad.cn
// @description using jquery
// @require http://jqueryjs.googlecode.com/files/jquery-1.3.2.js
// @include *
// ==/UserScript==
var log = (unsafeWindow.console && console.log) || GM_log;
log($('a'));
安装之后就可以使用jquery了,但是这个jquery似乎不能被其它gm脚本使用(?) 我的gm_scripts的目录,可以看到
535 ~/.mozilla/firefox/lljsqryp.default/gm_scripts>tree
.
|-- config.xml
|-- douban
| |-- douban.user.js
| `-- douban.user.js~
|-- douban_highlight_friends
| |-- autoupdatehelper.js
| |-- douban_highlight_friends.user.js
| |-- jquery.js
| |-- jquerydouban.js
| |-- oauth.js
| `-- sha1.js
|-- google_reader
| `-- google_reader.user.js
|-- google_reader_full_feed_
| |-- google_reader_full_feed_.user.js
| `-- google_reader_full_feed_.user.js~
|-- googlekingkong
| `-- googlekingkong.user.js
|-- jquery
| |-- jquery-132.js
| `-- jquery.user.js
`-- xiaonei
|-- xiaonei.user.js
`-- xiaonei.user.js~
可以看到 require http://jqueryjs.googlecode.com/files/jquery-1.3.2.js, 这个jquery-1.3.2.js,已经被缓存起来了