parent
945f8ce431
commit
a95d919b0e
|
|
@ -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);
|
||||
}
|
||||
|
||||
// 腾讯云开发者社区
|
||||
|
|
|
|||
|
|
@ -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) => {
|
||||
|
|
|
|||
Loading…
Reference in New Issue