Skip to content

Commit

Permalink
opti code: 优化trans函数架构
Browse files Browse the repository at this point in the history
  • Loading branch information
Bistutu committed May 25, 2024
1 parent f6c8ed4 commit f7c4927
Show file tree
Hide file tree
Showing 14 changed files with 218 additions and 299 deletions.
16 changes: 8 additions & 8 deletions entrypoints/content.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Config} from "./utils/model";
import {cssInject} from "./main/css";
import {handler} from "./main/trans";
import {handleTranslation} from "./main/trans";
import {cache} from "./utils/cache";
import {constants} from "@/entrypoints/utils/constant";

Expand Down Expand Up @@ -34,7 +34,7 @@ export default defineContentScript({
window.addEventListener('keydown', event => {
if (config.hotkey === event.key) {
screen.hotkeyPressed = true;
handler(config, screen.mouseX, screen.mouseY, 25)
handleTranslation(config, screen.mouseX, screen.mouseY)
}
})

Expand All @@ -43,7 +43,7 @@ export default defineContentScript({
screen.mouseX = event.clientX;
screen.mouseY = event.clientY;
if (screen.hotkeyPressed) {
handler(config, screen.mouseX, screen.mouseY, 25, true)
handleTranslation(config, screen.mouseX, screen.mouseY, 50)
}
});

Expand All @@ -52,7 +52,7 @@ export default defineContentScript({
if (event.touches.length === 3) {
let centerX = (event.touches[0].clientX + event.touches[1].clientX + event.touches[2].clientX) / 3;
let centerY = (event.touches[0].clientY + event.touches[1].clientY + event.touches[2].clientY) / 3;
handler(config, centerX, centerY)
handleTranslation(config, centerX, centerY)
}
});

Expand All @@ -62,8 +62,8 @@ export default defineContentScript({
// 通过双击事件获取鼠标位置
let mouseX = event.clientX;
let mouseY = event.clientY;
// 调用 handler 函数进行翻译
handler(config, mouseX, mouseY);
// 调用 handleTranslation 函数进行翻译
handleTranslation(config, mouseX, mouseY);
}
});

Expand All @@ -79,7 +79,7 @@ export default defineContentScript({
timer = setTimeout(() => {
let mouseX = event.clientX;
let mouseY = event.clientY;
handler(config, mouseX, mouseY);
handleTranslation(config, mouseX, mouseY);
}, 500) as unknown as number;
}
});
Expand All @@ -104,7 +104,7 @@ export default defineContentScript({
if (event.button === 1) {
let mouseX = event.clientX;
let mouseY = event.clientY;
handler(config, mouseX, mouseY);
handleTranslation(config, mouseX, mouseY);
}
}
});
Expand Down
39 changes: 0 additions & 39 deletions entrypoints/main/common.ts

This file was deleted.

File renamed without changes.
2 changes: 1 addition & 1 deletion entrypoints/main/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ const css = `
}
/*双语模式,block 强制换行、inline-block 自适应换行*/
.fluent-bilingual-style{
.fluent-read-bilingual{
display: block;
word-break: break-word;
margin: 8px 0 !important;
Expand Down
38 changes: 34 additions & 4 deletions entrypoints/main/dom.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {Config} from "@/entrypoints/utils/model";
import {btnTransThrottle} from "@/entrypoints/main/trans";
import {getMainDomain, selectCompatFn} from "@/entrypoints/main/compatible";
import {getMainDomain, selectCompatFn} from "@/entrypoints/main/compat";
import {html} from 'js-beautify';
import {handleBtnTranslation} from "@/entrypoints/main/trans";

// 当遇到这些 tag 时直接翻译
const directSet = new Set(['h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'p', "li"]);
Expand Down Expand Up @@ -33,12 +34,12 @@ export function grabNode(config: Config, node: any): any {
// 3、button 按钮适配
if (curTag === 'button' || (curTag === 'span' && node.parentNode && node.parentNode.tagName.toLowerCase() === 'button')) {
// 翻译按钮内部而不是整个按钮,避免按钮失去响应式点击事件
if (node.textContent.trim() !== '') btnTransThrottle(config, node)
if (node.textContent.trim() !== '') handleBtnTranslation(config, node)
return false;
}

// 4、特殊适配,根据域名进行特殊处理
let fn = selectCompatFn[getMainDomain(url)];
let fn = selectCompatFn[getMainDomain(url.host)];
if (fn && fn(node)) return node;

// 4、如果遇到 span,则首先该节点就符合翻译条件
Expand Down Expand Up @@ -98,3 +99,32 @@ export function LLMStandardHTML(node: any) {
});
return text;
}

export function beautyHTML(text: string): string {
text = replaceSensitiveWords(text);
return html(text)
}

// 替换 svg 标签中的一些大小写敏感的词(html 不区分大小写,但 svg 标签区分大小写)
function replaceSensitiveWords(text: string): string {
return text.replace(/viewbox|preserveaspectratio|clippathunits|gradienttransform|patterncontentunits|lineargradient|clippath/gi, (match) => {
switch (match.toLowerCase()) {
case 'viewbox':
return 'viewBox';
case 'preserveaspectratio':
return 'preserveAspectRatio';
case 'clippathunits':
return 'clipPathUnits';
case 'gradienttransform':
return 'gradientTransform';
case 'patterncontentunits':
return 'patternContentUnits';
case 'lineargradient':
return 'linearGradient';
case 'clippath':
return 'clipPath';
default:
return match;
}
});
}
Loading

0 comments on commit f7c4927

Please sign in to comment.