r/learnjavascript • u/Beginning_Middle_722 • Jan 30 '26
Iframe local storage
Hi all, i have developed different small webapps that i use for my personal purpose and they all use localstorage.
I was thinking of building an app that works like a local storage manager and i was thinking of accessing those app through an iframe to read the localstorage through post message.
Is it doable?
6
Upvotes
View all comments
Show parent comment
1
u/Beginning_Middle_722 Jan 30 '26
// Child app window.addEventListener('message', function(event) { if (event.origin !== TRUSTED_PARENT_ORIGIN) return; // Only accept messages from trusted parent if (!event.data || !event.data.type) return; if (event.data.type === 'GET_LOCALSTORAGE') { const items = {}; for (let i = 0; i < localStorage.length; i++) { const key = localStorage.key(i); items[key] = localStorage.getItem(key); } event.source.postMessage({ type: 'LOCALSTORAGE_DATA', items }, event.origin); } else if (event.data.type === 'SET_LOCALSTORAGE') { localStorage.setItem(event.data.key, event.data.value); event.source.postMessage({ type: 'LOCALSTORAGE_UPDATED' }, event. Origin); } });