From a66c416fa4f3041ae81e8d7363c3ca38d4936a0a Mon Sep 17 00:00:00 2001 From: Phillweston <2436559745@qq.com> Date: Wed, 15 May 2024 12:46:17 +0000 Subject: [PATCH] Add parent checking before inserting or removing the child --- components/CustomContextMenu.js | 4 +++- components/DifyChatbot.js | 4 +++- components/FacebookMessenger.js | 6 +++++- components/PWA.js | 2 +- components/PrismMac.js | 2 +- lib/plugins/busuanzi.js | 5 ++++- public/js/flutteringRibbon.js | 2 +- public/js/nest.js | 2 +- public/js/ribbon.js | 2 +- public/js/sakura.js | 10 ++++++---- themes/movie/index.js | 6 +++++- themes/theme.js | 4 +++- 12 files changed, 34 insertions(+), 15 deletions(-) diff --git a/components/CustomContextMenu.js b/components/CustomContextMenu.js index 46ffb2a0..282c9244 100644 --- a/components/CustomContextMenu.js +++ b/components/CustomContextMenu.js @@ -111,7 +111,9 @@ export default function CustomContextMenu(props) { document.body.appendChild(tempInput); tempInput.select(); document.execCommand('copy'); - document.body.removeChild(tempInput); + if (tempInput && tempInput.parentNode && tempInput.parentNode.contains(tempInput)) { + tempInput.parentNode.removeChild(tempInput); + } // alert("Text copied: " + selectedText); } else { // alert("Please select some text first."); diff --git a/components/DifyChatbot.js b/components/DifyChatbot.js index c954a872..6190c58b 100644 --- a/components/DifyChatbot.js +++ b/components/DifyChatbot.js @@ -24,7 +24,9 @@ export default function DifyChatbot() { return () => { // 在组件卸载时清理 script 标签 const existingScript = document.getElementById(siteConfig('DIFY_CHATBOT_TOKEN')); // 注意调用 siteConfig() - if (existingScript) document.body.removeChild(existingScript); + if (existingScript && existingScript.parentNode && existingScript.parentNode.contains(existingScript)) { + existingScript.parentNode.removeChild(existingScript); + } }; }, []); // 注意依赖数组为空,意味着脚本将仅在加载页面时执行一次 diff --git a/components/FacebookMessenger.js b/components/FacebookMessenger.js index ef8da0cb..9531b322 100644 --- a/components/FacebookMessenger.js +++ b/components/FacebookMessenger.js @@ -116,7 +116,11 @@ class MessengerCustomerChat extends Component { js = d.createElement(s); js.id = id; js.src = `https://connect.facebook.net/${language}/sdk/xfbml.customerchat.js`; - fjs.parentNode.insertBefore(js, fjs); + if (fjs && fjs.parentNode && fjs.parentNode.contains(fjs)) { + fjs.parentNode.insertBefore(js, fjs); + } else { + document.body.appendChild(js); + } })(document, 'script', 'facebook-jssdk'); /* eslint-enable */ } diff --git a/components/PWA.js b/components/PWA.js index 118a7c18..2a292100 100644 --- a/components/PWA.js +++ b/components/PWA.js @@ -34,7 +34,7 @@ export function PWA(post, siteInfo) { // 删除已有的 manifest link 元素(如果存在) const existingManifest = document.querySelector('link[rel="manifest"]') - if (existingManifest) { + if (existingManifest && existingManifest.parentNode && existingManifest.parentNode.contains(existingManifest)) { existingManifest.parentNode.removeChild(existingManifest) } diff --git a/components/PrismMac.js b/components/PrismMac.js index c92d594d..5d04fb22 100644 --- a/components/PrismMac.js +++ b/components/PrismMac.js @@ -74,7 +74,7 @@ const loadPrismThemeCSS = (isDarkMode, prismThemeSwitch, prismThemeDarkPath, pri PRISM_PREVIOUS = prismThemeDarkPath } const previousTheme = document.querySelector(`link[href="${PRISM_PREVIOUS}"]`) - if (previousTheme) { + if (previousTheme && previousTheme.parentNode && previousTheme.parentNode.contains(previousTheme)) { previousTheme.parentNode.removeChild(previousTheme) } loadExternalResource(PRISM_THEME, 'css') diff --git a/lib/plugins/busuanzi.js b/lib/plugins/busuanzi.js index 439aa4b7..9186078a 100644 --- a/lib/plugins/busuanzi.js +++ b/lib/plugins/busuanzi.js @@ -43,7 +43,10 @@ bszCaller = { return function (t) { ready(function () { try { - e(t), scriptTag && scriptTag.parentElement && scriptTag.parentElement.removeChild && scriptTag.parentElement.removeChild(scriptTag) + e(t) + if (scriptTag && scriptTag.parentElement && scriptTag.parentElement.contains(scriptTag)) { + scriptTag.parentElement.removeChild(scriptTag) + } } catch (t) { // console.log(t), bszTag.hides() } diff --git a/public/js/flutteringRibbon.js b/public/js/flutteringRibbon.js index d698a117..e701ebd2 100644 --- a/public/js/flutteringRibbon.js +++ b/public/js/flutteringRibbon.js @@ -3,7 +3,7 @@ const idFlutteringRibbon = 'canvasFlutteringRibbon' function destroyFlutteringRibbon() { const ribbon = document.getElementById(idFlutteringRibbon) - if (ribbon && ribbon.parentNode) { + if (ribbon && ribbon.parentNode && ribbon.parentNode.contains(ribbon)) { ribbon.parentNode.removeChild(ribbon) } } diff --git a/public/js/nest.js b/public/js/nest.js index 2b151d00..2dc5cf25 100644 --- a/public/js/nest.js +++ b/public/js/nest.js @@ -109,7 +109,7 @@ function createNest() { function destroyNest() { const nest = document.getElementById(idNest) - if(nest && nest.parentNode){ + if (nest && nest.parentNode && nest.parentNode.contains(nest)) { nest.parentNode.removeChild(nest) } } diff --git a/public/js/ribbon.js b/public/js/ribbon.js index 928ccd11..55c6731c 100644 --- a/public/js/ribbon.js +++ b/public/js/ribbon.js @@ -80,7 +80,7 @@ function createRibbon() { function destroyRibbon() { const ribbon = document.getElementById(idRibbon) - if (ribbon && ribbon.parentNode) { + if (ribbon && ribbon.parentNode && ribbon.parentNode.contains(ribbon)) { ribbon.parentNode.removeChild(ribbon) } } diff --git a/public/js/sakura.js b/public/js/sakura.js index 4d246a35..fcdf0739 100644 --- a/public/js/sakura.js +++ b/public/js/sakura.js @@ -165,9 +165,11 @@ function createSakura() { function stopp() { if (staticx) { var child = document.getElementById(id) - child.parentNode.removeChild(child) - window.cancelAnimationFrame(stop) - staticx = false + if (child && child.parentNode && child.parentNode.contains(child)) { + child.parentNode.removeChild(child) + window.cancelAnimationFrame(stop) + staticx = false + } } else { startSakura() } @@ -177,7 +179,7 @@ function createSakura() { // 销毁樱花雨 function destroySakura() { const sakura = document.getElementById(idSakura) - if (sakura && sakura.parentNode) { + if (sakura && sakura.parentNode && sakura.parentNode.contains(sakura)) { sakura.parentNode.removeChild(sakura) } } diff --git a/themes/movie/index.js b/themes/movie/index.js index 96195bf2..b62b543f 100644 --- a/themes/movie/index.js +++ b/themes/movie/index.js @@ -258,7 +258,11 @@ const LayoutSlug = props => { videoWrapper.appendChild(figCaptionWrapper) } // 放入页面 - notionArticle.insertBefore(videoWrapper, notionArticle.firstChild) + if (notionArticle.firstChild && notionArticle.contains(notionArticle.firstChild)) { + notionArticle.insertBefore(videoWrapper, notionArticle.firstChild) + } else { + notionArticle.appendChild(videoWrapper) + } } } diff --git a/themes/theme.js b/themes/theme.js index fb4f94f0..8192c96f 100644 --- a/themes/theme.js +++ b/themes/theme.js @@ -82,7 +82,9 @@ const checkThemeDOM = () => { elements[elements.length - 1].scrollIntoView() // 删除前面的元素,只保留最后一个元素 for (let i = 0; i < elements.length - 1; i++) { - elements[i].parentNode.removeChild(elements[i]) + if (elements[i] && elements[i].parentNode && elements[i].parentNode.contains(elements[i])) { + elements[i].parentNode.removeChild(elements[i]) + } } } }