Add parent checking before inserting or removing the child

This commit is contained in:
Phillweston
2024-05-15 12:46:17 +00:00
parent 8c5d31498b
commit a66c416fa4
12 changed files with 34 additions and 15 deletions

View File

@@ -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.");

View File

@@ -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);
}
};
}, []); // 注意依赖数组为空,意味着脚本将仅在加载页面时执行一次

View File

@@ -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 */
}

View File

@@ -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)
}

View File

@@ -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')

View File

@@ -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()
}

View File

@@ -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)
}
}

View File

@@ -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)
}
}

View File

@@ -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)
}
}

View File

@@ -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)
}
}

View File

@@ -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)
}
}
}

View File

@@ -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])
}
}
}
}