Skip to content

Commit

Permalink
fix: #4 replacement of readOnly content
Browse files Browse the repository at this point in the history
  • Loading branch information
scolastico committed May 28, 2024
1 parent 4a774c8 commit 98d085b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
21 changes: 14 additions & 7 deletions mixins/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion pages/q/[id].vue
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 98d085b

Please sign in to comment.