1

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



    }
}
Community
  • 1
  • 1
Michael Moeller
  • 896
  • 4
  • 12
  • 27
  • @BrockAdams app.js is the mentioned express server. We would like to alert a string we receive from our express server when hitting F1 in our firefox while browsing the internet. We need only one line of code were the ??????? are – Michael Moeller Mar 02 '13 at 23:06
  • forget the "alert(script);" command, we put it there just to see the onkeypress work. we can delete it when you whish – Michael Moeller Mar 02 '13 at 23:09

1 Answers1

1

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);
                        }
        } );
    }
}
Brock Adams
  • 90,639
  • 22
  • 233
  • 295