-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathajaxreplay.min.js
1 lines (1 loc) · 1.47 KB
/
ajaxreplay.min.js
1
var ajaxReplay={noCache:false,refresh:true,originalXHR:window.XMLHttpRequest};window.XMLHttpRequest=function(){};XMLHttpRequest.prototype.open=function(method,url,async,user,pass){this.method=method;this.url=url;this.async=async;this.user=user;this.pass=pass;this.headers=[];this.cacheid="ajaxreplay"+this.method+this.url};XMLHttpRequest.prototype.setRequestHeader=function(name,value){this.headers.push({name:name,value:value})};XMLHttpRequest.prototype.send=function(params){if(params){this.cacheid+=params}var onReadyCalled=false;if(!ajaxReplay.noCache&&localStorage[this.cacheid]){this.readyState=4;this.status=200;this.responseText=localStorage[this.cacheid];if(this.onreadystatechange){this.onreadystatechange.call(this);onReadyCalled=true}}if(!localStorage[this.cacheid]||ajaxReplay.refresh){var me=this;var xhr=new ajaxReplay.originalXHR;xhr.open(this.method,this.url,this.async,this.user,this.pass);if(this.headers.length>0){for(var i=0,len=this.headers.length;i<len;i++){xhr.setRequestHeader(this.headers[i].name,this.headers[i].value)}}xhr.onreadystatechange=function(){me.readyState=xhr.readyState;if(xhr.readyState===4){me.status=xhr.status;me.responseText=xhr.responseText}if(me.onreadystatechange&&!onReadyCalled){me.onreadystatechange()}if(!ajaxReplay.noCache&&me.readyState===4&&me.status===200){localStorage[me.cacheid]=me.responseText}};xhr.send(params)}};ajaxReplay.clearCache=function(){for(var key in localStorage){if(key.indexOf("ajaxreplay")!==-1){localStorage.removeItem(key)}}};