From a95d919b0ec7b4b5a54d603edca8813cac90d22b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A5=87=E8=B6=A3=E4=BF=9D=E7=BD=97?= Date: Thu, 8 Aug 2024 10:16:33 +0800 Subject: [PATCH] Fix weixin html format MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 优化格式解析 --- contents/read.ts | 2 +- utils/html.ts | 17 +++++++++++++++-- 2 files changed, 16 insertions(+), 3 deletions(-) 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) => {