解析JSONP响应(从express.js服务器)客户端使用greasemonkey [英] parse JSONP response (from express.js server) clientside using greasemonkey

查看:357
本文介绍了解析JSONP响应(从express.js服务器)客户端使用greasemonkey的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用greasemonkeyscript从快速服务器跨域提取数据。
(我们发现代码适用于正常的html网站这里

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

你能得到这个工作greasemonkey吗?
(可能使用unsafeWindow吗?)

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:

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



    }
}


推荐答案

我从来没有使用过Express ,但是该应用看起来像是返回代码:

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

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

放置在< script>

这意味着Greasemonkey脚本还必须将 __ parseJSONPResponse code>

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

一种方法是:

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








b $ b

但是,它看起来像你控制快速应用程序。如果这是真的,那么不要使用JSONP这种事情。使用 GM_xmlhttpRequest()

app.js 可能会成为:

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)



GM脚本如下:


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

这篇关于解析JSONP响应(从express.js服务器)客户端使用greasemonkey的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆