Files
kiss-translator/src/libs/webfix.js

159 lines
3.2 KiB
JavaScript
Raw Normal View History

2023-09-08 21:41:32 +08:00
/**
* 修复程序类型
*/
2024-03-16 23:37:27 +08:00
const FIXER_NONE = "-";
2023-11-14 15:15:19 +08:00
const FIXER_BR = "br";
2023-10-16 15:48:53 +08:00
const FIXER_BN = "bn";
2023-11-14 15:15:19 +08:00
const FIXER_BR_DIV = "brToDiv";
const FIXER_BN_DIV = "bnToDiv";
2024-03-16 23:37:27 +08:00
2023-11-14 15:15:19 +08:00
export const FIXER_ALL = [
2024-03-16 23:37:27 +08:00
FIXER_NONE,
2023-11-14 15:15:19 +08:00
FIXER_BR,
FIXER_BN,
FIXER_BR_DIV,
FIXER_BN_DIV,
2023-09-08 16:56:00 +08:00
];
/**
* 修复过的标记
*/
const fixedSign = "kissfixed";
/**
* 采用 `br` 换行网站的修复函数
* 目标是将 `br` 替换成 `p`
* @param {*} node
* @returns
*/
2023-11-14 15:15:19 +08:00
function brFixer(node, tag = "p") {
2023-09-08 16:56:00 +08:00
if (node.hasAttribute(fixedSign)) {
return;
}
node.setAttribute(fixedSign, "true");
2024-02-02 15:44:44 +08:00
const gapTags = ["BR", "WBR"];
const newlineTags = [
2023-09-08 16:56:00 +08:00
"DIV",
"UL",
"OL",
"LI",
"H1",
"H2",
"H3",
"H4",
"H5",
"H6",
"P",
"HR",
"PRE",
"TABLE",
2024-02-05 10:02:30 +08:00
"BLOCKQUOTE",
2023-09-08 16:56:00 +08:00
];
2024-02-02 15:44:44 +08:00
let html = "";
2023-09-08 16:56:00 +08:00
node.childNodes.forEach(function (child, index) {
if (index === 0) {
2023-11-14 15:15:19 +08:00
html += `<${tag} class="kiss-p">`;
2023-09-08 16:56:00 +08:00
}
if (gapTags.indexOf(child.nodeName) !== -1) {
2023-11-14 15:15:19 +08:00
html += `</${tag}><${tag} class="kiss-p">`;
2023-09-08 16:56:00 +08:00
} else if (newlineTags.indexOf(child.nodeName) !== -1) {
2023-11-14 15:15:19 +08:00
html += `</${tag}>${child.outerHTML}<${tag} class="kiss-p">`;
2023-09-08 16:56:00 +08:00
} else if (child.outerHTML) {
html += child.outerHTML;
2024-01-12 16:04:34 +08:00
} else if (child.textContent) {
html += child.textContent;
2023-09-08 16:56:00 +08:00
}
if (index === node.childNodes.length - 1) {
2023-11-14 15:15:19 +08:00
html += `</${tag}>`;
2023-09-08 16:56:00 +08:00
}
});
node.innerHTML = html;
}
2023-11-14 15:15:19 +08:00
function brDivFixer(node) {
return brFixer(node, "div");
}
2023-10-16 15:48:53 +08:00
/**
* 目标是将 `\n` 替换成 `p`
* @param {*} node
* @returns
*/
2023-11-14 15:15:19 +08:00
function bnFixer(node, tag = "p") {
2023-10-16 15:48:53 +08:00
if (node.hasAttribute(fixedSign)) {
return;
}
node.setAttribute(fixedSign, "true");
2023-11-14 15:15:19 +08:00
node.innerHTML = node.innerHTML
.split("\n")
.map((item) => `<${tag} class="kiss-p">${item || "&nbsp;"}</${tag}>`)
.join("");
}
2023-10-16 15:48:53 +08:00
2023-11-14 15:15:19 +08:00
function bnDivFixer(node) {
return bnFixer(node, "div");
2023-10-16 15:48:53 +08:00
}
2023-09-08 21:41:32 +08:00
/**
* 修复程序映射
*/
const fixerMap = {
2023-09-09 23:32:17 +08:00
[FIXER_BR]: brFixer,
2023-10-16 15:48:53 +08:00
[FIXER_BN]: bnFixer,
2023-11-14 15:15:19 +08:00
[FIXER_BR_DIV]: brDivFixer,
[FIXER_BN_DIV]: bnDivFixer,
2023-09-08 21:41:32 +08:00
};
2023-09-08 16:56:00 +08:00
/**
* 查找监听节点并执行修复函数
* @param {*} selector
* @param {*} fixer
2023-09-11 16:12:37 +08:00
* @param {*} rootSelector
2023-09-08 16:56:00 +08:00
*/
2023-09-11 16:12:37 +08:00
function run(selector, fixer, rootSelector) {
2024-02-02 15:44:44 +08:00
const mutaObserver = new MutationObserver(function (mutations) {
2023-09-08 16:56:00 +08:00
mutations.forEach(function (mutation) {
mutation.addedNodes.forEach(function (addNode) {
2023-11-14 10:26:07 +08:00
if (addNode && addNode.querySelectorAll) {
2023-11-14 15:15:19 +08:00
addNode.querySelectorAll(selector).forEach(function (node) {
fixer(node);
});
2023-11-14 10:26:07 +08:00
}
2023-09-08 16:56:00 +08:00
});
});
});
2024-02-02 15:44:44 +08:00
let rootNodes = [document];
2023-09-11 16:12:37 +08:00
if (rootSelector) {
rootNodes = document.querySelectorAll(rootSelector);
2023-09-08 16:56:00 +08:00
}
rootNodes.forEach(function (rootNode) {
2023-11-14 15:15:19 +08:00
rootNode.querySelectorAll(selector).forEach(function (node) {
fixer(node);
});
2023-09-08 16:56:00 +08:00
mutaObserver.observe(rootNode, {
childList: true,
2023-10-16 15:48:53 +08:00
subtree: true,
2023-09-08 16:56:00 +08:00
});
});
}
/**
2023-12-27 15:44:02 +08:00
* 执行fixer
* @param {*} param0
2023-09-08 16:56:00 +08:00
*/
2024-03-21 10:15:23 +08:00
export function runFixer(selector, fixer = "-", rootSelector) {
2023-12-27 15:44:02 +08:00
try {
2024-03-16 23:37:27 +08:00
if (Object.keys(fixerMap).includes(fixer)) {
run(selector, fixerMap[fixer], rootSelector);
2023-09-08 16:56:00 +08:00
}
} catch (err) {
2024-03-16 23:37:27 +08:00
console.error(`[kiss-webfix run]: ${err.message}`);
2023-09-08 16:56:00 +08:00
}
}