Created
January 23, 2024 05:36
-
-
Save dongfg/84e4036962a45663f20357982d3da3a9 to your computer and use it in GitHub Desktop.
墨刀 modao.cc 画布目录获取,打开控制台执行
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function removeAttributes(node) { | |
if(!node.attributes) { | |
return; | |
} | |
for (var i = node.attributes.length - 1; i >= 0; i--) { | |
node.removeAttribute(node.attributes[i].name); | |
} | |
// 对于元素节点,递归处理其子节点 | |
if (node.nodeType === Node.ELEMENT_NODE) { | |
for (var child of node.childNodes) { | |
removeAttributes(child); | |
} | |
} | |
} | |
function removeEmptyElements(node) { | |
// 遍历子节点 | |
for (let child of Array.from(node.childNodes)) { | |
if (child.nodeType === Node.ELEMENT_NODE && !child.firstChild) { | |
node.removeChild(child); | |
} else { | |
removeEmptyElements(child); | |
} | |
} | |
} | |
function removeDivElements(node) { | |
for (let child of Array.from(node.childNodes)) { | |
if (child.nodeType === Node.ELEMENT_NODE && child.tagName.toLowerCase() === 'div') { | |
// 如果是div元素 | |
while (child.firstChild) { | |
// 将div的每个子节点移动到其父节点(node)中 | |
node.insertBefore(child.firstChild, child); | |
} | |
// 移除空的div元素 | |
node.removeChild(child); | |
} else { | |
// 对非div节点递归调用该函数 | |
removeDivElements(child); | |
} | |
} | |
} | |
// 获取画布目录的根元素 | |
var container = document.querySelector("#mb-preview-leftpane > div.preview-panel-wrapper > div > div.screen-panel > div.screen-list-container > ul").cloneNode(true); | |
// 移除 svg 标签 | |
var svgs = container.getElementsByTagName("svg"); | |
while (svgs.length > 0) { | |
svgs[0].parentNode.removeChild(svgs[0]); | |
} | |
// 移除标签的属性 | |
removeAttributes(container); | |
// 移除空标签 | |
removeEmptyElements(container); | |
// 执行多次(空标签删除之后父级标签也是空了) | |
removeEmptyElements(container); | |
// 移除div标签 | |
removeDivElements(container); | |
removeDivElements(container); | |
removeDivElements(container); | |
console.log(container.innerHTML); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
根元素如果发生变化自行修改