UNPKG

fet-block

Version:

fetBlock is a web h5 request hook library

7 lines (6 loc) 1.76 kB
/*! * fet-block v1.0.0 * (c) sheldon cui * Released under the MIT License. */ !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).fetBlock={})}(this,(function(t){"use strict";let e=function(t){return e=Object.setPrototypeOf?Object.getPrototypeOf:function(t){return t.__proto__},e(t)};const o=function(){let t=window.XMLHttpRequest.prototype;for(;t&&e(t)!==XMLHttpRequestEventTarget.prototype;)t=e(t);return t.constructor}();function n(t){const e=document.createElement("script");e.innerHTML=t,document.body.appendChild(e),document.body.removeChild(e)}const r="https://www.unpkg.com/fet@1.0.0/umd/fetBlockLogic.min.js",s="fet_block_logic_js_content";t.init=async function(t={}){!function(...t){var e,o;e=window.location.href,o="fetblock_debug",function(t){const e=t.split("#")[0].split("?")[1]||"",o={};return e.split("&").forEach((t=>{const[e,n]=t.split("=");o[e]=n})),o}(e)[o]&&console.warn(...t)}("stub exec",r),t.configUrl&&(window.fetBlockConfigUrl=configUrl);const e=localStorage.getItem(s);e&&e.includes("fetBlock")&&n(e);const c=await async function(t){return new Promise((e=>{const n=new o;n.openUrl=t.url,n.open(t.method||"get",t.url),n.timeout=t.timeout||5e3,n.responseType=t.responseType||"text",n.onload=function(){"text"===n.responseType?e({status:n.status,statusText:n.statusText,body:n.responseText,headers:n.getAllResponseHeaders()}):e(n.response)},n.onerror=function(t){reject(new Error("network error"))},n.ontimeout=function(t){reject(new Error("network timeout"))},n.send()}))}({url:r,method:"get"});localStorage.setItem(s,c?.body),!e&&c?.body&&c?.body.includes("fetBlock")&&n(c?.body)}}));