feat: move fixer to rules

This commit is contained in:
Gabe Yuan
2024-03-21 11:40:47 +08:00
parent 3ea14c1687
commit ac921cd5a0
3 changed files with 65 additions and 29 deletions

View File

@@ -1,11 +1,11 @@
export const DEFAULT_SELECTOR = `:is(li, p, h1, h2, h3, h4, h5, h6, dd, blockquote)`; import { FIXER_BR, FIXER_BN, FIXER_BR_DIV, FIXER_BN_DIV } from "../libs/webfix";
export const DEFAULT_KEEP_SELECTOR = `code, img, svg`;
export const GLOBAL_KEY = "*"; export const GLOBAL_KEY = "*";
export const REMAIN_KEY = "-"; export const REMAIN_KEY = "-";
export const SHADOW_KEY = ">>>"; export const SHADOW_KEY = ">>>";
export const DEFAULT_SELECTOR = `:is(li, p, h1, h2, h3, h4, h5, h6, dd, blockquote)`;
export const DEFAULT_KEEP_SELECTOR = `code, img, svg`;
export const DEFAULT_RULE = { export const DEFAULT_RULE = {
pattern: "", // 匹配网址 pattern: "", // 匹配网址
selector: "", // 选择器 selector: "", // 选择器
@@ -124,18 +124,18 @@ const RULES_MAP = {
}, },
"deno.land, docs.github.com": { "deno.land, docs.github.com": {
selector: `main ${DEFAULT_SELECTOR}`, selector: `main ${DEFAULT_SELECTOR}`,
keepSelector: `code, img, svg`, keepSelector: DEFAULT_KEEP_SELECTOR,
}, },
"doc.rust-lang.org": { "doc.rust-lang.org": {
selector: `.content ${DEFAULT_SELECTOR}`, selector: `.content ${DEFAULT_SELECTOR}`,
keepSelector: `code, img, svg`, keepSelector: DEFAULT_KEEP_SELECTOR,
}, },
"www.indiehackers.com": { "www.indiehackers.com": {
selector: `h1, h3, .content ${DEFAULT_SELECTOR}, .feed-item__title-link`, selector: `h1, h3, .content ${DEFAULT_SELECTOR}, .feed-item__title-link`,
}, },
"platform.openai.com/docs": { "platform.openai.com/docs": {
selector: `.docs-body ${DEFAULT_SELECTOR}`, selector: `.docs-body ${DEFAULT_SELECTOR}`,
keepSelector: `code, img, svg`, keepSelector: DEFAULT_KEEP_SELECTOR,
}, },
"en.wikipedia.org": { "en.wikipedia.org": {
selector: `h1, .mw-parser-output ${DEFAULT_SELECTOR}`, selector: `h1, .mw-parser-output ${DEFAULT_SELECTOR}`,
@@ -144,7 +144,7 @@ const RULES_MAP = {
"stackoverflow.com, serverfault.com, superuser.com, stackexchange.com, askubuntu.com, stackapps.com, mathoverflow.net": "stackoverflow.com, serverfault.com, superuser.com, stackexchange.com, askubuntu.com, stackapps.com, mathoverflow.net":
{ {
selector: `.s-prose ${DEFAULT_SELECTOR}, .comment-copy, .question-hyperlink, .s-post-summary--content-title, .s-post-summary--content-excerpt`, selector: `.s-prose ${DEFAULT_SELECTOR}, .comment-copy, .question-hyperlink, .s-post-summary--content-title, .s-post-summary--content-excerpt`,
keepSelector: `code, img, svg, .math-container`, keepSelector: `${DEFAULT_KEEP_SELECTOR}, .math-container`,
}, },
"www.npmjs.com/package, developer.chrome.com/docs, medium.com, react.dev, create-react-app.dev, pytorch.org": "www.npmjs.com/package, developer.chrome.com/docs, medium.com, react.dev, create-react-app.dev, pytorch.org":
{ {
@@ -152,10 +152,12 @@ const RULES_MAP = {
}, },
"news.ycombinator.com": { "news.ycombinator.com": {
selector: `.title, p`, selector: `.title, p`,
fixerSelector: `.toptext, .commtext`,
fixerFunc: FIXER_BR,
}, },
"github.com": { "github.com": {
selector: `.markdown-body ${DEFAULT_SELECTOR}, .repo-description p, .Layout-sidebar .f4, .container-lg .py-4 .f5, .container-lg .my-4 .f5, .Box-row .pr-4, .Box-row article .mt-1, [itemprop="description"], .markdown-title, bdi, .ws-pre-wrap, .status-meta, span.status-meta, .col-10.color-fg-muted, .TimelineItem-body, .pinned-item-list-item-content .color-fg-muted, .markdown-body td, .markdown-body th`, selector: `.markdown-body ${DEFAULT_SELECTOR}, .repo-description p, .Layout-sidebar .f4, .container-lg .py-4 .f5, .container-lg .my-4 .f5, .Box-row .pr-4, .Box-row article .mt-1, [itemprop="description"], .markdown-title, bdi, .ws-pre-wrap, .status-meta, span.status-meta, .col-10.color-fg-muted, .TimelineItem-body, .pinned-item-list-item-content .color-fg-muted, .markdown-body td, .markdown-body th`,
keepSelector: `code, img, svg`, keepSelector: DEFAULT_KEEP_SELECTOR,
}, },
"twitter.com": { "twitter.com": {
selector: `[data-testid="tweetText"], [data-testid="birdwatch-pivot"]>div.css-1rynq56`, selector: `[data-testid="tweetText"], [data-testid="birdwatch-pivot"]>div.css-1rynq56`,
@@ -165,11 +167,13 @@ const RULES_MAP = {
selector: `.slim-video-information-title .yt-core-attributed-string, .media-item-headline .yt-core-attributed-string, .comment-text .yt-core-attributed-string, .typography-body-2b .yt-core-attributed-string, #ytp-caption-window-container .ytp-caption-segment`, selector: `.slim-video-information-title .yt-core-attributed-string, .media-item-headline .yt-core-attributed-string, .comment-text .yt-core-attributed-string, .typography-body-2b .yt-core-attributed-string, #ytp-caption-window-container .ytp-caption-segment`,
selectStyle: `-webkit-line-clamp: unset; max-height: none; height: auto;`, selectStyle: `-webkit-line-clamp: unset; max-height: none; height: auto;`,
parentStyle: `-webkit-line-clamp: unset; max-height: none; height: auto;`, parentStyle: `-webkit-line-clamp: unset; max-height: none; height: auto;`,
keepSelector: `img, #content-text>a`,
}, },
"www.youtube.com": { "www.youtube.com": {
selector: `h1, #video-title, #content-text, #title, yt-attributed-string>span>span, #ytp-caption-window-container .ytp-caption-segment`, selector: `h1, #video-title, #content-text, #title, yt-attributed-string>span>span, #ytp-caption-window-container .ytp-caption-segment`,
selectStyle: `-webkit-line-clamp: unset; max-height: none; height: auto;`, selectStyle: `-webkit-line-clamp: unset; max-height: none; height: auto;`,
parentStyle: `-webkit-line-clamp: unset; max-height: none; height: auto;`, parentStyle: `-webkit-line-clamp: unset; max-height: none; height: auto;`,
keepSelector: `img, #content-text>a`,
}, },
"bard.google.com": { "bard.google.com": {
selector: `.query-content ${DEFAULT_SELECTOR}, message-content ${DEFAULT_SELECTOR}`, selector: `.query-content ${DEFAULT_SELECTOR}, message-content ${DEFAULT_SELECTOR}`,
@@ -179,6 +183,8 @@ const RULES_MAP = {
}, },
"www.phoronix.com": { "www.phoronix.com": {
selector: `article ${DEFAULT_SELECTOR}`, selector: `article ${DEFAULT_SELECTOR}`,
fixerSelector: `.content`,
fixerFunc: FIXER_BR,
}, },
"wx2.qq.com": { "wx2.qq.com": {
selector: `.js_message_plain`, selector: `.js_message_plain`,
@@ -192,14 +198,20 @@ const RULES_MAP = {
}, },
"t.me/s/": { "t.me/s/": {
selector: `.js-message_text ${DEFAULT_SELECTOR}`, selector: `.js-message_text ${DEFAULT_SELECTOR}`,
fixerSelector: `.tgme_widget_message_text`,
fixerFunc: FIXER_BR,
}, },
"web.telegram.org/k": { "web.telegram.org/k": {
selector: `div.kiss-p`, selector: `div.kiss-p`,
keepSelector: `div[class^=time], .peer-title, .document-wrapper, .message.spoilers-container custom-emoji-element, reactions-element`, keepSelector: `div[class^=time], .peer-title, .document-wrapper, .message.spoilers-container custom-emoji-element, reactions-element`,
fixerSelector: `.message`,
fixerFunc: FIXER_BN_DIV,
}, },
"web.telegram.org/a": { "web.telegram.org/a": {
selector: `.text-content > .kiss-p`, selector: `.text-content > .kiss-p`,
keepSelector: `.Reactions, .time, .peer-title, .document-wrapper, .message.spoilers-container custom-emoji-element`, keepSelector: `.Reactions, .time, .peer-title, .document-wrapper, .message.spoilers-container custom-emoji-element`,
fixerSelector: `.text-content`,
fixerFunc: FIXER_BR_DIV,
}, },
"www.instagram.com/": { "www.instagram.com/": {
selector: `h1, article span[dir=auto] > span[dir=auto], ._ab1y`, selector: `h1, article span[dir=auto] > span[dir=auto], ._ab1y`,
@@ -208,26 +220,34 @@ const RULES_MAP = {
selector: `h1, div[class='x9f619 xjbqb8w x78zum5 x168nmei x13lgxp2 x5pf9jr xo71vjh x1uhb9sk x1plvlek xryxfnj x1c4vz4f x2lah0s xdt5ytf xqjyukv x1cy8zhl x1oa3qoh x1nhvcw1'] > span[class='x1lliihq x1plvlek xryxfnj x1n2onr6 x193iq5w xeuugli x1fj9vlw x13faqbe x1vvkbs x1s928wv xhkezso x1gmr53x x1cpjm7i x1fgarty x1943h6x x1i0vuye xvs91rp xo1l8bm x5n08af x10wh9bi x1wdrske x8viiok x18hxmgj'], span[class='x193iq5w xeuugli x1fj9vlw x13faqbe x1vvkbs xt0psk2 x1i0vuye xvs91rp xo1l8bm x5n08af x10wh9bi x1wdrske x8viiok x18hxmgj']`, selector: `h1, div[class='x9f619 xjbqb8w x78zum5 x168nmei x13lgxp2 x5pf9jr xo71vjh x1uhb9sk x1plvlek xryxfnj x1c4vz4f x2lah0s xdt5ytf xqjyukv x1cy8zhl x1oa3qoh x1nhvcw1'] > span[class='x1lliihq x1plvlek xryxfnj x1n2onr6 x193iq5w xeuugli x1fj9vlw x13faqbe x1vvkbs x1s928wv xhkezso x1gmr53x x1cpjm7i x1fgarty x1943h6x x1i0vuye xvs91rp xo1l8bm x5n08af x10wh9bi x1wdrske x8viiok x18hxmgj'], span[class='x193iq5w xeuugli x1fj9vlw x13faqbe x1vvkbs xt0psk2 x1i0vuye xvs91rp xo1l8bm x5n08af x10wh9bi x1wdrske x8viiok x18hxmgj']`,
}, },
"mail.google.com": { "mail.google.com": {
selector: `${DEFAULT_SELECTOR}, span[data-thread-id]`, selector: `.a3s.aiL ${DEFAULT_SELECTOR}, span[data-thread-id]`,
fixerSelector: `.a3s.aiL`,
fixerFunc: FIXER_BR,
}, },
"web.whatsapp.com": { "web.whatsapp.com": {
selector: `.copyable-text > span`, selector: `.copyable-text > span`,
}, },
"chat.openai.com": { "chat.openai.com": {
selector: `div[data-message-author-role] > div ${DEFAULT_SELECTOR}`, selector: `div[data-message-author-role] > div ${DEFAULT_SELECTOR}`,
fixerSelector: `div[data-message-author-role='user'] > div`,
fixerFunc: FIXER_BN,
}, },
"forum.ru-board.com": { "forum.ru-board.com": {
selector: `.tit, .dats, .kiss-p, .lgf ${DEFAULT_SELECTOR}`, selector: `.tit, .dats, .kiss-p, .lgf ${DEFAULT_SELECTOR}`,
fixerSelector: `span.post`,
fixerFunc: FIXER_BR,
}, },
"education.github.com": { "education.github.com": {
selector: `${DEFAULT_SELECTOR}, a, summary, span.Button-content`, selector: `${DEFAULT_SELECTOR}, a, summary, span.Button-content`,
}, },
"blogs.windows.com": { "blogs.windows.com": {
selector: `${DEFAULT_SELECTOR}, .c-uhf-nav-link, figcaption`, selector: `${DEFAULT_SELECTOR}, .c-uhf-nav-link, figcaption`,
fixerSelector: `.t-content>div>ul>li`,
fixerFunc: FIXER_BR,
}, },
"developer.apple.com/documentation/": { "developer.apple.com/documentation/": {
selector: `#main ${DEFAULT_SELECTOR}, #main .abstract .content, #main .abstract.content, #main .link span`, selector: `#main ${DEFAULT_SELECTOR}, #main .abstract .content, #main .abstract.content, #main .link span`,
keepSelector: `code, img, svg`, keepSelector: DEFAULT_KEEP_SELECTOR,
}, },
"greasyfork.org": { "greasyfork.org": {
selector: `h2, .script-link, .script-description, #additional-info ${DEFAULT_SELECTOR}`, selector: `h2, .script-link, .script-description, #additional-info ${DEFAULT_SELECTOR}`,
@@ -254,7 +274,7 @@ const RULES_MAP = {
}, },
"pkg.go.dev": { "pkg.go.dev": {
selector: `.Documentation-content ${DEFAULT_SELECTOR}`, selector: `.Documentation-content ${DEFAULT_SELECTOR}`,
keepSelector: `code, img, svg, a, span`, keepSelector: `${DEFAULT_KEEP_SELECTOR}, a, span`,
}, },
"docs.rs": { "docs.rs": {
selector: `.docblock ${DEFAULT_SELECTOR}, .docblock-short`, selector: `.docblock ${DEFAULT_SELECTOR}, .docblock-short`,
@@ -265,12 +285,28 @@ const RULES_MAP = {
}, },
"notebooks.githubusercontent.com/view/ipynb": { "notebooks.githubusercontent.com/view/ipynb": {
selector: `#notebook-container ${DEFAULT_SELECTOR}`, selector: `#notebook-container ${DEFAULT_SELECTOR}`,
keepSelector: `code, img, svg`, keepSelector: DEFAULT_KEEP_SELECTOR,
}, },
"developers.cloudflare.com": { "developers.cloudflare.com": {
selector: `article ${DEFAULT_SELECTOR}, .WorkerStarter--description`, selector: `article ${DEFAULT_SELECTOR}, .WorkerStarter--description`,
keepSelector: `a[rel='noopener'], code`, keepSelector: `a[rel='noopener'], code`,
}, },
"ubuntuforums.org": {
fixerSelector: `.postcontent`,
fixerFunc: FIXER_BR,
},
"play.google.com/store/apps/details": {
fixerSelector: `[data-g-id="description"]`,
fixerFunc: FIXER_BR,
},
"news.yahoo.co.jp/articles/": {
fixerSelector: `.sc-cTsKDU`,
fixerFunc: FIXER_BN,
},
"chromereleases.googleblog.com": {
fixerSelector: `.post-content, .post-content > span, li > span`,
fixerFunc: FIXER_BR,
},
}; };
export const BUILTIN_RULES = Object.entries(RULES_MAP) export const BUILTIN_RULES = Object.entries(RULES_MAP)

View File

@@ -1,11 +1,11 @@
/** /**
* 修复程序类型 * 修复程序类型
*/ */
const FIXER_NONE = "-"; export const FIXER_NONE = "-";
const FIXER_BR = "br"; export const FIXER_BR = "br";
const FIXER_BN = "bn"; export const FIXER_BN = "bn";
const FIXER_BR_DIV = "brToDiv"; export const FIXER_BR_DIV = "brToDiv";
const FIXER_BN_DIV = "bnToDiv"; export const FIXER_BN_DIV = "bnToDiv";
export const FIXER_ALL = [ export const FIXER_ALL = [
FIXER_NONE, FIXER_NONE,
@@ -18,7 +18,7 @@ export const FIXER_ALL = [
/** /**
* 修复过的标记 * 修复过的标记
*/ */
const fixedSign = "kissfixed"; const fixedSign = "kiss-fixed";
/** /**
* 采用 `br` 换行网站的修复函数 * 采用 `br` 换行网站的修复函数
@@ -98,16 +98,6 @@ function bnDivFixer(node) {
return bnFixer(node, "div"); return bnFixer(node, "div");
} }
/**
* 修复程序映射
*/
const fixerMap = {
[FIXER_BR]: brFixer,
[FIXER_BN]: bnFixer,
[FIXER_BR_DIV]: brDivFixer,
[FIXER_BN_DIV]: bnDivFixer,
};
/** /**
* 查找、监听节点,并执行修复函数 * 查找、监听节点,并执行修复函数
* @param {*} selector * @param {*} selector
@@ -143,6 +133,16 @@ function run(selector, fixer, rootSelector) {
}); });
} }
/**
* 修复程序映射
*/
const fixerMap = {
[FIXER_BR]: brFixer,
[FIXER_BN]: bnFixer,
[FIXER_BR_DIV]: brDivFixer,
[FIXER_BN_DIV]: bnDivFixer,
};
/** /**
* 执行fixer * 执行fixer
* @param {*} param0 * @param {*} param0

View File

@@ -5,7 +5,7 @@ import { BUILTIN_RULES } from "./config/rules";
(() => { (() => {
// rules // rules
try { try {
const data = JSON.stringify(BUILTIN_RULES, null, " "); const data = JSON.stringify(BUILTIN_RULES, null, 2);
const file = path.resolve( const file = path.resolve(
__dirname, __dirname,
"../build/web/kiss-translator-rules.json" "../build/web/kiss-translator-rules.json"