mirror of
https://github.com/pezkuwichain/pezkuwi-extension.git
synced 2026-04-22 02:08:03 +00:00
1 line
1.7 KiB
JavaScript
1 line
1.7 KiB
JavaScript
(()=>{"use strict";var e={};e.g=function(){if("object"==typeof globalThis)return globalThis;try{return this||new Function("return this")()}catch(e){if("object"==typeof window)return window}}();const t="Pezkuwi-0x343cabeadae178ba",n=`${t}-content`,o=`${t}-page`,r=`${t}-content`,s="undefined"!=typeof globalThis?globalThis:void 0!==e.g?e.g:"undefined"!=typeof self?self:"undefined"!=typeof window?window:Function("return this"),a=(i="chrome",c=s.browser,void 0===s[i]?c:s[i]);var i,c;function d(e,t,n){const o=a.runtime.connect({name:e});return o.onMessage.addListener(t),o.onDisconnect.addListener(()=>{console.log(`Disconnected from ${e}`),n()}),o}const u=async function(){return new Promise((e,t)=>{a.runtime.sendMessage({type:"wakeup"},n=>{a.runtime.lastError?t(new Error(a.runtime.lastError.message)):e(n)})})};let l;const m={onPortDisconnectHandler:function(){l=void 0},onPortMessageHandler:function(e){window.postMessage({...e,origin:r},"*")},portName:n};window.addEventListener("message",({data:e,source:t})=>{t===window&&e.origin===o&&async function(e,t){for(let n=0;n<5;n++)try{const n=await u();if("awake"===n?.status)return e||d(t.portName,t.onPortMessageHandler,t.onPortDisconnectHandler)}catch(e){console.error(`Attempt ${n+1} failed: ${e.message}`),await new Promise(e=>setTimeout(e,1e3))}throw new Error("Failed to wake up the service worker and setup the port after multiple attempts")}(l,m).then(t=>{t.postMessage(e),l=t}).catch(e=>console.error(`Failed to send message: ${e.message}`))});const w=document.createElement("script");w.src=a.runtime.getURL("page.js"),w.onload=()=>{w.parentNode&&w.parentNode.removeChild(w)},(document.head||document.documentElement).appendChild(w)})(); |