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

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