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_BR_DIV = "brToDiv";
const FIXER_BN_DIV = "bnToDiv";
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
* @returns
*/
function brFixer(node) {
function brFixer(node, tag = "p") {
if (node.hasAttribute(fixedSign)) {
return;
}
@@ -82,13 +90,13 @@ function brFixer(node) {
var html = "";
node.childNodes.forEach(function (child, index) {
if (index === 0) {
html += "<p>";
html += `<${tag} class="kiss-p">`;
}
if (gapTags.indexOf(child.nodeName) !== -1) {
html += "</p><p>";
html += `</${tag}><${tag} class="kiss-p">`;
} else if (newlineTags.indexOf(child.nodeName) !== -1) {
html += "</p>" + child.outerHTML + "<p>";
html += `</${tag}>${child.outerHTML}<${tag} class="kiss-p">`;
} else if (child.outerHTML) {
html += child.outerHTML;
} else if (child.nodeValue) {
@@ -96,30 +104,34 @@ function brFixer(node) {
}
if (index === node.childNodes.length - 1) {
html += "</p>";
html += `</${tag}>`;
}
});
node.innerHTML = html;
}
function brDivFixer(node) {
return brFixer(node, "div");
}
/**
* 目标是将 `\n` 替换成 `p`
* @param {*} node
* @returns
*/
function bnFixer(node) {
function bnFixer(node, tag = "p") {
if (node.hasAttribute(fixedSign)) {
return;
}
node.setAttribute(fixedSign, "true");
const childs = node.childNodes;
if (childs.length === 1 && childs[0].nodeName === "#text") {
node.innerHTML = node.innerHTML
.split("\n")
.map((item) => `<p>${item || "&nbsp;"}</p>`)
.map((item) => `<${tag} class="kiss-p">${item || "&nbsp;"}</${tag}>`)
.join("");
}
}
function bnDivFixer(node) {
return bnFixer(node, "div");
}
/**
@@ -136,6 +148,8 @@ function fontSizeFixer(node) {
const fixerMap = {
[FIXER_BR]: brFixer,
[FIXER_BN]: bnFixer,
[FIXER_BR_DIV]: brDivFixer,
[FIXER_BN_DIV]: bnDivFixer,
[FIXER_FONTSIZE]: fontSizeFixer,
};
@@ -150,7 +164,9 @@ function run(selector, fixer, rootSelector) {
mutations.forEach(function (mutation) {
mutation.addedNodes.forEach(function (addNode) {
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) {
rootNode.querySelectorAll(selector).forEach(fixer);
rootNode.querySelectorAll(selector).forEach(function (node) {
fixer(node);
});
mutaObserver.observe(rootNode, {
childList: true,
subtree: true,
@@ -204,12 +222,14 @@ export const loadOrFetchWebfix = async (url) => {
*/
export async function runWebfix({ injectWebfix }) {
try {
const href = document.location.href;
let sites = await getWebfixRulesWithDefault();
if (injectWebfix) {
const subSites = await loadOrFetchWebfix(process.env.REACT_APP_WEBFIXURL);
sites = [...sites, ...subSites];
if (!injectWebfix) {
return;
}
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++) {
var site = sites[i];
if (isMatch(href, site.pattern)) {

View File

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