I'm building an extension to get the POST request in Firefox. I read through the documentation for intercepting page loads and HTTP observers, but still couldn't manage to get the specific POST data on a page load (ex: data1=50&sdata2=0&data3=50).
I looked into TamperData's code, and found that they used stream.available() and stream.read(1). However, I couldn't get these commands to work with my code.
Currently my code looks like this:
var ObserverTest = {
observe: function(subject, topic, data) {
if (topic == 'http-on-modify-request') {
var httpChannel = subject.QueryInterface(Components.interfaces.nsIHttpChannel);
}
if (topic == "http-on-examine-response") {
var newListener = new TracingListener();
subject.QueryInterface(Ci.nsITraceableChannel);
newListener.originalListener = subject.setNewListener(newListener);
}
},
register: function() {
var observerService = Components.classes["#mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
observerService.addObserver(ObserverTest, "http-on-modify-request", false);
observerService.addObserver(ObserverTest, "http-on-examine-response", false);
},
unregister: function() {
var observerService = Components.classes["#mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
observerService.removeObserver(ObserverTest, "http-on-modify-request");
observerService.removeObserver(ObserverTest,"http-on-examine-response");
}
}
window.addEventListener("load", ObserverTest.register, false);
window.addEventListener("unload", ObserverTest.unregister, false);
//Helper function for XPCOM instanciation (from Firebug)
function CCIN(cName, ifaceName) {
return Cc[cName].createInstance(Ci[ifaceName]);
}
// Copy response listener implementation.
function TracingListener() {
this.originalListener = null;
this.receivedData = []; // array for incoming data.
}
TracingListener.prototype = {
onDataAvailable: function(request, context, inputStream, offset, count) {
var binaryInputStream = CCIN("#mozilla.org/binaryinputstream;1", "nsIBinaryInputStream");
var storageStream = CCIN("#mozilla.org/storagestream;1", "nsIStorageStream");
var binaryOutputStream = CCIN("#mozilla.org/binaryoutputstream;1", "nsIBinaryOutputStream");
var stream = Components.classes["#mozilla.org/scriptableinputstream;1"].createInstance(Components.interfaces.nsIScriptableInputStream);
stream.init(binaryInputStream);
binaryInputStream.setInputStream(inputStream);
storageStream.init(8192, count, null);
binaryOutputStream.setOutputStream(storageStream.getOutputStream(0));
// Copy received data as they come.
var data = binaryInputStream.readBytes(count);
this.receivedData.push(data);
binaryOutputStream.writeBytes(data, count);
this.originalListener.onDataAvailable(request, context, storageStream.newInputStream(0), offset, count);
},
onStartRequest: function(request, context) {
this.originalListener.onStartRequest(request, context);
},
onStopRequest: function(request, context, statusCode)
{
// Get entire response
var responseSource = this.receivedData.join();
this.originalListener.onStopRequest(request, context, statusCode);
},
QueryInterface: function (aIID) {
if (aIID.equals(Ci.nsIStreamListener) || aIID.equals(Ci.nsISupports)) {
return this;
}
throw Components.results.NS_NOINTERFACE;
}
}
First of all, the "http-on-examine-response" and TracingListener isn't required at all. This stuff would have merit if you wanted to do something with the response, but you're after data in the request, so topic == 'http-on-modify-request' it is.
The following function (untested, but copied from one of my extensions and cleaned up a bit) demonstrates how to get post data. The function is assumed to be called from http-on-modify-request.
const ScriptableInputStream = Components.Constructor(
"#mozilla.org/scriptableinputstream;1",
"nsIScriptableInputStream",
"init");
function observeRequest(channel, topic, data) {
let post = null;
if (!(channel instanceof Ci.nsIHttpChannel) ||
!(channel instanceof Ci.nsIUploadChannel)) {
return post;
}
if (channel.requestMethod !== 'POST') {
return post;
}
try {
let us = channel.uploadStream;
if (!us) {
return post;
}
if (us instanceof Ci.nsIMultiplexInputStream) {
// Seeking in a nsIMultiplexInputStream effectively breaks the stream.
return post;
}
if (!(us instanceof Ci.nsISeekableStream)) {
// Cannot seek within the stream :(
return post;
}
let oldpos = us.tell();
us.seek(0, 0);
try {
let is = new ScriptableInputStream(us);
// we'll read max 64k
let available = Math.min(is.available(), 1 << 16);
if (available) {
post = is.read(available);
}
}
finally {
// Always restore the stream position!
us.seek(0, oldpos);
}
}
catch (ex) {
Cu.reportError(ex);
}
return post;
}
Depending on your use case, you might want to check if the us instanceof e.g. nsIMIMEInputStream or nsIStringInputStream for special handling or fast-paths...
You'd call it from your observer like:
observe: function(subject, topic, data) {
if (topic == 'http-on-modify-request') {
observeRequest(subject, topic, data);
}
},
Related
The closest issue I've found to mine is here. I believe I'm getting this error from how my .end() calls are set up. Here's the code we're working with:
app.get('/anihome',function(req,res){
var context = {};
function renderPage(context) {
res.render('anihome',context);
}
function addRequestToPage(text) {
context.data = text.toString('utf8');
context.info = JSON.parse(text);
return context;
}
function addAnimeToPage(text) {
context.anime = JSON.parse(text);
return context;
}
function addAnimeRequest(context) {
var options2 = {
host: 'anilist.co',
path: '/api/anime/20631?access_token=' + context.info.access_token,
method: 'GET'
};
https.request(options2, function(restRes) {
restRes.on('data',function(jsonResult) {
//context.anime = JSON.parse(jsonResult);
//console.log(JSON.parse(jsonResult));
console.log(context);
renderPage(context);
});
}).end();
}
function addHeaderRequest(context) {
var options = {
host: 'anilist.co',
path: '/api/auth/access_token?grant_type=client_credentials&client_id='
+ clientID + '&client_secret=' + secretKey,
method: 'POST'
};
https.request(options, function(restRes) {
restRes.on('data', function(jsonResult) {
context = addRequestToPage(jsonResult);
addAnimeRequest(context);
});
}).end();
}
addHeaderRequest(context);
});
I've tried setting up one of the .end()s with a callback, .end(addAnimeRequest(context));, which leaves me with a socket hang up error, so presumably something in my addAnimeRequest function is taking too long?
Is there a better way to make multiple requests to the same website with different options? I'm pretty new to Node.js.
The data event can be emitted more than once. You would need to add a listener for the end event and then pass in all of your data. Example:
https.request(options2, function(restRes) {
var buf = ''
restRes.on('data',function(jsonResult) {
//context.anime = JSON.parse(jsonResult);
//console.log(JSON.parse(jsonResult));
buf += jsonResult
});
restRes.on('end', function() {
// TODO JSON.parse can throw
var context = JSON.parse(buf)
renderPage(context)
})
}).end();
I'm using Node to run this code
but when I run this code get the undefined return from leadid variable in get function :
call = {};
call.hangup = {
get: function(isOpen, ami, elastic, validator, growly, mysql, request){
var self = call.hangup;
this.isOpen = isOpen;
this.ami = ami;
this.elastic = elastic;
this.validator = validator;
this.growly = growly;
this.mysql = mysql;
this.request = request;
if(isOpen)
{
ami.on('hangup', function(evt){
var cause_status = parseInt(evt.cause);
var message = self.causeStatus(cause_status);
var channel = evt.channel;
var isDAHDI = validator.contains(channel,"DAHDI");
if((isDAHDI)&&(cause_status == 16))
{
var mobile = evt.calleridnum;
if(validator.isLength(mobile,11))
{
if(validator.matches(mobile,/^09/i))
{
var txtMessage = "";
var sending = "";
var leadid = self.searching(mobile, mysql, validator);
retrun leadid; /// get the undefined !!!!
}
}
}
});
}else {
console.log("Hangup's Event is OFF !");
}
},
searching: function(number, mysql, validator){
this.number = number;
this.mysql = mysql;
this.validator = validator;
var query = "{sql ...}";
mysql.query(query, function(err, rows, fields) {
if (err) throw err;
if(!validator.isNull(rows))
{
return rows[0].leadid;
}else {
return false;
}
});
},
};
module.exports = call;
this is how I call it in main file:
var call = require('./call');
call.hangup.get(true, ami, client, validator, growly, connection, request);
in other hands when I call this function (searching) in the main file
new call.hangup.searching(number, connection, validator);
it's work correctly
how can I fix it ?
You need to keep in mind that JavaScript is asynchronous.
When searching is called the database query is issued asynchronously and the function returns before the database would give you a result. In other words your searching function does not give you the result from
function(err, rows, fields) {
if (err) throw err;
if(!validator.isNull(rows))
{
return rows[0].leadid;
}else {
return false;
}
}
as you would desire,
searching: function(number, mysql, validator){
this.number = number;
this.mysql = mysql;
this.validator = validator;
var query = "{sql ...}";
mysql.query(query, function(err, rows, fields) {
if (err) throw err;
if(!validator.isNull(rows))
{
return rows[0].leadid;
}else {
return false;
}
});
--- > This is reached at the end of the call and nothing is returned },
};
it does not return explicitly anything, hence the undefined.
You should either use promises or pass in searching a callback function that will be called when your database query returns.
So your solution should look like this:
get: function (...){
...
self.searching(mobile, mysql, validator, whatIwantToDoAfterValidation)
},
searching: function(number, mysql, validator, callback){
...
mysql.query(..., callback){
if(!validator.isNull(rows))
{
callback(rows[0].leadid);
}else {
callback(false);
}
}
},
whatIwantToDoAfterValidation: function(leadid){
...do whatever you want with leadid...
}
Take a look at jquery's promises to do the same thing with promises.
I have made an IRC bot for purely learning purposes but I have a Minecraft server that I use an API to get the status back as JSON. Now I have made the code and it works but for some reason when I try and use a return on the function so I can get the content it seems to not work?
So I have the two functions below:
function getservers(name) {
if (name == "proxy") {
var Request = unirest.get(proxy);
Request.header('Accept', 'application/json').end(function (response) {
main = response["raw_body"];
data = JSON.parse(main);
console.log(data["motd"]);
return data.motd;
});
} else if (name == "creative") {
var Request = unirest.get(creative);
Request.header('Accept', 'application/json').end(function (response) {
main = response["raw_body"];
data = JSON.parse(main);
return data;
});
} else if (name == "survival") {
var Request = unirest.get(survival);
Request.header('Accept', 'application/json').end(function (response) {
main = response["raw_body"];
data = JSON.parse(main);
return data;
});
}
}
// Main logic:
function parsemessage(msg, to) {
// Execute files
function pu(o,t,f){if(o)throw o;if(f)throw f;bot.say(to,t)}
if (msg.substring(0,1) == pre) {
// Get array
msgs = msg.split(' ');
console.log(msgs[0]);
// Run Login
if (msgs[0] == pre+"help") {
bot.say(to, "Help & Commands can be found here: https://server.dannysmc.com/bots.html");
} else if (msgs[0] == pre+"status") {
// Get status of server, should return online/offline - player count for each server - motd
server = getservers("proxy");
console.log(server);
/*var data = '';
var Request = unirest.get('https://mcapi.us/server/status?ip=185.38.149.35&port=25578');
Request.header('Accept', 'application/json').end(function (response) {
main = response["raw_body"];
data = JSON.parse(main);
});
} else if (msgs[0] == pre+"players") {
// Should return the player list for each server
} else if (msgs[0] == pre+"motd") {
// Should return the message of the day.
} else if (msgs[0] == pre+"ip") {
bot.say(to, "ShinexusUK IP Address: shinexusuk.nitrous.it");
} else if (msgs[0] == pre+"rules") {
}
}
}
The code in the getservers() function works, when I do the
console.log(data["motd"]);
It outputs my servers message of the day. But when I do return
data.motd
(same as data["motd"]?) The code that calls the function is here
server = getservers("proxy");
console.log(server);
Please note this is a node.js code and it contains many files so i can't exactly paste it. So here is the link to the github repo with the whole node application: Here
When the function getservers is called, it makes an asynchronous request and return nothing.
Then the callback is fired with the response of that request as parameter.
Note that the function getservers will end before the end callback of your request is called
(simplified version)
function getservers(name) {
var Request = unirest.get(proxy);
Request.header('Accept', 'application/json').end(function (response) {
main = response["raw_body"];
data = JSON.parse(main);
console.log(data["motd"]);
return data.motd;
});
// nothing returned here
}
What you need is a function callback that will be called after you got the response.
function getservers(name, callback) { // callback added
var Request = unirest.get(proxy);
Request.header('Accept', 'application/json').end(function (response) {
main = response["raw_body"];
data = JSON.parse(main);
console.log(data["motd"]);
callback(data.motd); // fire the callback with the data as parameter
});
// nothing returned here
}
And then you can use your function like this :
getservers("proxy", function(server){
console.log(server);
....
})
I'm trying to run this code:
function smConnect() {
ws = new WebSocket('ws://127.0.0.1:1805/');
delete ws.URL;
ws.onopen = function(response) {};
ws.onmessage = function(response) {};
ws.onclose = function(response) {};
ws.onerror = function(error) {};
}
smConnect();
ws.send('message', 'hi');
But it returns me this error:
Uncaught InvalidStateError: Failed to execute 'send' on 'WebSocket': Still in CONNECTING state.
What could be the problem?
You could do it like this, it adds some logging, the sending of the info you can handle outside of your constructor, and you would also have abstracted away the SocketWrapper in its own namespace (ok, yes, it's in window now :))
You can check the dev tools (F12 in most browsers) to see whats happening in the logs/error, eg: here it throws an error, as there is no socket available :)
And you are not required to give a value for all events, just the ones you need (your case onopen + maybe onmessage?)
(inside the defined callbacks, this would point towards the socket, not the SocketWrapper, the SocketWrapper also doesn't offer the ws variable, it's kind of private, but it should do i guess)
There is a send method on the SocketWrapper, that throws an error, when you send to a closed stream, but in case it wasn't opened yet, it will queue the messages till it gets opened and then empty the queue to the websocket (so in a sence, you are not required to set the onopen callback, just adding it using the send method should be fine ;)
(function(nameSpace) {
function createMethod(method, options, stateCallback) {
var that = this;
this[method] = function() {
if (stateCallback && stateCallback.apply) {
stateCallback(method);
}
console.info(method);
if (options[method] && options[method].apply) {
options[method].apply(that, arguments);
}
};
}
function SocketWrapper(options) {
var ws,
events = ['onopen', 'onmessage', 'onclose', 'onerror'],
i, len, prop = {
opened: false,
closed: false,
error: false
},
method;
if (typeof options === 'undefined' || !options) {
throw 'ArgumentException: please add default constructor options';
}
this.queue = [];
this.onEventTrigger = function(eventName) {
var i, len;
if (eventName === 'onopen') {
prop.opened = true;
prop.closed = false;
// openend send queue
if (this.queue.length > 0) {
for (i = this.queue.length; --i >= 0;) {
this.send.apply(this, this.queue[0]);
this.queue.splice(0, 1);
}
}
}
if (eventName === 'onerror') {
prop.error = true;
}
if (eventName === 'onclosed') {
prop.opened = false;
prop.closed = true;
}
};
this.init = function() {
var cb = this.onEventTrigger.bind(this);
ws = new WebSocket(options.url);
for (i = 0; i < events.length; i++) {
method = events[i];
createMethod.apply(ws, [method, options, cb]);
}
};
this.send = function() {
if (prop.closed) {
throw 'InvalidOperation: Cannot send messages to a closed Websocket!';
}
if (!prop.opened) {
this.queue.push(arguments);
} else {
ws.send.apply(ws, arguments);
}
};
this.init();
return this;
}
window.SocketWrapper = SocketWrapper;
}(window));
var socket = new window.SocketWrapper({
url: 'ws://127.0.0.1:1805',
onopen: function() {
this.send('message', 'hi');
},
onmessage: function() {
console.log(arguments);
},
onclose: function() {
socket = null;
},
onerror: function() {
console.log('error occured, oh no!');
console.error(arguments);
}
});
socket.send('i am message send to soon, but since i check the state of the ws object, i will be queued and send when appropriate');
I have 3 ajax call in one function and checkAjaxCompletion which checks each ajax completion flag.
What the code below does is send multiple separate ajax calls and interval method checks completion flags to determine whether to proceed or keep interval. (I know clearInterval is not shown but the point is I want to use something other than interval)
Current code is:
function manyAjax() {
setInterval( function() { checkAjaxCompletion(); } , 200);
ajax1();
ajax2();
ajax3();
}
function ajax1() {
//send ajax request to server and if success set flag to 1. Default is 0. Error is 2.
}
function ajax2() {
//send ajax request to server and if success set flag to 1. Default is 0. Error is 2.
}
function ajax3() {
//send ajax request to server and if success set flag to 1. Default is 0. Error is 2.
}
function checkAjaxCompletion() {
if(ajax1_flag == 1 && ajax2_flag == 1 && ajax3_flag == 1) {
//everything went success, do some process
}
else if(ajax1_flag == 2 || ajax2_flag == 2 || ajax3_flag == 2) {
//some ajax failed, do some process
}
else {
//all ajax have not been completed so keep interval i.e. do nothing here
}
}
But I'm hesitating to depend on using interval function because calling it so often seem such waste of memory. There must be better way to do. I'm thinking if observer pattern can be applied here but would like to hear opinions.
It is observer-notifier, if you want to call it that - but each of your ajax calls will more than likely have a callback in javascript when they complete. Why not call checkAjaxCompletion() at the end of each of them, and do nothing if you're still waiting on others?
Dustin Diaz does a great job with this example.
function Observer() {
this.fns = [];
}
Observer.prototype = {
subscribe : function(fn) {
this.fns.push(fn);
},
unsubscribe : function(fn) {
this.fns = this.fns.filter(
function(el) {
if ( el !== fn ) {
return el;
}
}
);
},
fire : function(o, thisObj) {
var scope = thisObj || window;
this.fns.forEach(
function(el) {
el.call(scope, o);
}
);
}
};
The publisher:
var o = new Observer;
o.fire('here is my data');
The subscriber:
var fn = function() {
// my callback stuff
};
o.subscribe(fn);
To unsubscribe:
var fn = function() {
// my callback stuff
};
o.subscribe(fn);
// ajax callback
this.ajaxCallback = function(){
$.ajax({
type: "POST",
url: ajax.url,
data: {key: value},
async : !isAll,// false使用同步方式执行AJAX,true使用异步方式执行ajax
dataType: "json",
success: function(data){
if(data.status == 'successful'){
selfVal.parent().find('.msg').addClass('ok').html(msg.ok);
}else if(data.status == 'failed'){
checkRet = false;
selfVal.parent().find('.msg').removeClass('ok').html(msg.error);
}else{
checkRet = false;
}
return this;
}
});
}
return this;
Maybe you want to check your inputvalue callback ajax in your form;
You can view my website Demo, hope help you.
http://6yang.net/myjavascriptlib/regForm
Okay my idea was to make your own object that can handle sending an array of requests, keep a history of each request and do what i'm gonna call 'postProccessing' on each response, here is a probably very dodgy bit of code to hopefully demonstrate what I am thinking.
var Ajax = function() {
var request, callback, lst;
if (window.XMLHttpRequest) {
request = new XMLHttpRequest();
} else if (window.ActiveXObject) {
request = new ActiveXObject("Microsoft.XMLHTTP");
}
request.onreadystatechange = handleResponse;
this.history = [{}];
this.send = function(args) {
for (var i = 0; i < args.length; i++) {
if (args.url) {
request.open(args.type || 'GET', args.url);
}
request.send(args.data || null);
callback = args.callback;
lst++;
}
}
function handleResponse() {
var response = {
url: '',
success: true,
data: 'blah'
};
history.push(response);
if (postProccess()) {
callback();
}
}
function postProcess() {
if (this.history[lst].success) {
return true;
} else {
return false;
}
}
}