UNPKG

i18n-behavior

Version:

Instant and Modular I18N engine for lit-html and Polymer

55 lines (54 loc) 2.01 kB
<!-- @license https://github.com/t2ym/i18n-behavior/blob/master/LICENSE.md Copyright (c) 2019, Tetsuya Mori <t2y3141592@gmail.com>. All rights reserved. --> <html lang="en"> <head> <meta charset="utf-8"> <script> onload = () => { const url = location.href; const key = url.match(/[?]key=([^&]*)&/)[1]; const value = url.match(/&value=([^&]*)&/)[1]; const origin = decodeURIComponent(url.match(/&origin=([^&]*)/)[1]); if (key && value && origin) { let isStorageEventDispatched = false; let oldValue = localStorage.getItem(key); let onStorage = (event) => { console.log('onStorage'); isStorageEventDispatched = true; }; window.parent.addEventListener('storage', onStorage); localStorage.setItem(key, JSON.stringify(value)); document.body.innerHTML = ` <pre> key = ${key} value = ${value} origin = ${origin} location.href = ${location.href} </pre> `; console.log(`${location.href}: key=${key} value=${value} origin=${origin}`); setTimeout(() => { if (!isStorageEventDispatched) { console.log(`isStorageEventDispatched=${isStorageEventDispatched}`); // mock the storage event console.log('mock the StorageEvent'); let initStorageEvent = window.parent.Object.create(null); initStorageEvent.key = key; initStorageEvent.oldValue = oldValue; initStorageEvent.newValue = JSON.stringify(value); initStorageEvent.storageArea = localStorage; initStorageEvent.url = location.href; window.parent.dispatchEvent(new window.parent.StorageEvent('storage', initStorageEvent)); } window.parent.postMessage('setItem', origin); window.parent.removeEventListener('storage', onStorage); }, 100); } } </script> </head> <body> </body> </html>