parent
945f8ce431
commit
a95d919b0e
|
|
@ -121,7 +121,7 @@ const getHTML = () => {
|
||||||
if (location.host === "mp.weixin.qq.com") {
|
if (location.host === "mp.weixin.qq.com") {
|
||||||
console.log("微信");
|
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 内容
|
// 格式化 HTML 内容
|
||||||
export const formatHTML = (html: string, extraFormatter?: (doc: Document) => void) => {
|
export const formatHTML = (html: string, extraFormatter?: (doc: Document) => void) => {
|
||||||
|
// 删除空格
|
||||||
|
html = html.replaceAll(" ", "");
|
||||||
|
|
||||||
const nextDocument = document.implementation.createHTMLDocument();
|
const nextDocument = document.implementation.createHTMLDocument();
|
||||||
|
|
||||||
nextDocument.documentElement.innerHTML = html;
|
nextDocument.documentElement.innerHTML = html;
|
||||||
|
|
@ -43,8 +46,8 @@ export const formatHTML = (html: string, extraFormatter?: (doc: Document) => voi
|
||||||
// 优化 p 标签
|
// 优化 p 标签
|
||||||
const paraEl = nextDocument.querySelectorAll("p") as NodeListOf<HTMLElement>;
|
const paraEl = nextDocument.querySelectorAll("p") as NodeListOf<HTMLElement>;
|
||||||
paraEl.forEach((el) => {
|
paraEl.forEach((el) => {
|
||||||
// 删除空的 p 标签
|
// 删除空的 p 标签(没图片的)
|
||||||
if (!el.innerHTML.trim()) {
|
if (!el.innerText.trim() && !el.querySelector("img")) {
|
||||||
el.remove();
|
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 标签
|
// 删除 figure 标签
|
||||||
const figureEl = nextDocument.querySelectorAll("figure") as NodeListOf<HTMLElement>;
|
const figureEl = nextDocument.querySelectorAll("figure") as NodeListOf<HTMLElement>;
|
||||||
figureEl.forEach((el) => {
|
figureEl.forEach((el) => {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue