greasemonkey, jquery.ajax() ,and jsonp

631 views
Skip to first unread message

Ben Hest

unread,
Jun 18, 2014, 2:06:16 PM6/18/14
to greasemon...@googlegroups.com
I have a userscript with an jquery.ajax() call:

// ==UserScript==
// @name        myscripttest
// @namespace   myscripttest
// @version     1
// @include     *
// @require     http://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js
// @grant       GM_addStyle
// @grant       GM_getResourceText
// @grant         none
// ==/UserScript==

var partNumber = "1234";
var apiURL = "http://myapi.com"


 var ajaxConfig = {
        url: apiURL,
        type: "POST",
        data: {
            'k': partNumber
        },
        dataType: 'jsonp'
 }

    var jqxhr = $.ajax( ajaxConfig )
        .done(function(msg) {
            console.log( msg);
        })
        .fail(function() {
            alert( "error" );
        })
        .always(function(w, x, y) {
            console.log( "complete" );
            console.log(x, y);
        })
;


I'm make the ajax call to an api on a domain which allows CORS (Access-Control-Allow-Origin: *).
If I'm playing in the security restricted sandbox (for ex needing GM_addStyle, or GM_getResourceText), my ajax calls to consume the jsonp are met with this error found in the console:

ReferenceError: jQuery18107040466994572055_1403112877434 is not defined

The log line in the .always() callback outputs this:

parsererror
Error: jQuery18107040466994572055_1403112877434 was not called

If I simply avoid needing the custom GM_ functions and employ @grant none the call is performed as expected, returning a usable json object with no errors.  Though I have not used GM_addStyle in the example code, I would like to use it and others if I can.  It's a rather abitrary requirement to 'need' $.ajax() as opposed to using the a gm_xmlhttprequest, but I'd prefer that part to be as portable as possible as this is rapid prototyping against a live site.  It may be a bit backwards, but the oddity remains.

So the main questions are: 
  • Why does a $.ajax() jsonp call not work in the greasemonkey sandbox with GM_ functions, but does under the the @grant none condition?
  • Is there a "right way" to make this work?

I've tried multiple incantations and settings, but have yet to arrive at a solution.  Any help would be much appreciated.

-Ben

Anthony Lieuallen

unread,
Jun 18, 2014, 4:44:53 PM6/18/14
to greasemon...@googlegroups.com
On Wed, Jun 18, 2014 at 2:06 PM, Ben Hest <bombl...@gmail.com> wrote:
> ...
> // @grant GM_addStyle
> // @grant GM_getResourceText
> // @grant none
> ...

Saying both grant none plus grant something else is confusing at best
and not likely to do what you want

> Why does a $.ajax() jsonp call not work in the greasemonkey sandbox with GM_
> functions, but does under the the @grant none condition?
> Is there a "right way" to make this work?

What version of Greasemonkey? Did you read the release notes for 2.0?
http://www.greasespot.net/2014/06/greasemonkey-20-release.html

Ben Hest

unread,
Jun 18, 2014, 5:49:01 PM6/18/14
to greasemon...@googlegroups.com
I apologize for the confusion.  I guess the text styling didn't make it through or clear enough.  The question is rooted from this: if only @grant none is present, the userscript will work.  If only the @grant GM_somefunctions are included, the errors discussed pop up.  Is there a fundamental reason why one works and the other doesn't when making a CORS compliant JSONP $.ajax() call?

I am using 1.15 on FF30.  I didn't see that 2.0 was out until you mentioned it.  So far it hasn't been pushed through the addon updates yet.  I'll check into it on 2.0 as well.

Thanks,
Ben


On Wednesday, June 18, 2014 3:44:53 PM UTC-5, Anthony Lieuallen wrote:
On Wed, Jun 18, 2014 at 2:06 PM, Ben Hest <bombl...@gmail.com> wrote:
> ...

Anthony Lieuallen

unread,
Jun 18, 2014, 10:30:54 PM6/18/14
to greasemon...@googlegroups.com
On Wed, Jun 18, 2014 at 5:49 PM, Ben Hest <bombl...@gmail.com> wrote:
> Is there a fundamental reason why one works and the other doesn't when
> making a CORS compliant JSONP $.ajax() call?

Yes. If you're actually doing JSONP. Combining CORS and JSONP
doesn't make sense. If you ask for privileged functions, you run in a
sandbox isolated from content. If you create a <script> node in the
content (i.e. JSONP) it cannot access the sandbox.
Reply all
Reply to author
Forward
0 new messages