我们正在开发一个greasemonkeyscript来从快速服务器跨域提取数据。 (我们发现代码适用于普通的html网站here: )
你可以让它适用于greasemonkey吗? (也许是不安全的窗户?)
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);
}
}
答案 0 :(得分:1)
之前我从未使用过 Express ,但该应用程序看起来像是返回代码:
__parseJSONPResponse("\"hi buddy!\"");
放置在目标网页范围的<script>
节点中。
这意味着Greasemonkey脚本还必须将__parseJSONPResponse
函数放在目标页面范围内。
一种方法是:
unsafeWindow.__parseJSONPResponse = function (data) {
alert (data);
}
但是,您似乎可以控制Express应用。如果这是真的,那么不要将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脚本将类似于:
// ==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);
}
} );
}
}