Analyze JSONP response (from express.js server) of clients using greasemonkey

We are working on greasemonkeyscript to retrieve data from a cross-domain server. (We found code that works for a regular html site here :)

Can you make it work on greasemonkey? (possibly 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); } } 
+1
source share
1 answer

I have never used Express before, but this application looks like this:

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

which is placed in the <script> node in the landing page area .

This means that the Greasemonkey script must also place the __parseJSONPResponse function in the landing page area.

One way to do this:

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


However, it looks like you are in control of the Express application. If this is true, then do not use JSONP for this kind of thing. Use GM_xmlhttpRequest () .

app.js can 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 will be something like this:

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

All Articles