(function(){"use strict";console.log("--------  contentScript  ------------"),(async()=>chrome.runtime.onMessage.addListener((o,i,t)=>{if(o.do==="contentExecuteTask")return console.log("通知content执行任务 ==> ",o.requestParam),g(o.url,o.task,o.requestParam).then(c=>{console.log("content执行任务执行结果 ==> ",c),t(c)}),!0}))();const g=async(o,i,t)=>{try{const c=t.method.toUpperCase(),a=t.cache,f={...t.headers,"Content-Type":(t==null?void 0:t.contentType)||"application/json"},n=t!=null&&t.data?JSON.parse(t==null?void 0:t.data):{},l=(t==null?void 0:t.timeout)||6e5;return await h(o,c,a,f,n,l)}catch(c){console.log(c);const a="执行任务"+i.documentFile+"，URL："+o+"；异常提示："+c;await chrome.runtime.sendMessage({fn:"Logs.addLog",content:a}),await chrome.runtime.sendMessage({fn:"Logs.addToolLog",msgType:4,msg:a}),await chrome.runtime.sendMessage({fn:"Tasks.setExecuteErrorInfo",task:i,errorInfo:"执行任务时异常"})}},h=async(o="",i,t,c={},a={},f=6e5)=>{let n=null;try{const l=new AbortController;n=setTimeout(()=>{l.abort(),n&&clearTimeout(n)},f);const d={method:i,mode:"cors",cache:t?"default":"no-cache",credentials:"same-origin",headers:{"Content-Type":"application/json","request-from":"plugin",signal:l.signal,...c},redirect:"follow",body:JSON.stringify(a)};i==="GET"&&delete d.body,console.log("content ---> fetchConfig ======> ",d);const e=await fetch(o,d),r=e.headers.get("content-type");console.log("content ---> fetchHtmlData =========>>>>",e),console.log("content ---> fetchHtmlData-contentType =========>>>> contentType",r),console.log("content ---> fetchHtmlData-headers =========>>>> headers",Object.fromEntries(e.headers));let s={};return e.status===200?(r.indexOf("text/html")>-1?s={type:r,data:await e.text()}:r.indexOf("application/octet-stream")>-1?s=await e.blob()||{}:(s=await e.json()||{},s={data:JSON.stringify(s),status:e.status,statusText:e.statusText,headers:JSON.stringify(Object.fromEntries(e.headers))}),console.log("content ---> fetchHtmlData =========>>>> res",s),n&&clearTimeout(n),s):(n&&clearTimeout(n),s=await e.text()||{},{data:JSON.stringify(s),status:e.status,statusText:e.statusText,headers:JSON.stringify(Object.fromEntries(e.headers))})}catch(l){clearTimeout(n),console.log(l)}}})();
