update webfix

This commit is contained in:
Gabe Yuan
2023-11-14 15:15:19 +08:00
parent 8067f34ce6
commit 0a32f94d32
2 changed files with 44 additions and 29 deletions

View File

@@ -5,10 +5,18 @@ import { apiFetch } from "../apis";
/** /**
* 修复程序类型 * 修复程序类型
*/ */
export const FIXER_BR = "br"; const FIXER_BR = "br";
const FIXER_BN = "bn"; const FIXER_BN = "bn";
const FIXER_BR_DIV = "brToDiv";
const FIXER_BN_DIV = "bnToDiv";
const FIXER_FONTSIZE = "fontSize"; const FIXER_FONTSIZE = "fontSize";
export const FIXER_ALL = [FIXER_BR, FIXER_BN, FIXER_FONTSIZE]; export const FIXER_ALL = [
FIXER_BR,
FIXER_BN,
FIXER_BR_DIV,
FIXER_BN_DIV,
FIXER_FONTSIZE,
];
/** /**
* 需要修复的站点列表 * 需要修复的站点列表
@@ -55,7 +63,7 @@ const fixedSign = "kissfixed";
* @param {*} node * @param {*} node
* @returns * @returns
*/ */
function brFixer(node) { function brFixer(node, tag = "p") {
if (node.hasAttribute(fixedSign)) { if (node.hasAttribute(fixedSign)) {
return; return;
} }
@@ -82,13 +90,13 @@ function brFixer(node) {
var html = ""; var html = "";
node.childNodes.forEach(function (child, index) { node.childNodes.forEach(function (child, index) {
if (index === 0) { if (index === 0) {
html += "<p>"; html += `<${tag} class="kiss-p">`;
} }
if (gapTags.indexOf(child.nodeName) !== -1) { if (gapTags.indexOf(child.nodeName) !== -1) {
html += "</p><p>"; html += `</${tag}><${tag} class="kiss-p">`;
} else if (newlineTags.indexOf(child.nodeName) !== -1) { } else if (newlineTags.indexOf(child.nodeName) !== -1) {
html += "</p>" + child.outerHTML + "<p>"; html += `</${tag}>${child.outerHTML}<${tag} class="kiss-p">`;
} else if (child.outerHTML) { } else if (child.outerHTML) {
html += child.outerHTML; html += child.outerHTML;
} else if (child.nodeValue) { } else if (child.nodeValue) {
@@ -96,30 +104,34 @@ function brFixer(node) {
} }
if (index === node.childNodes.length - 1) { if (index === node.childNodes.length - 1) {
html += "</p>"; html += `</${tag}>`;
} }
}); });
node.innerHTML = html; node.innerHTML = html;
} }
function brDivFixer(node) {
return brFixer(node, "div");
}
/** /**
* 目标是将 `\n` 替换成 `p` * 目标是将 `\n` 替换成 `p`
* @param {*} node * @param {*} node
* @returns * @returns
*/ */
function bnFixer(node) { function bnFixer(node, tag = "p") {
if (node.hasAttribute(fixedSign)) { if (node.hasAttribute(fixedSign)) {
return; return;
} }
node.setAttribute(fixedSign, "true"); node.setAttribute(fixedSign, "true");
node.innerHTML = node.innerHTML
.split("\n")
.map((item) => `<${tag} class="kiss-p">${item || "&nbsp;"}</${tag}>`)
.join("");
}
const childs = node.childNodes; function bnDivFixer(node) {
if (childs.length === 1 && childs[0].nodeName === "#text") { return bnFixer(node, "div");
node.innerHTML = node.innerHTML
.split("\n")
.map((item) => `<p>${item || "&nbsp;"}</p>`)
.join("");
}
} }
/** /**
@@ -136,6 +148,8 @@ function fontSizeFixer(node) {
const fixerMap = { const fixerMap = {
[FIXER_BR]: brFixer, [FIXER_BR]: brFixer,
[FIXER_BN]: bnFixer, [FIXER_BN]: bnFixer,
[FIXER_BR_DIV]: brDivFixer,
[FIXER_BN_DIV]: bnDivFixer,
[FIXER_FONTSIZE]: fontSizeFixer, [FIXER_FONTSIZE]: fontSizeFixer,
}; };
@@ -150,7 +164,9 @@ function run(selector, fixer, rootSelector) {
mutations.forEach(function (mutation) { mutations.forEach(function (mutation) {
mutation.addedNodes.forEach(function (addNode) { mutation.addedNodes.forEach(function (addNode) {
if (addNode && addNode.querySelectorAll) { if (addNode && addNode.querySelectorAll) {
addNode.querySelectorAll(selector).forEach(fixer); addNode.querySelectorAll(selector).forEach(function (node) {
fixer(node);
});
} }
}); });
}); });
@@ -162,7 +178,9 @@ function run(selector, fixer, rootSelector) {
} }
rootNodes.forEach(function (rootNode) { rootNodes.forEach(function (rootNode) {
rootNode.querySelectorAll(selector).forEach(fixer); rootNode.querySelectorAll(selector).forEach(function (node) {
fixer(node);
});
mutaObserver.observe(rootNode, { mutaObserver.observe(rootNode, {
childList: true, childList: true,
subtree: true, subtree: true,
@@ -204,12 +222,14 @@ export const loadOrFetchWebfix = async (url) => {
*/ */
export async function runWebfix({ injectWebfix }) { export async function runWebfix({ injectWebfix }) {
try { try {
const href = document.location.href; if (!injectWebfix) {
let sites = await getWebfixRulesWithDefault(); return;
if (injectWebfix) {
const subSites = await loadOrFetchWebfix(process.env.REACT_APP_WEBFIXURL);
sites = [...sites, ...subSites];
} }
const href = document.location.href;
const userSites = await getWebfixRulesWithDefault();
const subSites = await loadOrFetchWebfix(process.env.REACT_APP_WEBFIXURL);
const sites = [...userSites, ...subSites];
for (var i = 0; i < sites.length; i++) { for (var i = 0; i < sites.length; i++) {
var site = sites[i]; var site = sites[i];
if (isMatch(href, site.pattern)) { if (isMatch(href, site.pattern)) {

View File

@@ -13,12 +13,7 @@ import FormControlLabel from "@mui/material/FormControlLabel";
import Switch from "@mui/material/Switch"; import Switch from "@mui/material/Switch";
import { useSetting } from "../../hooks/Setting"; import { useSetting } from "../../hooks/Setting";
import CircularProgress from "@mui/material/CircularProgress"; import CircularProgress from "@mui/material/CircularProgress";
import { import { syncWebfix, loadOrFetchWebfix, FIXER_ALL } from "../../libs/webfix";
syncWebfix,
loadOrFetchWebfix,
FIXER_BR,
FIXER_ALL,
} from "../../libs/webfix";
import Button from "@mui/material/Button"; import Button from "@mui/material/Button";
import SyncIcon from "@mui/icons-material/Sync"; import SyncIcon from "@mui/icons-material/Sync";
import { useAlert } from "../../hooks/Alert"; import { useAlert } from "../../hooks/Alert";
@@ -33,7 +28,7 @@ function WebfixFields({ rule, webfix, setShow }) {
pattern: "", pattern: "",
selector: "", selector: "",
rootSelector: "", rootSelector: "",
fixer: FIXER_BR, fixer: FIXER_ALL[0],
}; };
const i18n = useI18n(); const i18n = useI18n();
const [disabled, setDisabled] = useState(editMode); const [disabled, setDisabled] = useState(editMode);