diff --git a/contents/read.ts b/contents/read.ts index e9ed1ef..fbd915c 100644 --- a/contents/read.ts +++ b/contents/read.ts @@ -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); } // 腾讯云开发者社区 diff --git a/utils/html.ts b/utils/html.ts index a8f5595..c290a32 100644 --- a/utils/html.ts +++ b/utils/html.ts @@ -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; 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; + 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; figureEl.forEach((el) => {