添加@grant值会破坏我的Greasemonkey + jQuery脚本吗? [英] Adding a @grant value breaks my Greasemonkey+jQuery script?
问题描述
当我为GM_xmlhttpRequest添加@grant时,我得到:
When I add the @grant for GM_xmlhttpRequest, I get:
错误:拒绝访问属性通话"的权限
Error: Permission denied to access property 'call'
jQuery文件中的
.
如果我取消了赠款,它就可以正常工作.
in the jQuery file.
If I remove the grant, it works fine.
// ==UserScript==
// @name Dimi Test
// @namespace Dimi
// @include about:addons
// @version 1
// @grant GM_xmlhttpRequest
// @include http://*.myDomain.*/*
// ==/UserScript==
var $J = unsafeWindow.jQuery;
$J(unsafeWindow.document).ready(function(){
alert('Hello');
});
推荐答案
您不能再像这样调用目标页面的jQuery.
You can no longer invoke the target-page's jQuery like that.
(请注意,在@grant none
模式下(GM 2的默认设置),unsafeWindow
与window
相同……但是,您不能使用GM_
函数.)
(Note that in @grant none
mode (the default as of GM 2), unsafeWindow
is the same as window
... But, then you can't use GM_
functions.)
@require
您自己的jQuery副本;它不会与页面冲突,并且加载速度会更快.
@require
your own copy of jQuery; it will not conflict with the page's and will load faster, to boot.
请勿将unsafeWindow
用于此类操作(或者,如果可以的话,请完全使用),并且Greasemonkey脚本几乎也不需要$(document).ready()
.
Do not use unsafeWindow
for things like this (or at all, if you can help it), and $(document).ready()
is also almost never needed for Greasemonkey scripts.
您的(新的)示例脚本将仅仅是:
Your (new) sample script would merely be:
// ==UserScript==
// @name Dimi Test
// @namespace Dimi
// @version 1
// @grant GM_xmlhttpRequest
// @include about:addons
// @include http://*.myDomain.*/*
// @require http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// ==/UserScript==
$("body").prepend ('<h1>Hello World!</h1>');
然后您可以毫无问题地将GM_
函数和jQuery实例混合使用.
And you can then mix GM_
functions and your instance of jQuery with no problems.
注意:问题脚本具有// @include about:addons
.
按设计,Greasemonkey脚本将无法在about:addons
页面上使用.
Note: The question script has // @include about:addons
.
Greasemonkey scripts will not work on the about:addons
page, by design.
这篇关于添加@grant值会破坏我的Greasemonkey + jQuery脚本吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!