chrome.runtime.onInstalled.addListener(()=>{chrome.scripting.unregisterContentScripts({ids:["fetch-interceptor"]}).catch(()=>{}).then(()=>{chrome.scripting.registerContentScripts([{id:"fetch-interceptor",matches:["https://*.alibaba.com/*"],js:["fetchInterceptor.js"],runAt:"document_start",world:"MAIN"}])}),chrome.declarativeNetRequest.updateDynamicRules({removeRuleIds:[1001,1002],addRules:[{id:1001,priority:1,action:{type:"modifyHeaders",requestHeaders:[{header:"origin",operation:"set",value:"https://photobank.alibaba.com"},{header:"referer",operation:"set",value:"https://photobank.alibaba.com/"}]},condition:{urlFilter:"||photobank.alibaba.com",resourceTypes:["xmlhttprequest"]}},{id:1002,priority:1,action:{type:"modifyHeaders",requestHeaders:[{header:"origin",operation:"set",value:"https://photobank.alibaba.com"},{header:"referer",operation:"set",value:"https://photobank.alibaba.com/"}]},condition:{urlFilter:"||filebroker.alibaba.com",resourceTypes:["xmlhttprequest"]}}]})}),chrome.runtime.onMessage.addListener((e,t,a)=>"fetchImage"===e.action?((async()=>{try{const t=await fetch(e.url);if(!t.ok)throw new Error(`HTTP ${t.status}`);const r=await t.arrayBuffer();a({success:!0,data:Array.from(new Uint8Array(r)),type:t.headers.get("content-type")||"image/jpeg"})}catch(t){a({success:!1,error:t.message})}})(),!0):"proxyFetch"===e.action?((async()=>{try{const t=(await chrome.cookies.getAll({domain:"alibaba.com"})).map(e=>`${e.name}=${e.value}`).join("; "),r={method:e.method||"GET",headers:{Cookie:t,...e.headers}},o=e.method?.toUpperCase();if("POST"===o||"PUT"===o)if(e.fileData){const{arrayBuffer:t,name:a,type:o}=e.fileData,s=new Blob([new Uint8Array(t)],{type:o}),i=new FormData;if(i.append("file",s,a),e.params)for(const r in e.params)i.append(r,e.params[r]);r.body=i,delete r.headers["Content-Type"]}else e.isFormUrlEncoded?(r.body=new URLSearchParams(e.body).toString(),r.headers["Content-Type"]="application/x-www-form-urlencoded; charset=UTF-8"):e.body&&(r.body=JSON.stringify(e.body),r.headers["Content-Type"]||(r.headers["Content-Type"]="application/json"));const s=await fetch(e.url,r);if(!s.ok)throw new Error(`HTTP error! status: ${s.status}`);const i=await s.json();a({success:!0,data:i})}catch(t){a({success:!1,error:t.message})}})(),!0):void 0);
