Fix weixin html format

优化格式解析
This commit is contained in:
奇趣保罗 2024-08-08 10:16:33 +08:00
parent 945f8ce431
commit a95d919b0e
2 changed files with 16 additions and 3 deletions

View File

@ -121,7 +121,7 @@ const getHTML = () => {
if (location.host === "mp.weixin.qq.com") {
console.log("微信");
return document.querySelector(".rich_media_content").innerHTML;
return formatHTML(document.querySelector(".rich_media_content").innerHTML);
}
// 腾讯云开发者社区

View File

@ -27,6 +27,9 @@ const removeAttributes = (el: HTMLElement) => {
// 格式化 HTML 内容
export const formatHTML = (html: string, extraFormatter?: (doc: Document) => void) => {
// 删除空格
html = html.replaceAll(" ", "");
const nextDocument = document.implementation.createHTMLDocument();
nextDocument.documentElement.innerHTML = html;
@ -43,8 +46,8 @@ export const formatHTML = (html: string, extraFormatter?: (doc: Document) => voi
// 优化 p 标签
const paraEl = nextDocument.querySelectorAll("p") as NodeListOf<HTMLElement>;
paraEl.forEach((el) => {
// 删除空的 p 标签
if (!el.innerHTML.trim()) {
// 删除空的 p 标签(没图片的)
if (!el.innerText.trim() && !el.querySelector("img")) {
el.remove();
}
@ -58,6 +61,16 @@ export const formatHTML = (html: string, extraFormatter?: (doc: Document) => voi
});
});
// 优化 img 标签,仅保留有效内容
const imgEl = nextDocument.querySelectorAll("img") as NodeListOf<HTMLImageElement>;
imgEl.forEach((el) => {
const nextImgEl = document.createElement("img");
nextImgEl.src = el.src;
nextImgEl.alt = el.alt;
el.parentNode.replaceChild(nextImgEl, el);
});
// 删除 figure 标签
const figureEl = nextDocument.querySelectorAll("figure") as NodeListOf<HTMLElement>;
figureEl.forEach((el) => {