Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
830 views
in Technique[技术] by (71.8m points)

javascript - Adding a @grant value breaks my Greasemonkey+jQuery script?

When I add the @grant for GM_xmlhttpRequest, I get:

Error: Permission denied to access property 'call'

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');
});
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

See "Error: Permission denied to access property 'handler'".

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

(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 your own copy of jQuery; it will not conflict with the page's and will load faster, to boot.

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>');

And you can then mix GM_ functions and your instance of jQuery with no problems.



Note: The question script has // @include about:addons.
Greasemonkey scripts will not work on the about:addons page, by design.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...