From 3ff63bbf3d7e1dbc611f94655ff76d0704ecd3f8 Mon Sep 17 00:00:00 2001 From: Ivan Voskoboinyk Date: Fri, 31 May 2024 12:13:25 +0200 Subject: [PATCH 1/3] [CARE-5169] Fix client side exception It's happening on pages with Video node AND OneTrust cookie consent integration > DOMException: Node.insertBefore: Child to insert before is not a child of this node --- src/components/HtmlInjection.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/components/HtmlInjection.tsx b/src/components/HtmlInjection.tsx index 4fc4b05..50694e2 100644 --- a/src/components/HtmlInjection.tsx +++ b/src/components/HtmlInjection.tsx @@ -88,8 +88,8 @@ function useScripts(html: Props['html'], onError: Props['onError']) { // Iframely docs advise to insert their embed script before other scripts if (attributes.src === IFRAMELY_EMBED_SCRIPT_SRC) { const firstScript = document.body.getElementsByTagName('script')[0]; - if (firstScript) { - document.body.insertBefore(script, firstScript); + if (firstScript && firstScript.parentNode) { + firstScript.parentNode.insertBefore(script, firstScript); return; } } From 789300bb722f527df6c4c503a636e413e4aa5c51 Mon Sep 17 00:00:00 2001 From: Ivan Voskoboinyk Date: Fri, 31 May 2024 15:37:11 +0200 Subject: [PATCH 2/3] [CARE-5169] Make sure we only target top-level body scripts --- src/components/HtmlInjection.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/HtmlInjection.tsx b/src/components/HtmlInjection.tsx index 50694e2..f1e7036 100644 --- a/src/components/HtmlInjection.tsx +++ b/src/components/HtmlInjection.tsx @@ -87,7 +87,7 @@ function useScripts(html: Props['html'], onError: Props['onError']) { // Iframely docs advise to insert their embed script before other scripts if (attributes.src === IFRAMELY_EMBED_SCRIPT_SRC) { - const firstScript = document.body.getElementsByTagName('script')[0]; + const firstScript = document.body.querySelectorAll('body > script')[0]; if (firstScript && firstScript.parentNode) { firstScript.parentNode.insertBefore(script, firstScript); return; From bcd6f27e714a8cab527f120e67b2cb8baffaec68 Mon Sep 17 00:00:00 2001 From: Ivan Voskoboinyk Date: Fri, 31 May 2024 15:38:16 +0200 Subject: [PATCH 3/3] [CARE-5169] Run the CSS selector on the document object --- src/components/HtmlInjection.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/HtmlInjection.tsx b/src/components/HtmlInjection.tsx index f1e7036..e671ee7 100644 --- a/src/components/HtmlInjection.tsx +++ b/src/components/HtmlInjection.tsx @@ -87,7 +87,7 @@ function useScripts(html: Props['html'], onError: Props['onError']) { // Iframely docs advise to insert their embed script before other scripts if (attributes.src === IFRAMELY_EMBED_SCRIPT_SRC) { - const firstScript = document.body.querySelectorAll('body > script')[0]; + const firstScript = document.querySelectorAll('body > script')[0]; if (firstScript && firstScript.parentNode) { firstScript.parentNode.insertBefore(script, firstScript); return;