var ports = []; function getItem(data) { const {k} = data; if (!!this){ try{ const v = window.sessionStorage.getItem(k); this.postMessage({...data, v : v}); } catch(e){ this.postMessage({...data, v : null, error : e}); } } } function setItem(data) { const {k, v} = data; if (!!this){ try{ window.sessionStorage.setItem(k, v); this.postMessage(data); } catch(e){ this.postMessage({...data, v : null, error : e}); } } } function removeItem(data) { const {k} = data; if (!!this){ try{ window.sessionStorage.removeItem(k); this.postMessage(data); } catch(e){ this.postMessage({...data, v : null, error : e}); } } } function handleStorage(m) { const {action, ...rest} = m.data; this.getItem = getItem; this.setItem = setItem; this.removeItem = removeItem; if (action === 'get'){ this.getItem(rest); } else if (action === 'set'){ this.setItem(rest); } else if (action === 'remove'){ this.removeItem(rest); } } window.addEventListener('message', e => { if (e?.ports?.length > 0) { const port = e.ports[0]; port.onmessage = handleStorage; ports = [...ports, port]; } })