Last active
December 5, 2021 15:57
-
-
Save aachyee/3bb1addd0dde76e5701c3dae24794cc7 to your computer and use it in GitHub Desktop.
XMLHttpRequest API hook Sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // XMLHttpRequest API hook Sample | |
| function headPadding(s,l,p=' '){ | |
| let t=Array(l+1).join(p)+s.toString(); | |
| return l>=1?t.slice(-l):''; | |
| } | |
| function tailPadding(s,l,p=' '){ | |
| let t=s.toString()+Array(l+1).join(p); | |
| return l>=1?t.slice(0,l):''; | |
| } | |
| function createUniqueId(){ | |
| let t=headPadding(new Date().getTime().toString(16),16,'0'); | |
| let r=headPadding(Math.floor(65536*Math.random()).toString(16),4,'0'); | |
| return t+r; | |
| } | |
| var orig_XHR_open=XMLHttpRequest.prototype.open; | |
| XMLHttpRequest.prototype.open=function(method, url, async, user, password){ | |
| if(!this.id) this.id=createUniqueId(); | |
| this.prevReadyState=-1; | |
| this.url=url; | |
| console.log('XMLHttpRequest['+this.id+'].open('+[].slice.call(arguments).join(',')+')'); | |
| return orig_XHR_open.apply(this, arguments); | |
| }; | |
| var XHR_readyState_Name=['UNSENT','OPENED','HEADERS_RECEIVED','LOADING','DONE']; | |
| XMLHttpRequest.prototype.onreadystatechangeCallback=function(){ | |
| if(this.readyState>=2 && this.prevReadyState!=this.readyState){ | |
| console.log('XMLHttpRequest['+this.id+'].readystate: '+XHR_readyState_Name[this.readyState]); | |
| } | |
| if(this.readyState==2){ | |
| if(this.url!=this.responseURL)console.log('responseURL: '+this.responseURL); | |
| console.log('XMLHttpRequest['+this.id+'].status: '+this.status); | |
| console.log('XMLHttpRequest['+this.id+'].getAllResponseHeaders():'); | |
| console.log(this.getAllResponseHeaders()); | |
| } | |
| if(this.readyState==4 && this.status>=100 && this.status<600){ | |
| // console.log('XMLHttpRequest['+this.id+'].responseType: '+this.responseType); | |
| if (this.responseType == 'text' || this.responseType == ''){ | |
| console.log('XMLHttpRequest['+this.id+'].responseText: '+this.responseText); | |
| } | |
| } | |
| this.prevReadyState=this.readyState; | |
| (this.orig_onreadystatechange)(); | |
| }; | |
| var orig_XHR_send=XMLHttpRequest.prototype.send; | |
| XMLHttpRequest.prototype.send=function(body){ | |
| // this.setRequestHeader('Connection','close'); // THis may be refused. | |
| // this.overrideMimeType('image/jpeg'); | |
| console.log('XMLHttpRequest['+this.id+'].send('+[].slice.call(arguments).join(',')+')'); | |
| this.orig_onreadystatechange = this.onreadystatechange; | |
| this.onreadystatechange = this.onreadystatechangeCallback; | |
| return orig_XHR_send.apply(this, arguments); | |
| }; | |
| var orig_XHR_abort=XMLHttpRequest.prototype.abort; | |
| XMLHttpRequest.prototype.abort=function(){ | |
| console.log('XMLHttpRequest['+this.id+'].abort()'); | |
| return orig_XHR_abort.apply(this); | |
| }; | |
| var orig_XHR_setRequestHeader=XMLHttpRequest.prototype.setRequestHeader; | |
| XMLHttpRequest.prototype.setRequestHeader=function(header,value){ | |
| console.log('XMLHttpRequest['+this.id+'].setRequestHeader('+[].slice.call(arguments).join(',')+')'); | |
| return orig_XHR_setRequestHeader.apply(this,arguments); | |
| }; | |
| var orig_XHR_overrideMimeType=XMLHttpRequest.prototype.overrideMimeType; | |
| XMLHttpRequest.prototype.overrideMimeType=function(mimeType){ | |
| console.log('XMLHttpRequest['+this.id+'].overrideMimeType('+[].slice.call(arguments).join(',')+')'); | |
| return orig_XHR_overrideMimeType.apply(this,arguments); | |
| }; | |
| // Test code | |
| xhr=new XMLHttpRequest(); | |
| xhr.open('POST',location.href); | |
| xhr.onreadystatechange=function(){console.log('readystatechanged')}; | |
| xhr.send('abc=123&xyz=999'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment