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

When I add @grant for GM_xmlhttpRequest, I get:

Error: Permission denied to access the resource 'call'

in jQuery file.
If I delete the grant, it works great.

// ==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'); }); 
+5
source share
1 answer

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

You can no longer link to jQuery landing page.

(Note that in @grant none mode (default for GM 2) unsafeWindow same as window ... But then you cannot use the GM_ functions.)


@require your own copy of jQuery; it will not conflict with the page and will load faster to load.

Do not use unsafeWindow for such things (or in general if you can help), and $(document).ready() also almost never required for Greasemonkey scripts.

Your (new) sample script will be simple:

 // ==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 mix GM_ functions and your jQuery instance without any problems.



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

+4
source

Source: https://habr.com/ru/post/1201249/


All Articles