Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Listen to serve response JavaScript

Background

I am making a Chrome extension for a webpage. In this webpage, I need to catch the response that the server sends when a user makes a POST request.

Currently, we are using the Observer pattern to check changes on the HTML page, but this is clumsy and it fires several times.

Objective

I need to catch that response, parse its information accordingly, and then base on it do some actions on the page's HTML by changing (adding some colors to some tables, add additional info, etc).

Problem

My issue here, is that I don't know of any JavaScript library or pure method that allows me to listen to server responses, so I can parse them.

What I tried

I tried JavaScript's EventSource, however for it to work I need to have specific code in the server and that is not an option.

Another research venue was making the request myself using an XHMLRequest, but then I would have to have a listener on every button on the page so I could stop its default action and perform it via my code, which doesn't sound that good either.

Questions

  1. Are there any JavaScript tools that allow me to check for server responses and treat them?
like image 992
Flame_Phoenix Avatar asked Jan 20 '17 10:01

Flame_Phoenix


People also ask

What is the use of HTTP Server listen in Java?

The http.server.listen () is an inbuilt application programming interface of class Server within the http module which is used to start the server from accepting new connections. Parameters: This method accepts the following two parameters:

How do I remove an event listener in JavaScript?

You can easily remove an event listener by using the removeEventListener () method. The first parameter is the type of the event (like " click " or " mousedown " or any other HTML DOM Event .) The second parameter is the function we want to call when the event occurs.

How do I listen to a local server?

You can call like: server.listen (80) server.listen (80, '127.0.0.1') server.listen ('/tmp/server.sock') Gets the server up and listening.

How do I listen to a node server?

You can call like: server.listen (80) server.listen (80, '127.0.0.1') server.listen ('/tmp/server.sock') Gets the server up and listening. Wraps node's listen (). Once listen () is called, this will be filled in with where the server is running. Name of the server. fs-extra contains methods that aren't included in the vanilla Node.js fs package.


1 Answers

How about something like this?

(function(open) {
    XMLHttpRequest.prototype.open = function(m, u, a, us, p) {
        this.addEventListener('readystatechange', function() {
            console.log(this.response);
        }, false);

        open.call(this, m, u, a, us, p);
    };
})(XMLHttpRequest.prototype.open)

You can override open function and then do with response whatever you want.

like image 51
Maciej Kasprzak Avatar answered Sep 30 '22 12:09

Maciej Kasprzak