在Tampermonkey中使用jQuery

我使用的是Chrome 27.0.1453.116 m,并启用了“实验 Javascript”,但是我无法让jQuery在Tampermonkey上运行.

我努力了:

// ==UserScript==
// @name       My Fancy New Userscript
// @require    http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js
// ==/UserScript==

var $= unsafeWindow.jQuery;
var jQuery = unsafeWindow.jQuery;

但是,我得到一个错误var $= unsafeWindow.jQuery;突出显示unsafeWindow在定义之前使用unsafeWindow.如何解决这个问题?

解决方法

编辑:由于Greasemonkey 2.0不能(并且不能)使用unsafeWindow.jQuery来获取主机页面的jQuery实例.您只需使用window.jQuery,// @grant none(或@grant任何其他特殊的API,非jQuery的东西).

资料来源:
https://github.com/greasemonkey/greasemonkey/issues/1952
http://www.greasespot.net/2014/06/greasemonkey-20-release.html

Tampermonkey目前(2015年2月)似乎按照默认设置:http://tampermonkey.net/faq.php#Q404

老过时的答案如下:

Add a directive // @grant unsafeWindow. Also,if you are getting
your jQuery reference from the host window object,you won’t need the
@require line.

相关文章

1.第一步 设置响应头 header('Access-Control-Allow...
$.inArray()方法介绍 $.inArray()函数用于在数组中搜索指定的...
jquery.serializejson.min.js的妙用 关于这个jquery.seriali...
JS 将form表单数据快速转化为object对象(json对象) jaymou...
jQuery插件之jquery.spinner数字智能增减插件 参考地址:http...