在 Tampermonkey 中使用 jQuery
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17255303/
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
Using jQuery in Tampermonkey
提问by Michael Jarvis
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.
我正在使用 Chrome 27.0.1453.116 m 并启用了“实验性 Javascript”,但是我无法让 jQuery 在 Tampermonkey 上运行。
I have tried:
我试过了:
// ==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;
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?
但是,我在var $ = unsafeWindow.jQuery;
高亮显示unsafeWindow
unsafeWindow 在定义之前使用了该行时收到错误消息。如何解决这个问题?
采纳答案by tuff
EDIT: 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).
编辑:从 Greasemonkey 2.0 开始,您不会(也不能)unsafeWindow.jQuery
用于获取主机页面的 jQuery 实例。您只需使用window.jQuery
, and // @grant none
(或@grant
任何其他非 jQuery 事物所需的特殊 API)。
Sources:
https://github.com/greasemonkey/greasemonkey/issues/1952
http://www.greasespot.net/2014/06/greasemonkey-20-release.html
来源:https:
//github.com/greasemonkey/greasemonkey/issues/1952
http://www.greasespot.net/2014/06/greasemonkey-20-release.html
Tampermonkey currently (Feb 2015) seems to work similarly by default settings: http://tampermonkey.net/faq.php#Q404
Tampermonkey 当前(2015 年 2 月)在默认设置下似乎工作类似:http://tampermonkey.net/faq.php#Q404
old outdated answer below:
旧的过时答案如下:
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.
添加指令
// @grant unsafeWindow
。此外,如果您从宿主窗口对象获取 jQuery 引用,则不需要该@require
行。