添加@grant值会破坏我的Greasemonkey + jQuery脚本吗? [英] Adding a @grant value breaks my Greasemonkey+jQuery script?

查看:82
本文介绍了添加@grant值会破坏我的Greasemonkey + jQuery脚本吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我为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');
});

推荐答案

请参见错误:拒绝访问属性'handler'的权限" .

您不能再像这样调用目标页面的jQuery.

You can no longer invoke the target-page's jQuery like that.

(请注意,在@grant none模式下(GM 2的默认设置),unsafeWindowwindow相同……但是,您不能使用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屋!

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