phizclip-uniapp-demo/finclip-uniapp-test/nativeplugins/MopSdk/ios/FinApplet.framework/FinApplet.bundle/FinClipAJAXHook.min.js

2 lines
17 KiB
JavaScript

!function(e){"function"==typeof define&&define.amd?define(e):e()}(function(){"use strict";var t,r,s={searchParams:"URLSearchParams"in self,iterable:"Symbol"in self&&"iterator"in Symbol,blob:"FileReader"in self&&"Blob"in self&&function(){try{return new Blob,!0}catch(e){return!1}}(),formData:"FormData"in self,arrayBuffer:"ArrayBuffer"in self};function o(e){if("string"!=typeof e&&(e=String(e)),/[^a-z0-9\-#$%&'*+.^_`|~!]/i.test(e)||""===e)throw new TypeError("Invalid character in header field name");return e.toLowerCase()}function n(e){return e="string"!=typeof e?String(e):e}function e(t){var e={next:function(){var e=t.shift();return{done:void 0===e,value:e}}};return s.iterable&&(e[Symbol.iterator]=function(){return e}),e}function l(t){this.map={},t instanceof l?t.forEach(function(e,t){this.append(t,e)},this):Array.isArray(t)?t.forEach(function(e){this.append(e[0],e[1])},this):t&&Object.getOwnPropertyNames(t).forEach(function(e){this.append(e,t[e])},this)}function a(e){if(e.bodyUsed)return Promise.reject(new TypeError("Already read"));e.bodyUsed=!0}function i(r){return new Promise(function(e,t){r.onload=function(){e(r.result)},r.onerror=function(){t(r.error)}})}function u(e){var t=new FileReader,r=i(t);return t.readAsArrayBuffer(e),r}function c(e){if(e.slice)return e.slice(0);var t=new Uint8Array(e.byteLength);return t.set(new Uint8Array(e)),t.buffer}function d(){return this.bodyUsed=!1,this._initBody=function(e){var t;this.bodyUsed=this.bodyUsed,(this._bodyInit=e)?"string"==typeof e?this._bodyText=e:s.blob&&Blob.prototype.isPrototypeOf(e)?this._bodyBlob=e:s.formData&&FormData.prototype.isPrototypeOf(e)?this._bodyFormData=e:s.searchParams&&URLSearchParams.prototype.isPrototypeOf(e)?this._bodyText=e.toString():s.arrayBuffer&&s.blob&&((t=e)&&DataView.prototype.isPrototypeOf(t))?(this._bodyArrayBuffer=c(e.buffer),this._bodyInit=new Blob([this._bodyArrayBuffer])):s.arrayBuffer&&(ArrayBuffer.prototype.isPrototypeOf(e)||r(e))?this._bodyArrayBuffer=c(e):this._bodyText=e=Object.prototype.toString.call(e):this._bodyText="",this.headers.get("content-type")||("string"==typeof e?this.headers.set("content-type","text/plain;charset=UTF-8"):this._bodyBlob&&this._bodyBlob.type?this.headers.set("content-type",this._bodyBlob.type):s.searchParams&&URLSearchParams.prototype.isPrototypeOf(e)&&this.headers.set("content-type","application/x-www-form-urlencoded;charset=UTF-8"))},s.blob&&(this.blob=function(){var e=a(this);if(e)return e;if(this._bodyBlob)return Promise.resolve(this._bodyBlob);if(this._bodyArrayBuffer)return Promise.resolve(new Blob([this._bodyArrayBuffer]));if(this._bodyFormData)throw new Error("could not read FormData body as blob");return Promise.resolve(new Blob([this._bodyText]))},this.arrayBuffer=function(){return this._bodyArrayBuffer?a(this)||Promise.resolve(this._bodyArrayBuffer):this.blob().then(u)}),this.text=function(){var e,t,r=a(this);if(r)return r;if(this._bodyBlob)return e=this._bodyBlob,t=new FileReader,r=i(t),t.readAsText(e),r;if(this._bodyArrayBuffer)return Promise.resolve(function(e){for(var t=new Uint8Array(e),r=new Array(t.length),o=0;o<t.length;o++)r[o]=String.fromCharCode(t[o]);return r.join("")}(this._bodyArrayBuffer));if(this._bodyFormData)throw new Error("could not read FormData body as text");return Promise.resolve(this._bodyText)},s.formData&&(this.formData=function(){return this.text().then(p)}),this.json=function(){return this.text().then(JSON.parse)},this}s.arrayBuffer&&(t=["[object Int8Array]","[object Uint8Array]","[object Uint8ClampedArray]","[object Int16Array]","[object Uint16Array]","[object Int32Array]","[object Uint32Array]","[object Float32Array]","[object Float64Array]"],r=ArrayBuffer.isView||function(e){return e&&-1<t.indexOf(Object.prototype.toString.call(e))}),l.prototype.append=function(e,t){e=o(e),t=n(t);var r=this.map[e];this.map[e]=r?r+", "+t:t},l.prototype.delete=function(e){delete this.map[o(e)]},l.prototype.get=function(e){return e=o(e),this.has(e)?this.map[e]:null},l.prototype.has=function(e){return this.map.hasOwnProperty(o(e))},l.prototype.set=function(e,t){this.map[o(e)]=n(t)},l.prototype.forEach=function(e,t){for(var r in this.map)this.map.hasOwnProperty(r)&&e.call(t,this.map[r],r,this)},l.prototype.keys=function(){var r=[];return this.forEach(function(e,t){r.push(t)}),e(r)},l.prototype.values=function(){var t=[];return this.forEach(function(e){t.push(e)}),e(t)},l.prototype.entries=function(){var r=[];return this.forEach(function(e,t){r.push([t,e])}),e(r)},s.iterable&&(l.prototype[Symbol.iterator]=l.prototype.entries);var f=["DELETE","GET","HEAD","OPTIONS","POST","PUT"];function h(e,t){var r,o=(t=t||{}).body;if(e instanceof h){if(e.bodyUsed)throw new TypeError("Already read");this.url=e.url,this.credentials=e.credentials,t.headers||(this.headers=new l(e.headers)),this.method=e.method,this.mode=e.mode,this.signal=e.signal,o||null==e._bodyInit||(o=e._bodyInit,e.bodyUsed=!0)}else this.url=String(e);if(this.credentials=t.credentials||this.credentials||"same-origin",!t.headers&&this.headers||(this.headers=new l(t.headers)),this.method=(r=t.method||this.method||"GET",e=r.toUpperCase(),-1<f.indexOf(e)?e:r),this.mode=t.mode||this.mode||null,this.signal=t.signal||this.signal,this.referrer=null,("GET"===this.method||"HEAD"===this.method)&&o)throw new TypeError("Body not allowed for GET or HEAD requests");this._initBody(o)}function p(e){var r=new FormData;return e.trim().split("&").forEach(function(e){var t;e&&(e=(t=e.split("=")).shift().replace(/\+/g," "),t=t.join("=").replace(/\+/g," "),r.append(decodeURIComponent(e),decodeURIComponent(t)))}),r}function y(e,t){t=t||{},this.type="default",this.status=void 0===t.status?200:t.status,this.ok=200<=this.status&&this.status<300,this.statusText="statusText"in t?t.statusText:"",this.headers=new l(t.headers),this.url=t.url||"",this._initBody(e)}h.prototype.clone=function(){return new h(this,{body:this._bodyInit})},d.call(h.prototype),d.call(y.prototype),y.prototype.clone=function(){return new y(this._bodyInit,{status:this.status,statusText:this.statusText,headers:new l(this.headers),url:this.url})},y.error=function(){var e=new y(null,{status:0,statusText:""});return e.type="error",e};var b=[301,302,303,307,308];y.redirect=function(e,t){if(-1===b.indexOf(t))throw new RangeError("Invalid status code");return new y(null,{status:t,headers:{location:e}})};var m=self.DOMException;try{new m}catch(e){(m=function(e,t){this.message=e,this.name=t;e=Error(e);this.stack=e.stack}).prototype=Object.create(Error.prototype),m.prototype.constructor=m}function v(o,i){return new Promise(function(n,e){var t=new h(o,i);if(t.signal&&t.signal.aborted)return e(new m("Aborted","AbortError"));var a=new XMLHttpRequest;function r(){a.abort()}a.onload=function(){var e,r,t={status:a.status,statusText:a.statusText,headers:(e=a.getAllResponseHeaders()||"",r=new l,e.replace(/\r?\n[\t ]+/g," ").split(/\r?\n/).forEach(function(e){var t=e.split(":"),e=t.shift().trim();e&&(t=t.join(":").trim(),r.append(e,t))}),r)};t.url="responseURL"in a?a.responseURL:t.headers.get("X-Request-URL");var o="response"in a?a.response:a.responseText;setTimeout(function(){n(new y(o,t))},0)},a.onerror=function(){setTimeout(function(){e(new TypeError("Network request failed"))},0)},a.ontimeout=function(){setTimeout(function(){e(new TypeError("Network request failed"))},0)},a.onabort=function(){setTimeout(function(){e(new m("Aborted","AbortError"))},0)},a.open(t.method,function(t){try{return""===t&&self.location.href?self.location.href:t}catch(e){return t}}(t.url),!0),"include"===t.credentials?a.withCredentials=!0:"omit"===t.credentials&&(a.withCredentials=!1),"responseType"in a&&(s.blob?a.responseType="blob":s.arrayBuffer&&-1!==t.headers.get("Content-Type").indexOf("application/octet-stream")&&(a.responseType="arraybuffer")),t.headers.forEach(function(e,t){a.setRequestHeader(t,e)}),t.signal&&(t.signal.addEventListener("abort",r),a.onreadystatechange=function(){4===a.readyState&&t.signal.removeEventListener("abort",r)}),a.send(void 0===t._bodyInit?null:t._bodyInit)})}v.polyfill=!0;var g=self.Headers,w=self.Request,B=self.Response,F=self.fetch;function A(e){F&&(e?(self.fetch=v,self.Headers=l,self.Request=h,self.Response=y):(self.fetch=F,self.Headers=g,self.Request=w,self.Response=B))}self.fetch||(self.fetch=v,self.Headers=l,self.Request=h,self.Response=y);var T=(H.setupHook=function(){var e=window.FormData.prototype.append;window.FormData.prototype.entries||(window.FormData.prototype.append=function(){return this._entries||(this._entries=[]),this._entries.push(arguments),e.apply(this,arguments)})},H.traversalEntries=function(e,t){var r,o,n,a,i;if(e._entries)for(var s=0;s<e._entries.length;s++){var l=(h=e._entries[s])[0],u=h[1],c=2<h.length?h[2]:null;t&&t(l,u,c)}else try{for(var d=(n=e.entries(),a="function"==typeof Symbol&&n[Symbol.iterator],i=0,a?a.call(n):{next:function(){return{value:(n=n&&i>=n.length?void 0:n)&&n[i++],done:!n}}}),f=d.next();!f.done;f=d.next()){var h,l=(h=f.value)[0],u=h[1];t&&t(l,u,null)}}catch(e){r={error:e}}finally{try{f&&!f.done&&(o=d.return)&&o.call(d)}finally{if(r)throw r.error}}},H);function H(){}var C=(q.convertArrayBufferToBase64=function(e){for(var t=new Uint8Array(e),r="",o=t.byteLength,n=0;n<o;n++)r+=String.fromCharCode(t[n]);return window.btoa(r)},q.convertFormDataToJson=function(e,a){var o=[];T.traversalEntries(e,function(e,t,r){o.push(q.convertSingleFormDataRecordToArray(e,t,r))}),Promise.all(o).then(function(e){for(var t={},r=[],o=0;o<e.length;o++){var n=e[o];1<n.length&&"string"!=typeof n[1]&&r.push(n[0])}t.fileKeys=r,t.formData=e,a(t)}).catch(function(e){console.log(e)})},q.convertSingleFormDataRecordToArray=function(n,a,i){return new Promise(function(t,r){var e,o=[];o.push(n),a instanceof File||a instanceof Blob?((e=new FileReader).readAsDataURL(a),e.onload=function(e){e=e.target.result,e={name:i||(a instanceof File?a.name:""),lastModified:a instanceof File?a.lastModified:0,size:a.size,type:a.type,data:e};return o.push(e),t(o),null},e.onerror=function(e){return r(Error("formdata 表单读取文件数据失败")),null}):(o.push(a),t(o))})},q.convertFileToJson=function(o){return new Promise(function(t,r){var e=new FileReader;e.readAsDataURL(o),e.onload=function(e){e=e.target.result,e={name:o instanceof File?o.name:"",lastModified:o instanceof File?o.lastModified:0,size:o.size,type:o.type,data:e};return t(e),null},e.onerror=function(e){return r(Error("formdata 表单读取文件数据失败")),null}})},q);function q(){}var R=(k.dispatchMessage=function(e){var t=document.querySelectorAll("iframe");if(t)for(var r=t.length,o=0;o<r;o++)t[o].contentWindow.postMessage(e,"*")},k.addMessageListener=function(){window.addEventListener("message",function(e){var e=e.data;"string"!=typeof e||-1!=(e=e).indexOf("messageType")&&window.FinClipHookBridge._handleMessageFromNative(e)})},k.addAjaxMessageListener=function(){window.addEventListener("message",function(e){var e=e.data;"string"!=typeof e||-1!=(e=e).indexOf("ajaxType")&&window._FinClipHookBridgeXHR.setProperties(e)})},k.setupHook=function(){document.addEventListener("DOMContentLoaded",function(){var e=document.querySelectorAll("iframe");if(e)for(var t=e.length,r=0;r<t;r++){var o=e[r];o.getAttribute("sandbox")&&-1==o.getAttribute("sandbox").indexOf("allow-scripts")&&o.setAttribute("sandbox",o.getAttribute("sandbox")+" allow-scripts")}});var o=document.createElement;document.createElement=function(e){var t=o.call(document,e);if("iframe"===e.toLowerCase())try{var r=Object.getOwnPropertyDescriptor(window.HTMLIFrameElement,"sandbox")||Object.getOwnPropertyDescriptor(HTMLIFrameElement.prototype,"sandbox");r&&r.configurable&&Object.defineProperty(t,"sandbox",{configurable:!0,enumerable:!0,get:function(){return r.get.call(t)},set:function(e){-1==e.indexOf("allow-scripts")&&(e+=" allow-scripts"),r.set.call(t,e)}})}catch(e){console.log("this browser does not support reconfigure iframe sandbox property",e)}return t}},k);function k(){}var E=(_.prototype.callNative=function(e,t,r,o){r={module:e||"default",method:t,data:r,callbackId:null};o&&(t="cb_"+r.module+"_"+t+"_"+this.uniqueId+++"_"+(new Date).getTime(),this.callbackCache[t]=o,r.callbackId=t),window.webkit.messageHandlers.FinClipHookBridgeMessage.postMessage(r)},_.prototype._getMessages=function(){var e=JSON.stringify(this.queues);return this.queues=[],e},_.prototype._handleMessageFromNative=function(e){var t=JSON.parse(e);if("callback"===t.messageType){var r=this.callbackCache[t.callbackId];r&&(r(t.data),this.callbackCache[t.callbackId]=null,delete this.callbackCache[t.callbackId])}else if("event"===t.messageType){var o=this.eventCallbackCache[t.eventName];if(o)for(var n=0;n<o.length;n++){var a=o[n];a&&a(t.data)}}R.dispatchMessage(e)},_.prototype.call=function(e,t,r,o){this.callNative(e,t,r,o)},_.prototype.syncCall=function(e,t,r){try{return o=JSON.stringify({module:e||"default",method:t,data:r}),(o=window.prompt("FinClipHookBridge",o))?JSON.parse(o):null}catch(e){return console.log("window.prompt will happen error when beforeunload event triggered",e),null}var o},_.prototype.on=function(e,t){var r=this.eventCallbackCache[e];r?r.push(t):this.eventCallbackCache[e]=r=[t]},_.prototype.off=function(e){e=this.eventCallbackCache[e];e&&0<e.length&&e.splice(0,e.length)},_);function _(){this.uniqueId=1,this.callbackCache={},this.eventCallbackCache={},this.queues=[]}var x=(I.moduleName="ajax",I.globalId=Math.floor(1e5*Math.random()),I.callbackCache=[],I.generateXHRRequestId=function(){return(new Date).getTime()+""+I.globalId++},I.generateNewActionForForm=function(e,t){var r=e.action;e.action=I.generateNewUrlWithRequestId(r,t)},I.generateNewUrlWithRequestId=function(e,t){var r=e,o=document.createElement("a");o.href=r;var n,a=o.search||"",i=o.hash||"";return/FinClipHookBridge-RequestId/.test(r)?o.search=o.search.replace(/FinClipHookBridge-RequestId=(\d+)/,"FinClipHookBridge-RequestId="+t):o.search&&0<o.search.length?(n=o.search,/FinClipHookBridge-RequestId/.test(n)?o.search=n.replace(/FinClipHookBridge-RequestId=(\d+)/,"FinClipHookBridge-RequestId="+t):o.search=n+"&FinClipHookBridge-RequestId="+t):o.search="?FinClipHookBridge-RequestId="+t,(e="#"===(e=r.replace(a,"").replace(i,"")).trim()?"":e)+o.search+o.hash},I.generateNewOpenUrlWithRequestId=function(e,t){function r(e){return"^^^^"+e+"^^^^"}var o=/\^\^\^\^(\d+)\^\^\^\^/,n=document.createElement("a");n.href=e;var a=n.hash||"";return o.test(n.hash)?n.hash=n.hash.replace(o,r(t)):n.hash&&0<n.hash.length?n.hash=n.hash+r(t):n.hash=r(t),(e="#"===(e=e.replace(a,"")).trim()?"":e)+n.hash},I.isNonNormalHttpRequest=function(e,t){return!/^((http|https):\/\/)/.test(e)&&"GET"===t},I.sendBodyToNativeForCache=function(e,t,r,o,n,a){var i=t.requestId,s=function(){return"FORM"===e&&I.generateNewActionForForm(t,i),r.apply(t,o)};if(a=void 0===a?!0:a)return window.FinClipHookBridge.call(I.moduleName,"cacheAJAXBody",n,function(e){}),void s();window.FinClipHookBridge.syncCall(I.moduleName,"cacheAJAXBody",n),s()},I.setupHook=function(){var s=XMLHttpRequest.prototype.open;XMLHttpRequest.prototype.open=function(e,t,r,o,n){var a=[].slice.call(arguments),i=this;return i.requestId=I.generateXHRRequestId(),i.requestUrl=t,i.requestHref=document.location.href,i.requestMethod=e,i.requestAsync=r,!I.isNonNormalHttpRequest(t,e)&&window.FinClipHookBridgeConfig.ajaxHook?(a[1]=I.generateNewUrlWithRequestId(t,i.requestId),void s.apply(i,a)):s.apply(i,a)};var a=XMLHttpRequest.prototype.send;XMLHttpRequest.prototype.send=function(e){var t=[].slice.call(arguments),r=this,o={requestId:r.requestId,requestHref:r.requestHref,requestUrl:r.requestUrl,bodyType:"String",value:null};if(I.isNonNormalHttpRequest(r.requestUrl,r.requestMethod))return a.apply(r,t);if(!window.FinClipHookBridgeConfig.ajaxHook)return a.apply(r,t);if(!e)return a.apply(r,t);if(e instanceof ArrayBuffer)o.bodyType="ArrayBuffer",o.value=C.convertArrayBufferToBase64(e);else{if(e instanceof Blob){o.bodyType="Blob";var n=new FileReader;return n.onload=function(e){e=e.target.result;o.value=e,I.sendBodyToNativeForCache("AJAX",r,a,t,o)},void n.readAsDataURL(e)}if(e instanceof FormData)return o.bodyType="FormData",o.formEnctype="multipart/form-data",void C.convertFormDataToJson(e,function(e){o.value=e,I.sendBodyToNativeForCache("AJAX",r,a,t,o)});o.bodyType="String",o.value=e}I.sendBodyToNativeForCache("AJAX",r,a,t,o,r.requestAsync)};var n=HTMLFormElement.prototype.submit;HTMLFormElement.prototype.submit=function(){var t=[].slice.call(arguments),r=this;r.requestId=I.generateXHRRequestId(),r.requestUrl=r.action,r.requestHref=document.location.href;var o={requestId:r.requestId,requestHref:r.requestHref,requestUrl:r.requestUrl,bodyType:"FormData",formEnctype:r.enctype,value:null};if(!window.FinClipHookBridgeConfig.ajaxHook)return n.apply(r,t);if(!r.action)return n.apply(r,t);var e=new FormData(r);C.convertFormDataToJson(e,function(e){o.value=e,I.sendBodyToNativeForCache("FORM",r,n,t,o)})}},I);function I(){}var U;function D(){}window.FinClipHookBridge||(D.ajaxHook=!1,D.enableAjaxHook=function(e){A(D.ajaxHook=!!e)},D.bridgeReady=function(){var e=document.createEvent("Events");e.initEvent("FinClipHookBridgeReady"),document.dispatchEvent(e)},U=D,window.FinClipHookBridge=new E,window.FinClipHookBridgeConfig=U,window._FinClipHookBridgeXHR=x,R.addMessageListener(),R.setupHook(),T.setupHook(),x.setupHook(),window.FinClipHookBridgeConfig.bridgeReady())});