admin管理员组

文章数量:1325236

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?

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?
Share Improve this question asked Jan 20, 2017 at 10:30 Flame_PhoenixFlame_Phoenix 17.6k40 gold badges144 silver badges284 bronze badges 2
  • How are you making the POST request from the user? – Pineda Commented Jan 20, 2017 at 10:42
  • The user is clicking the button. That is how he is making the post request. – Flame_Phoenix Commented Jan 20, 2017 at 13:39
Add a ment  | 

1 Answer 1

Reset to default 8

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.

本文标签: htmlListen to serve response JavaScriptStack Overflow