From 364940bd46ea643899c7cabee7031717251b4b60 Mon Sep 17 00:00:00 2001 From: Sage Abdullah Date: Sun, 26 Feb 2023 20:13:34 +0000 Subject: [PATCH] web: Prevent postMessage before iframe is loaded --- web/src/giscus.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/web/src/giscus.ts b/web/src/giscus.ts index 19b2d15e..4835b3e7 100644 --- a/web/src/giscus.ts +++ b/web/src/giscus.ts @@ -14,6 +14,7 @@ export class GiscusWidget extends LitElement { private __session = ''; private _iframeRef: Ref = createRef(); private messageEventHandler = this.handleMessageEvent.bind(this); + private hasLoaded = false; get iframeRef() { return this._iframeRef?.value; @@ -217,8 +218,9 @@ export class GiscusWidget extends LitElement { } private sendMessage(message: T) { - if (!this.iframeRef || !this.iframeRef.src.startsWith(this.host)) return; - this.iframeRef?.contentWindow?.postMessage({ giscus: message }, this.host); + if (!this.iframeRef || !this.iframeRef.contentWindow || !this.hasLoaded) + return; + this.iframeRef.contentWindow.postMessage({ giscus: message }, this.host); } private updateConfig() { @@ -245,6 +247,8 @@ export class GiscusWidget extends LitElement { firstUpdated() { this.iframeRef?.addEventListener('load', () => { this.iframeRef?.classList.remove('loading'); + this.hasLoaded = true; + // Make sure to update the config in case the iframe is loaded lazily. this.updateConfig(); }); }