From ac921cd5a0a0aefd9d3fe3ce9be144c188e59bc1 Mon Sep 17 00:00:00 2001 From: Gabe Yuan Date: Thu, 21 Mar 2024 11:40:47 +0800 Subject: [PATCH] feat: move fixer to rules --- src/config/rules.js | 60 ++++++++++++++++++++++++++++++++++++--------- src/libs/webfix.js | 32 ++++++++++++------------ src/rules.js | 2 +- 3 files changed, 65 insertions(+), 29 deletions(-) diff --git a/src/config/rules.js b/src/config/rules.js index c04d8c3..fe71be6 100644 --- a/src/config/rules.js +++ b/src/config/rules.js @@ -1,11 +1,11 @@ -export const DEFAULT_SELECTOR = `:is(li, p, h1, h2, h3, h4, h5, h6, dd, blockquote)`; -export const DEFAULT_KEEP_SELECTOR = `code, img, svg`; +import { FIXER_BR, FIXER_BN, FIXER_BR_DIV, FIXER_BN_DIV } from "../libs/webfix"; export const GLOBAL_KEY = "*"; export const REMAIN_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 = { pattern: "", // 匹配网址 selector: "", // 选择器 @@ -124,18 +124,18 @@ const RULES_MAP = { }, "deno.land, docs.github.com": { selector: `main ${DEFAULT_SELECTOR}`, - keepSelector: `code, img, svg`, + keepSelector: DEFAULT_KEEP_SELECTOR, }, "doc.rust-lang.org": { selector: `.content ${DEFAULT_SELECTOR}`, - keepSelector: `code, img, svg`, + keepSelector: DEFAULT_KEEP_SELECTOR, }, "www.indiehackers.com": { selector: `h1, h3, .content ${DEFAULT_SELECTOR}, .feed-item__title-link`, }, "platform.openai.com/docs": { selector: `.docs-body ${DEFAULT_SELECTOR}`, - keepSelector: `code, img, svg`, + keepSelector: DEFAULT_KEEP_SELECTOR, }, "en.wikipedia.org": { 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": { 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": { @@ -152,10 +152,12 @@ const RULES_MAP = { }, "news.ycombinator.com": { selector: `.title, p`, + fixerSelector: `.toptext, .commtext`, + fixerFunc: FIXER_BR, }, "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`, - keepSelector: `code, img, svg`, + keepSelector: DEFAULT_KEEP_SELECTOR, }, "twitter.com": { 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`, selectStyle: `-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": { 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;`, parentStyle: `-webkit-line-clamp: unset; max-height: none; height: auto;`, + keepSelector: `img, #content-text>a`, }, "bard.google.com": { selector: `.query-content ${DEFAULT_SELECTOR}, message-content ${DEFAULT_SELECTOR}`, @@ -179,6 +183,8 @@ const RULES_MAP = { }, "www.phoronix.com": { selector: `article ${DEFAULT_SELECTOR}`, + fixerSelector: `.content`, + fixerFunc: FIXER_BR, }, "wx2.qq.com": { selector: `.js_message_plain`, @@ -192,14 +198,20 @@ const RULES_MAP = { }, "t.me/s/": { selector: `.js-message_text ${DEFAULT_SELECTOR}`, + fixerSelector: `.tgme_widget_message_text`, + fixerFunc: FIXER_BR, }, "web.telegram.org/k": { selector: `div.kiss-p`, 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": { selector: `.text-content > .kiss-p`, keepSelector: `.Reactions, .time, .peer-title, .document-wrapper, .message.spoilers-container custom-emoji-element`, + fixerSelector: `.text-content`, + fixerFunc: FIXER_BR_DIV, }, "www.instagram.com/": { 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']`, }, "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": { selector: `.copyable-text > span`, }, "chat.openai.com": { selector: `div[data-message-author-role] > div ${DEFAULT_SELECTOR}`, + fixerSelector: `div[data-message-author-role='user'] > div`, + fixerFunc: FIXER_BN, }, "forum.ru-board.com": { selector: `.tit, .dats, .kiss-p, .lgf ${DEFAULT_SELECTOR}`, + fixerSelector: `span.post`, + fixerFunc: FIXER_BR, }, "education.github.com": { selector: `${DEFAULT_SELECTOR}, a, summary, span.Button-content`, }, "blogs.windows.com": { selector: `${DEFAULT_SELECTOR}, .c-uhf-nav-link, figcaption`, + fixerSelector: `.t-content>div>ul>li`, + fixerFunc: FIXER_BR, }, "developer.apple.com/documentation/": { selector: `#main ${DEFAULT_SELECTOR}, #main .abstract .content, #main .abstract.content, #main .link span`, - keepSelector: `code, img, svg`, + keepSelector: DEFAULT_KEEP_SELECTOR, }, "greasyfork.org": { selector: `h2, .script-link, .script-description, #additional-info ${DEFAULT_SELECTOR}`, @@ -254,7 +274,7 @@ const RULES_MAP = { }, "pkg.go.dev": { selector: `.Documentation-content ${DEFAULT_SELECTOR}`, - keepSelector: `code, img, svg, a, span`, + keepSelector: `${DEFAULT_KEEP_SELECTOR}, a, span`, }, "docs.rs": { selector: `.docblock ${DEFAULT_SELECTOR}, .docblock-short`, @@ -265,12 +285,28 @@ const RULES_MAP = { }, "notebooks.githubusercontent.com/view/ipynb": { selector: `#notebook-container ${DEFAULT_SELECTOR}`, - keepSelector: `code, img, svg`, + keepSelector: DEFAULT_KEEP_SELECTOR, }, "developers.cloudflare.com": { selector: `article ${DEFAULT_SELECTOR}, .WorkerStarter--description`, 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) diff --git a/src/libs/webfix.js b/src/libs/webfix.js index c354be4..a686786 100644 --- a/src/libs/webfix.js +++ b/src/libs/webfix.js @@ -1,11 +1,11 @@ /** * 修复程序类型 */ -const FIXER_NONE = "-"; -const FIXER_BR = "br"; -const FIXER_BN = "bn"; -const FIXER_BR_DIV = "brToDiv"; -const FIXER_BN_DIV = "bnToDiv"; +export const FIXER_NONE = "-"; +export const FIXER_BR = "br"; +export const FIXER_BN = "bn"; +export const FIXER_BR_DIV = "brToDiv"; +export const FIXER_BN_DIV = "bnToDiv"; export const FIXER_ALL = [ FIXER_NONE, @@ -18,7 +18,7 @@ export const FIXER_ALL = [ /** * 修复过的标记 */ -const fixedSign = "kissfixed"; +const fixedSign = "kiss-fixed"; /** * 采用 `br` 换行网站的修复函数 @@ -98,16 +98,6 @@ function bnDivFixer(node) { return bnFixer(node, "div"); } -/** - * 修复程序映射 - */ -const fixerMap = { - [FIXER_BR]: brFixer, - [FIXER_BN]: bnFixer, - [FIXER_BR_DIV]: brDivFixer, - [FIXER_BN_DIV]: bnDivFixer, -}; - /** * 查找、监听节点,并执行修复函数 * @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 * @param {*} param0 diff --git a/src/rules.js b/src/rules.js index 460684a..884552e 100644 --- a/src/rules.js +++ b/src/rules.js @@ -5,7 +5,7 @@ import { BUILTIN_RULES } from "./config/rules"; (() => { // rules try { - const data = JSON.stringify(BUILTIN_RULES, null, " "); + const data = JSON.stringify(BUILTIN_RULES, null, 2); const file = path.resolve( __dirname, "../build/web/kiss-translator-rules.json"