最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - Listen to serve response JavaScript - Stack Overflow

programmeradmin1浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论