Michael Moeller
Michael Moeller

Reputation: 906

parse JSONP response (from express.js server) clientside using greasemonkey

We are working on a greasemonkeyscript to pull data from an express server cross-domain. (We found code which is working for a normal html site here: )

Can you get this to work for greasemonkey? (maybe with unsafeWindow ?)

app.js:

var express = require("express");
var app = express();
var fs=require('fs');
  var stringforfirefox = 'hi buddy!'



// in the express app for crossDomainServer.com
app.get('/getJSONPResponse', function(req, res) {

    res.writeHead(200, {'Content-Type': 'application/javascript'});
    res.end("__parseJSONPResponse(" + JSON.stringify( stringforfirefox) + ");");
});
app.listen(8001)

greasemonkeyscript:

// ==UserScript==
// @name          greasemonkeytestscript
// @namespace     http://www.example.com/
// @description   jQuery test script
// @include       *
// @require       http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js

// ==/UserScript==


function __parseJSONPResponse(data) {    alert(data); }       // ??????????

document.onkeypress = function keypressed(e){

    if (e.keyCode == 112) {
        var script = document.createElement('script');
        script.src = 'http://localhost:8001/getJSONPResponse';
        document.body.appendChild(script); // triggers a GET request
        alert(script);



    }
}

Upvotes: 1

Views: 589

Answers (1)

Brock Adams
Brock Adams

Reputation: 93473

I've never used Express before, but that app looks to be returning code like:

__parseJSONPResponse("\"hi buddy!\"");

which is placed into a <script> node in the target-page scope.

This means that the Greasemonkey script must also place the __parseJSONPResponse function in the target-page scope.

One way to do that is:

unsafeWindow.__parseJSONPResponse = function (data) {
    alert (data);
}


However, it looks like you control the Express app. If that's true, then don't use JSONP for this kind of thing. Use GM_xmlhttpRequest().

app.js might become:

var express             = require ("express");
var app                 = express ();
var fs                  = require ('fs');
var stringforfirefox    = 'hi buddy!'

app.get ('/getJSONPResponse', function (req, res) {

    res.send (JSON.stringify (stringforfirefox) );
} );

app.listen (8001)


And the GM script would be something like:

// ==UserScript==
// @name        greasemonkeytestscript
// @namespace   http://www.example.com/
// @description jQuery test script
// @include     *
// @require     http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
// @grant       GM_xmlhttpRequest
// ==/UserScript==

document.onkeypress = function keypressed (e){

    if (e.keyCode == 112) {
        GM_xmlhttpRequest ( {
            method:     'GET',
            url:        'http://localhost:8001/getJSONPResponse',
            onload:     function (respDetails) {
                            alert (respDetails.responseText);
                        }
        } );
    }
}

Upvotes: 1

Related Questions