From 98d085bf3448fa458d7b55c8866027e5342b519f Mon Sep 17 00:00:00 2001 From: Joschua Becker Date: Tue, 28 May 2024 21:02:41 +0200 Subject: [PATCH] fix: #4 replacement of readOnly content --- mixins/base.ts | 21 ++++++++++++++------- pages/q/[id].vue | 2 +- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/mixins/base.ts b/mixins/base.ts index dd9c6f1..4893c8f 100644 --- a/mixins/base.ts +++ b/mixins/base.ts @@ -14,10 +14,12 @@ export default defineNuxtComponent({ }), async mounted() { const handler = (event: any) => { + if (this.readOnly) return; if (event.origin !== location.origin) return; if (event.data?.type !== 'DUPLICATE_SHARE') return; this.content = event.data.content; window.removeEventListener('message', handler); + event.source.postMessage({ type: 'DUPLICATE_SHARE_OK' }, event.origin); } window.addEventListener('message', handler); const api = await this.getApi() @@ -51,13 +53,18 @@ export default defineNuxtComponent({ if (!this.content) return this.showError('No content to duplicate'); const win = window.open(location.origin, '_blank'); win?.addEventListener('load', async () => { - let counter = 0; - let interval = window.setInterval(() => { - if (counter++ > 200) { - window.clearInterval(interval); - } - win?.postMessage({ type: 'DUPLICATE_SHARE', content: this.content }, location.origin); - }, 100); + let ok = false; + let tries = 0; + win.addEventListener('message', (event) => { + if (event.origin !== location.origin) return; + if (event.data?.type !== 'DUPLICATE_SHARE_OK') return; + ok = true; + }); + while (!ok && tries < 200) { + win.postMessage({ type: 'DUPLICATE_SHARE', content: this.content }, location.origin); + tries++; + await new Promise(r => setTimeout(r, 100)); + } }); }, async newD() { diff --git a/pages/q/[id].vue b/pages/q/[id].vue index d4d9a26..74061ec 100644 --- a/pages/q/[id].vue +++ b/pages/q/[id].vue @@ -48,13 +48,13 @@ export default defineNuxtComponent({ }), async mounted() { try { + this.readOnly = true; const api = await this.getApi(); const secret = location.hash.substring(1); this.decryptURL = api.defaults.baseURL + `decrypt/${this.$route.params.id}/${secret}`; const res = await api.get(`json/${this.$route.params.id}`); this.content = CryptoJS.AES.decrypt(res.data.content, secret).toString(CryptoJS.enc.Utf8); this.isReady = true; - this.readOnly = true; this.expires = res.data.expires; } catch (e) { console.error(e);