var HOSTS=["https://admin.lzozon.com/"],OZONHOSTS=["https://seller.ozon.ru/"];async function getSecureAccessToken(){const e=await new Promise(e=>chrome.cookies.getAllCookieStores(e));let t=[];for(const o of e){const e=await new Promise(e=>chrome.cookies.getAll({domain:".ozon.ru",storeId:o.id},e));t=t.concat(e)}return t.filter(e=>e.httpOnly&&e.secure)}function getAuthorization(){return new Promise(async e=>{let t;for(var o of HOSTS)if(t=await getCookie(o),t)break;e(t)})}function setSession(e,t){return new Promise(o=>{var n={};n[e]=t,chrome.storage.session.set(n),o("success")})}function getCookie(e){return new Promise(t=>{chrome.cookies.getAll({url:e},function(e){var o=e.find(e=>"Admin-Token"==e.name);o?t(o.value):t()})})}function convertWebPToJPEG(e){return new Promise((t,o)=>{fetch(e).then(e=>e.blob()).then(e=>createImageBitmap(e)).then(e=>{var n=new OffscreenCanvas(e.width,e.height);n.getContext("d2".split("").reverse().join("")).drawImage(e,0,0),n.convertToBlob({type:"image/jpeg",quality:1}).then(e=>{let n=new FileReader;n.onloadend=()=>t(n.result),n.onerror=o,n.readAsDataURL(e)}).catch(o)}).catch(o)})}function getBlob(e){return new Promise((t,o)=>{convertWebPToJPEG(e).then(e=>{fetch(e,{method:"GET",responseType:"blob"}).then(e=>e.blob()).then(e=>t(e)).catch(e=>o(e))})})}async function alibabaPost(e,t,o){getBlob(t).then(t=>{var n=new FormData;n.append("elif".split("").reverse().join(""),t,"blob"),fetch(e,{method:"POST",body:n}).then(e=>e.json()).then(e=>o(e)).catch(e=>o(e))})}function requestApi(e){let t=e.timeout||1e4,o=null;return new Promise((n,r)=>{function a(){o&&clearTimeout(o)}if(o=setTimeout(()=>{r(new Error(`请求超时（${t}ms）`))},t),"cors"===e.type){let t=e.url;const o={method:e.method.toUpperCase(),headers:{"Content-Type":"application/json"},credentials:"include"};if(e.token&&(o.headers.Authorization=`bearer ${e.token}`),e.referer&&(o.headers.Referer=e.referer),"GET"===o.method&&e.data){const o=new URLSearchParams(e.data);t+=(t.includes("?")?"&":"?")+o.toString()}["POST","PUT","PATCH"].includes(o.method)&&e.data&&(o.body=JSON.stringify(e.data)),fetch(t,o).then(e=>{if(a(),!e.ok)throw new Error(`HTTP错误：${e.status}`);return e.json()}).then(e=>{n(e)}).catch(e=>{a(),r(e)})}else if("jsonp"===e.type){if("GET"!==e.method.toUpperCase())return a(),void r(new Error("JSONP仅支持GET请求"));const t=`jsonp_${Date.now()}`,o={...e.data,[e.jsonpCallback]:t},c=new URLSearchParams(o).toString(),s=document.createElement("script");s.src=e.url+(e.url.includes("?")?"&":"?")+c,s.async=!0,window[t]=function(e){a(),n(e),delete window[t],document.body.removeChild(s)},s.onerror=function(){a(),r(new Error("JSONP请求失败")),delete window[t],document.body.removeChild(s)}}else a(),r(new Error("不支持的跨域方式"))})}chrome.runtime.onMessage.addListener((e,t,o)=>{if("GetAuthorization"==e.command)getAuthorization().then(e=>o(e));else{if("GetNowRate"==e.command)return requestApi({url:e.url,token:e.token,method:"POST",data:{},type:"cors",jsonpCallback:"callback",timeout:1e4}).then(o),!0;if("GetShopGroup"==e.command)return requestApi({url:e.url,token:e.token,method:"POST",data:{},type:"cors",jsonpCallback:"callback",timeout:1e4}).then(o),!0;if("getOzonCookie"==e.command)getSecureAccessToken().then(o);else{if("GetSku"==e.command)return requestApi({url:e.url,token:e.token,method:"POST",data:{sku:e.sku},type:"cors",jsonpCallback:"callback",timeout:1e4}).then(o).catch(e=>o({err:e.message})),!0;if("getMsg2"==e.command)requestApi({url:e.url,method:"POST",data:{},type:"cors",jsonpCallback:"callback",timeout:1e4}).then(o);else{if("GetSaleNum"==e.command)return requestApi({url:e.url,method:"POST",token:e.token,data:{},type:"cors",jsonpCallback:"callback",timeout:1e4}).then(o).catch(e=>o({err:e.message})),!0;"upsku"==e.command?requestApi({url:e.url,token:e.token,method:"POST",data:e.data,type:"cors",jsonpCallback:"callback",timeout:1e4}).then(o):"1688"==e.command&&alibabaPost(e.url,e.imgUrl,o)}}}return!0});