在Tampermonkey中使用jQuery [英] Using jQuery in Tampermonkey

查看:799
本文介绍了在Tampermonkey中使用jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Chrome 27.0.1453.116 m并启用了实验性Javascript",但是我无法使jQuery在Tampermonkey上运行.

I'm using Chrome 27.0.1453.116 m and have enabled "Experimental Javascript", however I'm unable to get jQuery to run on 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之前已使用它.该如何解决?

However, I get an error on the line var $ = unsafeWindow.jQuery; highlighting unsafeWindow saying unsafeWindow was used before it was defined. How to fix this?

推荐答案

由于Greasemonkey 2.0,您不会(也不能)使用unsafeWindow.jQuery获取jQuery的宿主页实例.您只需使用window.jQuery// @grant none(或@grant任何其他非jQuery所需的特殊API).

Since Greasemonkey 2.0 you do not (and cannot) use unsafeWindow.jQuery for getting the host page's instance of jQuery. You just use window.jQuery, and // @grant none (or @grant whatever other special APIs you need for non-jQuery things).

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

Sources: https://github.com/greasemonkey/greasemonkey/issues/1952
http://www.greasespot.net/2014/06/greasemonkey-20-release.html

当前(2015年2月)Tampermonkey的默认设置似乎与之类似: http://tampermonkey.net/faq.php#Q404

Tampermonkey currently (Feb 2015) seems to work similarly by default settings: http://tampermonkey.net/faq.php#Q404

以下旧的过时答案:

添加指令// @grant unsafeWindow.另外,如果你得到 您从宿主窗口对象获得的jQuery引用,则不需要 @require行.

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.

这篇关于在Tampermonkey中使用jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆