1 line
1.5 KiB
JavaScript
1 line
1.5 KiB
JavaScript
(()=>{"use strict";const e="polkadot{.js}-0x6e10e163a1550e8d",n=`${e}-content`,t=`${e}-page`,o=`${e}-content`,r="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof global?global:"undefined"!=typeof self?self:"undefined"!=typeof window?window:Function("return this"),s=(a="chrome",i=r.browser,void 0===r[a]?i:r[a]);var a,i;function d(e,n,t){const o=s.runtime.connect({name:e});return o.onMessage.addListener(n),o.onDisconnect.addListener(()=>{console.log(`Disconnected from ${e}`),t()}),o}const c=async function(){return new Promise((e,n)=>{s.runtime.sendMessage({type:"wakeup"},t=>{s.runtime.lastError?n(new Error(s.runtime.lastError.message)):e(t)})})};let l;const u={onPortDisconnectHandler:function(){l=void 0},onPortMessageHandler:function(e){window.postMessage({...e,origin:o},"*")},portName:n};window.addEventListener("message",({data:e,source:n})=>{n===window&&e.origin===t&&async function(e,n){for(let t=0;t<5;t++)try{const t=await c();if("awake"===t?.status)return e||d(n.portName,n.onPortMessageHandler,n.onPortDisconnectHandler)}catch(e){console.error(`Attempt ${t+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,u).then(n=>{n.postMessage(e),l=n}).catch(e=>console.error(`Failed to send message: ${e.message}`))});const m=document.createElement("script");m.src=s.runtime.getURL("page.js"),m.onload=()=>{m.parentNode&&m.parentNode.removeChild(m)},(document.head||document.documentElement).appendChild(m)})(); |