(async()=>{try{parent.window.console.warn("Playground sandbox is executing with the same origin as its parent.","This is a security risk.","https://github.com/google/playground-elements#sandbox-security")}catch{}const{scope:e,port:t}=await new Promise((e=>{const t=a=>{1===a.data.type&&(window.removeEventListener("message",t),e(a.data))};window.addEventListener("message",t)})),a=await navigator.serviceWorker.register(new URL("playground-service-worker.js",import.meta.url).href,{scope:e}),n=()=>{var e,t;return null!==(t=null!==(e=a.installing)&&void 0!==e?e:a.waiting)&&void 0!==t?t:a.active};let r=null;const s=async(e=!1)=>{const a=n();if(!e&&a===r)return;if(r=a,null===a)return void console.error("No playground service worker found.");if(await(async e=>{if("activated"!==e.state)return new Promise((t=>{const a=new AbortController;e.addEventListener("statechange",(()=>{"activated"===e.state&&(t(),a.abort())}),{signal:a.signal})}))})(a),a!==r)return;const{port1:s,port2:o}=new MessageChannel,i={type:3,port:s};t.postMessage(i,[s]);const d={type:2,port:o};a.postMessage(d,[o])};s(),a.addEventListener("updatefound",(()=>{s()})),navigator.serviceWorker.addEventListener("message",(e=>{e.source===r&&5===e.data.type&&s(!0)})),window.addEventListener("message",(async e=>{if(6===e.data.type){const e=n();await a.update(),n()===e&&console.error("Playground service worker update failed.")}}))})();</script>
0 commit comments