feat: Extensive refactoring and modification to support any number of interfaces

This commit is contained in:
Gabe
2025-09-24 23:24:00 +08:00
parent 779c9fc850
commit 2a46939aa5
65 changed files with 2054 additions and 1947 deletions

View File

@@ -26,22 +26,16 @@ export const OPT_TRANS_BAIDU = "Baidu";
export const OPT_TRANS_TENCENT = "Tencent";
export const OPT_TRANS_VOLCENGINE = "Volcengine";
export const OPT_TRANS_OPENAI = "OpenAI";
export const OPT_TRANS_OPENAI_2 = "OpenAI2";
export const OPT_TRANS_OPENAI_3 = "OpenAI3";
export const OPT_TRANS_GEMINI = "Gemini";
export const OPT_TRANS_GEMINI_2 = "Gemini2";
export const OPT_TRANS_CLAUDE = "Claude";
export const OPT_TRANS_CLOUDFLAREAI = "CloudflareAI";
export const OPT_TRANS_OLLAMA = "Ollama";
export const OPT_TRANS_OLLAMA_2 = "Ollama2";
export const OPT_TRANS_OLLAMA_3 = "Ollama3";
export const OPT_TRANS_OPENROUTER = "OpenRouter";
export const OPT_TRANS_CUSTOMIZE = "Custom";
export const OPT_TRANS_CUSTOMIZE_2 = "Custom2";
export const OPT_TRANS_CUSTOMIZE_3 = "Custom3";
export const OPT_TRANS_CUSTOMIZE_4 = "Custom4";
export const OPT_TRANS_CUSTOMIZE_5 = "Custom5";
export const OPT_TRANS_ALL = [
// 内置支持的翻译引擎
export const OPT_ALL_TYPES = [
OPT_TRANS_GOOGLE,
OPT_TRANS_GOOGLE_2,
OPT_TRANS_MICROSOFT,
@@ -53,64 +47,74 @@ export const OPT_TRANS_ALL = [
OPT_TRANS_DEEPLX,
OPT_TRANS_NIUTRANS,
OPT_TRANS_OPENAI,
OPT_TRANS_OPENAI_2,
OPT_TRANS_OPENAI_3,
OPT_TRANS_GEMINI,
OPT_TRANS_GEMINI_2,
OPT_TRANS_CLAUDE,
OPT_TRANS_CLOUDFLAREAI,
OPT_TRANS_OLLAMA,
OPT_TRANS_OLLAMA_2,
OPT_TRANS_OLLAMA_3,
OPT_TRANS_OPENROUTER,
OPT_TRANS_CUSTOMIZE,
OPT_TRANS_CUSTOMIZE_2,
OPT_TRANS_CUSTOMIZE_3,
OPT_TRANS_CUSTOMIZE_4,
OPT_TRANS_CUSTOMIZE_5,
];
// 可使用批处理的翻译引擎
export const OPT_TRANS_BATCH = new Set([
OPT_TRANS_GOOGLE_2,
OPT_TRANS_MICROSOFT,
OPT_TRANS_TENCENT,
OPT_TRANS_DEEPL,
OPT_TRANS_OPENAI,
OPT_TRANS_OPENAI_2,
OPT_TRANS_OPENAI_3,
OPT_TRANS_GEMINI,
OPT_TRANS_GEMINI_2,
OPT_TRANS_CLAUDE,
OPT_TRANS_OLLAMA,
OPT_TRANS_OLLAMA_2,
OPT_TRANS_OLLAMA_3,
OPT_TRANS_OPENROUTER,
OPT_TRANS_CUSTOMIZE,
OPT_TRANS_CUSTOMIZE_2,
OPT_TRANS_CUSTOMIZE_3,
OPT_TRANS_CUSTOMIZE_4,
OPT_TRANS_CUSTOMIZE_5,
]);
// 可使用上下文的翻译引擎
export const OPT_TRANS_CONTEXT = new Set([
OPT_TRANS_OPENAI,
OPT_TRANS_OPENAI_2,
OPT_TRANS_OPENAI_3,
OPT_TRANS_GEMINI,
OPT_TRANS_GEMINI_2,
OPT_TRANS_CLAUDE,
OPT_TRANS_OLLAMA,
OPT_TRANS_OLLAMA_2,
OPT_TRANS_OLLAMA_3,
OPT_TRANS_OPENROUTER,
OPT_TRANS_CUSTOMIZE,
OPT_TRANS_CUSTOMIZE_2,
OPT_TRANS_CUSTOMIZE_3,
OPT_TRANS_CUSTOMIZE_4,
OPT_TRANS_CUSTOMIZE_5,
]);
// 翻译引擎特殊集合
export const API_SPE_TYPES = {
// 内置翻译
builtin: new Set(OPT_ALL_TYPES),
// 机器翻译
machine: new Set([
OPT_TRANS_MICROSOFT,
OPT_TRANS_DEEPLFREE,
OPT_TRANS_BAIDU,
OPT_TRANS_TENCENT,
OPT_TRANS_VOLCENGINE,
]),
// AI翻译
ai: new Set([
OPT_TRANS_OPENAI,
OPT_TRANS_GEMINI,
OPT_TRANS_GEMINI_2,
OPT_TRANS_CLAUDE,
OPT_TRANS_OLLAMA,
OPT_TRANS_OPENROUTER,
]),
// 支持多key
mulkeys: new Set([
OPT_TRANS_DEEPL,
OPT_TRANS_OPENAI,
OPT_TRANS_GEMINI,
OPT_TRANS_GEMINI_2,
OPT_TRANS_CLAUDE,
OPT_TRANS_CLOUDFLAREAI,
OPT_TRANS_OLLAMA,
OPT_TRANS_OPENROUTER,
OPT_TRANS_NIUTRANS,
OPT_TRANS_CUSTOMIZE,
]),
// 支持批处理
batch: new Set([
OPT_TRANS_GOOGLE_2,
OPT_TRANS_MICROSOFT,
OPT_TRANS_TENCENT,
OPT_TRANS_DEEPL,
OPT_TRANS_OPENAI,
OPT_TRANS_GEMINI,
OPT_TRANS_GEMINI_2,
OPT_TRANS_CLAUDE,
OPT_TRANS_OLLAMA,
OPT_TRANS_OPENROUTER,
OPT_TRANS_CUSTOMIZE,
]),
// 支持上下文
context: new Set([
OPT_TRANS_OPENAI,
OPT_TRANS_GEMINI,
OPT_TRANS_GEMINI_2,
OPT_TRANS_CLAUDE,
OPT_TRANS_OLLAMA,
OPT_TRANS_OPENROUTER,
OPT_TRANS_CUSTOMIZE,
]),
};
export const OPT_LANGDETECTOR_ALL = [
OPT_TRANS_GOOGLE,
@@ -119,6 +123,24 @@ export const OPT_LANGDETECTOR_ALL = [
OPT_TRANS_TENCENT,
];
export const BUILTIN_STONES = [
"formal", // 正式风格
"casual", // 口语风格
"neutral", // 中性风格
"technical", // 技术风格
"marketing", // 营销风格
"Literary", // 文学风格
"academic", // 学术风格
"legal", // 法律风格
"literal", // 直译风格
"ldiomatic", // 意译风格
"transcreation", // 创译风格
"machine-like", // 机器风格
"concise", // 简明风格
];
export const BUILTIN_PLACEHOULDERS = ["{ }", "{{ }}", "[ ]", "[[ ]]"];
export const BUILTIN_TAG_NAMES = ["i", "a", "span"];
export const OPT_LANGS_TO = [
["en", "English - English"],
["zh-CN", "Simplified Chinese - 简体中文"],
@@ -250,12 +272,6 @@ export const OPT_LANGS_SPECIAL = {
[OPT_TRANS_OPENAI]: new Map(
OPT_LANGS_FROM.map(([key, val]) => [key, val.split(" - ")[0]])
),
[OPT_TRANS_OPENAI_2]: new Map(
OPT_LANGS_FROM.map(([key, val]) => [key, val.split(" - ")[0]])
),
[OPT_TRANS_OPENAI_3]: new Map(
OPT_LANGS_FROM.map(([key, val]) => [key, val.split(" - ")[0]])
),
[OPT_TRANS_GEMINI]: new Map(
OPT_LANGS_FROM.map(([key, val]) => [key, val.split(" - ")[0]])
),
@@ -268,12 +284,6 @@ export const OPT_LANGS_SPECIAL = {
[OPT_TRANS_OLLAMA]: new Map(
OPT_LANGS_FROM.map(([key, val]) => [key, val.split(" - ")[0]])
),
[OPT_TRANS_OLLAMA_2]: new Map(
OPT_LANGS_FROM.map(([key, val]) => [key, val.split(" - ")[0]])
),
[OPT_TRANS_OLLAMA_3]: new Map(
OPT_LANGS_FROM.map(([key, val]) => [key, val.split(" - ")[0]])
),
[OPT_TRANS_OPENROUTER]: new Map(
OPT_LANGS_FROM.map(([key, val]) => [key, val.split(" - ")[0]])
),
@@ -294,18 +304,6 @@ export const OPT_LANGS_SPECIAL = {
[OPT_TRANS_CUSTOMIZE]: new Map([
...OPT_LANGS_FROM.map(([key]) => [key, key]),
]),
[OPT_TRANS_CUSTOMIZE_2]: new Map([
...OPT_LANGS_FROM.map(([key]) => [key, key]),
]),
[OPT_TRANS_CUSTOMIZE_3]: new Map([
...OPT_LANGS_FROM.map(([key]) => [key, key]),
]),
[OPT_TRANS_CUSTOMIZE_4]: new Map([
...OPT_LANGS_FROM.map(([key]) => [key, key]),
]),
[OPT_TRANS_CUSTOMIZE_5]: new Map([
...OPT_LANGS_FROM.map(([key]) => [key, key]),
]),
};
export const OPT_LANGS_LIST = OPT_LANGS_TO.map(([lang]) => lang);
export const OPT_LANGS_MICROSOFT = new Map(
@@ -328,37 +326,44 @@ export const OPT_LANGS_TENCENT = new Map(
);
OPT_LANGS_TENCENT.set("zh", "zh-CN");
// 翻译接口
const defaultSystemPrompt = `Act as a translation API. Output a single raw JSON object only. No extra text or fences.
Input:
{"targetLanguage":"<lang>","title":"<context>","description":"<context>","segments":[{"id":1,"text":"..."}],"glossary":{"sourceTerm":"targetTerm"},"tone":"<formal|casual>"}
Output:
{"translations":[{"id":1,"text":"...","sourceLanguage":"<detected>"}]}
Rules:
1. Use title/description for context only; do not output them.
2. Keep id, order, and count of segments.
3. Preserve whitespace, HTML entities, and all HTML-like tags (e.g., <i1>, <a1>). Translate inner text only.
4. Highest priority: Follow 'glossary'. Use value for translation; if value is "", keep the key.
5. Do not translate: content in <code>, <pre>, text enclosed in backticks, or placeholders like {1}, {{1}}, [1], [[1]].
6. Apply the specified tone to the translation.
7. Detect sourceLanguage for each segment.
8. Return empty or unchanged inputs as is.
Example:
Input: {"targetLanguage":"zh-CN","segments":[{"id":1,"text":"A <b>React</b> component."}],"glossary":{"component":"组件","React":""}}
Output: {"translations":[{"id":1,"text":"一个<b>React</b>组件","sourceLanguage":"en"}]}
Fail-safe: On any error, return {"translations":[]}.`;
// 翻译接口默认参数
const defaultApi = {
apiSlug: "", // 唯一标识
apiName: "", // 接口名称
apiType: "", // 接口类型
url: "",
key: "",
model: "", // 模型名称
systemPrompt: `You are a translation API.
Output:
- Return one raw JSON object only.
- Start with "{" and end with "}".
- No fences or extra text.
Input JSON:
{"targetLanguage":"<lang>","title":"<title>","description":"<desc>","segments":[{"id":1,"text":"..."}]}
Output JSON:
{"translations":[{"id":1,"text":"...","sourceLanguage":"<detected-language>"}]}
Rules:
1. Use title/description as context only, do not output them.
2. Keep ids/order/count.
3. Translate inner text only, not HTML tags.
4. Do not translate <code>, <pre>, backticks, or terms like React, Docker, JavaScript, API.
5. Preserve whitespace & entities.
6. Automatically detect the source language of each segment and add it in the "sourceLanguage" field.
7. Empty/unchanged input → unchanged.
Fail-safe: {"translations":[]}`,
userPrompt: `${INPUT_PLACE_TEXT}`,
systemPrompt: defaultSystemPrompt,
userPrompt: "",
tone: "neutral", // 翻译风格
placeholder: "{ }", // 占位符todo: 备用)
tagName: "i", // 标签符 todo: 备用)
aiTerms: false, // AI智能专业术语 todo: 备用)
customHeader: "",
customBody: "",
reqHook: "", // request 钩子函数
@@ -370,7 +375,6 @@ Fail-safe: {"translations":[]}`,
batchSize: DEFAULT_BATCH_SIZE, // 每次最多发送段落数量
batchLength: DEFAULT_BATCH_LENGTH, // 每次发送最大文字数量
useBatchFetch: false, // 是否启用聚合发送请求
useRichText: false, // 是否启用富文本翻译
useContext: false, // 是否启用智能上下文
contextSize: DEFAULT_CONTEXT_SIZE, // 智能上下文保留会话数
temperature: 0,
@@ -379,10 +383,97 @@ Fail-safe: {"translations":[]}`,
thinkIgnore: "qwen3,deepseek-r1",
isDisabled: false, // 是否不显示
};
const defaultCustomApi = {
...defaultApi,
url: "https://translate.googleapis.com/translate_a/single?client=gtx&dj=1&dt=t&ie=UTF-8&q={{text}}&sl=en&tl=zh-CN",
reqHook: `// Request Hook
const defaultApiOpts = {
[OPT_TRANS_GOOGLE]: {
...defaultApi,
url: "https://translate.googleapis.com/translate_a/single",
},
[OPT_TRANS_GOOGLE_2]: {
...defaultApi,
url: "https://translate-pa.googleapis.com/v1/translateHtml",
key: "AIzaSyATBXajvzQLTDHEQbcpq0Ihe0vWDHmO520",
useBatchFetch: true,
},
[OPT_TRANS_MICROSOFT]: {
...defaultApi,
useBatchFetch: true,
},
[OPT_TRANS_BAIDU]: {
...defaultApi,
},
[OPT_TRANS_TENCENT]: {
...defaultApi,
useBatchFetch: true,
},
[OPT_TRANS_VOLCENGINE]: {
...defaultApi,
},
[OPT_TRANS_DEEPL]: {
...defaultApi,
url: "https://api-free.deepl.com/v2/translate",
useBatchFetch: true,
},
[OPT_TRANS_DEEPLFREE]: {
...defaultApi,
fetchLimit: 1,
},
[OPT_TRANS_DEEPLX]: {
...defaultApi,
url: "http://localhost:1188/translate",
fetchLimit: 1,
},
[OPT_TRANS_NIUTRANS]: {
...defaultApi,
url: "https://api.niutrans.com/NiuTransServer/translation",
dictNo: "",
memoryNo: "",
},
[OPT_TRANS_OPENAI]: {
...defaultApi,
url: "https://api.openai.com/v1/chat/completions",
model: "gpt-4",
useBatchFetch: true,
fetchLimit: 1,
},
[OPT_TRANS_GEMINI]: {
...defaultApi,
url: `https://generativelanguage.googleapis.com/v1/models/${INPUT_PLACE_MODEL}:generateContent?key=${INPUT_PLACE_KEY}`,
model: "gemini-2.5-flash",
useBatchFetch: true,
},
[OPT_TRANS_GEMINI_2]: {
...defaultApi,
url: `https://generativelanguage.googleapis.com/v1beta/openai/chat/completions`,
model: "gemini-2.0-flash",
useBatchFetch: true,
},
[OPT_TRANS_CLAUDE]: {
...defaultApi,
url: "https://api.anthropic.com/v1/messages",
model: "claude-3-haiku-20240307",
useBatchFetch: true,
},
[OPT_TRANS_CLOUDFLAREAI]: {
...defaultApi,
url: "https://api.cloudflare.com/client/v4/accounts/{{ACCOUNT_ID}}/ai/run/@cf/meta/m2m100-1.2b",
},
[OPT_TRANS_OLLAMA]: {
...defaultApi,
url: "http://localhost:11434/v1/chat/completions",
model: "llama3.1",
useBatchFetch: true,
},
[OPT_TRANS_OPENROUTER]: {
...defaultApi,
url: "https://openrouter.ai/api/v1/chat/completions",
model: "openai/gpt-4o",
useBatchFetch: true,
},
[OPT_TRANS_CUSTOMIZE]: {
...defaultApi,
url: "https://translate.googleapis.com/translate_a/single?client=gtx&dj=1&dt=t&ie=UTF-8&q={{text}}&sl=en&tl=zh-CN",
reqHook: `// Request Hook
(text, from, to, url, key) => [url, {
headers: {
"Content-type": "application/json",
@@ -390,172 +481,18 @@ const defaultCustomApi = {
method: "GET",
body: null,
}]`,
resHook: `// Response Hook
resHook: `// Response Hook
(res, text, from, to) => [res.sentences.map((item) => item.trans).join(" "), to === res.src]`,
};
const defaultOpenaiApi = {
...defaultApi,
url: "https://api.openai.com/v1/chat/completions",
model: "gpt-4",
fetchLimit: 1,
};
const defaultOllamaApi = {
...defaultApi,
url: "http://localhost:11434/v1/chat/completions",
model: "llama3.1",
};
export const DEFAULT_TRANS_APIS = {
[OPT_TRANS_GOOGLE]: {
...defaultApi,
apiSlug: OPT_TRANS_GOOGLE,
apiName: OPT_TRANS_GOOGLE,
url: "https://translate.googleapis.com/translate_a/single",
},
[OPT_TRANS_GOOGLE_2]: {
...defaultApi,
apiSlug: OPT_TRANS_GOOGLE_2,
apiName: OPT_TRANS_GOOGLE_2,
url: "https://translate-pa.googleapis.com/v1/translateHtml",
key: "AIzaSyATBXajvzQLTDHEQbcpq0Ihe0vWDHmO520",
useBatchFetch: true,
},
[OPT_TRANS_MICROSOFT]: {
...defaultApi,
apiSlug: OPT_TRANS_MICROSOFT,
apiName: OPT_TRANS_MICROSOFT,
useBatchFetch: true,
},
[OPT_TRANS_BAIDU]: {
...defaultApi,
apiSlug: OPT_TRANS_BAIDU,
apiName: OPT_TRANS_BAIDU,
},
[OPT_TRANS_TENCENT]: {
...defaultApi,
apiSlug: OPT_TRANS_TENCENT,
apiName: OPT_TRANS_TENCENT,
useBatchFetch: true,
},
[OPT_TRANS_VOLCENGINE]: {
...defaultApi,
apiSlug: OPT_TRANS_VOLCENGINE,
apiName: OPT_TRANS_VOLCENGINE,
},
[OPT_TRANS_DEEPL]: {
...defaultApi,
apiSlug: OPT_TRANS_DEEPL,
apiName: OPT_TRANS_DEEPL,
url: "https://api-free.deepl.com/v2/translate",
useBatchFetch: true,
},
[OPT_TRANS_DEEPLFREE]: {
...defaultApi,
apiSlug: OPT_TRANS_DEEPLFREE,
apiName: OPT_TRANS_DEEPLFREE,
fetchLimit: 1,
},
[OPT_TRANS_DEEPLX]: {
...defaultApi,
apiSlug: OPT_TRANS_DEEPLX,
apiName: OPT_TRANS_DEEPLX,
url: "http://localhost:1188/translate",
fetchLimit: 1,
},
[OPT_TRANS_NIUTRANS]: {
...defaultApi,
apiSlug: OPT_TRANS_NIUTRANS,
apiName: OPT_TRANS_NIUTRANS,
url: "https://api.niutrans.com/NiuTransServer/translation",
dictNo: "",
memoryNo: "",
},
[OPT_TRANS_OPENAI]: {
...defaultOpenaiApi,
apiSlug: OPT_TRANS_OPENAI,
apiName: OPT_TRANS_OPENAI,
},
[OPT_TRANS_OPENAI_2]: {
...defaultOpenaiApi,
apiSlug: OPT_TRANS_OPENAI_2,
apiName: OPT_TRANS_OPENAI_2,
},
[OPT_TRANS_OPENAI_3]: {
...defaultOpenaiApi,
apiSlug: OPT_TRANS_OPENAI_3,
apiName: OPT_TRANS_OPENAI_3,
},
[OPT_TRANS_GEMINI]: {
...defaultApi,
apiSlug: OPT_TRANS_GEMINI,
apiName: OPT_TRANS_GEMINI,
url: `https://generativelanguage.googleapis.com/v1/models/${INPUT_PLACE_MODEL}:generateContent?key=${INPUT_PLACE_KEY}`,
model: "gemini-2.5-flash",
},
[OPT_TRANS_GEMINI_2]: {
...defaultApi,
apiSlug: OPT_TRANS_GEMINI_2,
apiName: OPT_TRANS_GEMINI_2,
url: `https://generativelanguage.googleapis.com/v1beta/openai/chat/completions`,
model: "gemini-2.0-flash",
},
[OPT_TRANS_CLAUDE]: {
...defaultApi,
apiSlug: OPT_TRANS_CLAUDE,
apiName: OPT_TRANS_CLAUDE,
url: "https://api.anthropic.com/v1/messages",
model: "claude-3-haiku-20240307",
},
[OPT_TRANS_CLOUDFLAREAI]: {
...defaultApi,
apiSlug: OPT_TRANS_CLOUDFLAREAI,
apiName: OPT_TRANS_CLOUDFLAREAI,
url: "https://api.cloudflare.com/client/v4/accounts/{{ACCOUNT_ID}}/ai/run/@cf/meta/m2m100-1.2b",
},
[OPT_TRANS_OLLAMA]: {
...defaultOllamaApi,
apiSlug: OPT_TRANS_OLLAMA,
apiName: OPT_TRANS_OLLAMA,
},
[OPT_TRANS_OLLAMA_2]: {
...defaultOllamaApi,
apiSlug: OPT_TRANS_OLLAMA_2,
apiName: OPT_TRANS_OLLAMA_2,
},
[OPT_TRANS_OLLAMA_3]: {
...defaultOllamaApi,
apiSlug: OPT_TRANS_OLLAMA_3,
apiName: OPT_TRANS_OLLAMA_3,
},
[OPT_TRANS_OPENROUTER]: {
...defaultApi,
apiSlug: OPT_TRANS_OPENROUTER,
apiName: "",
url: "https://openrouter.ai/api/v1/chat/completions",
model: "openai/gpt-4o",
},
[OPT_TRANS_CUSTOMIZE]: {
...defaultCustomApi,
apiSlug: OPT_TRANS_CUSTOMIZE,
apiName: OPT_TRANS_CUSTOMIZE,
},
[OPT_TRANS_CUSTOMIZE_2]: {
...defaultCustomApi,
apiSlug: OPT_TRANS_CUSTOMIZE_2,
apiName: OPT_TRANS_CUSTOMIZE_2,
},
[OPT_TRANS_CUSTOMIZE_3]: {
...defaultCustomApi,
apiSlug: OPT_TRANS_CUSTOMIZE_3,
apiName: OPT_TRANS_CUSTOMIZE_3,
},
[OPT_TRANS_CUSTOMIZE_4]: {
...defaultCustomApi,
apiSlug: OPT_TRANS_CUSTOMIZE_4,
apiName: OPT_TRANS_CUSTOMIZE_4,
},
[OPT_TRANS_CUSTOMIZE_5]: {
...defaultCustomApi,
apiSlug: OPT_TRANS_CUSTOMIZE_5,
apiName: OPT_TRANS_CUSTOMIZE_5,
},
};
// 内置翻译接口列表(带参数)
export const DEFAULT_API_LIST = OPT_ALL_TYPES.map((apiType) => ({
...defaultApiOpts[apiType],
apiSlug: apiType,
apiName: apiType,
apiType,
}));
export const DEFAULT_API_TYPE = OPT_TRANS_MICROSOFT;
export const DEFAULT_API_SETTING = DEFAULT_API_LIST[DEFAULT_API_TYPE];

View File

@@ -1342,4 +1342,59 @@ export const I18N = {
en: `Hide Original`,
zh_TW: `隱藏原文`,
},
confirm_title: {
zh: `确认`,
en: `Confirm`,
zh_TW: `確認`,
},
confirm_message: {
zh: `确定操作吗?`,
en: `Are you sure you want to proceed?`,
zh_TW: `確定操作嗎?`,
},
confirm_action: {
zh: `确定`,
en: `Confirm`,
zh_TW: `確定`,
},
cancel_action: {
zh: `取消`,
en: `Cancel`,
zh_TW: `取消`,
},
pls_press_shortcut: {
zh: `请按下快捷键组合`,
en: `Please press the shortcut key combination`,
zh_TW: `請按下快速鍵組合`,
},
load_setting_err: {
zh: `数据加载出错,请刷新页面或卸载后重新安装。`,
en: `Please press the shortcut key combination`,
zh_TW: `請按下快速鍵組合`,
},
translation_style: {
zh: `翻译风格`,
en: `Translation style`,
zh_TW: `翻譯風格`,
},
placeholder: {
zh: `占位符`,
en: `Placeholder`,
zh_TW: `佔位符`,
},
tag_name: {
zh: `占位标签名`,
en: `Placeholder tag name`,
zh_TW: `佔位標名`,
},
ai_terms: {
zh: `AI识别术语表`,
en: `AI Identification Glossary`,
zh_TW: `AI辨識術語表`,
},
system_prompt_helper: {
zh: `在未完全理解默认Prompt的情况下请勿随意修改否则可能翻译失败。`,
en: `If you do not fully understand the default prompt, please do not modify it at will, otherwise the translation may fail.`,
zh_TW: `在未完全理解預設Prompt的情況下請勿隨意修改否則可能翻譯失敗。`,
},
};

View File

@@ -1,4 +1,3 @@
import { FIXER_BR, FIXER_BN, FIXER_BR_DIV, FIXER_BN_DIV } from "../libs/webfix";
import { OPT_TRANS_MICROSOFT } from "./api";
export const GLOBAL_KEY = "*";
@@ -58,6 +57,19 @@ export const OPT_TIMING_ALL = [
OPT_TIMING_ALT,
];
const DEFAULT_DIY_STYLE = `color: #666;
background: linear-gradient(
45deg,
LightGreen 20%,
LightPink 20% 40%,
LightSalmon 40% 60%,
LightSeaGreen 60% 80%,
LightSkyBlue 80%
);
&:hover {
color: #333;
};`;
export const DEFAULT_SELECTOR =
"h1, h2, h3, h4, h5, h6, li, p, dd, blockquote, figcaption, label, legend";
export const DEFAULT_IGNORE_SELECTOR =
@@ -68,13 +80,13 @@ export const DEFAULT_RULE = {
selector: "", // 选择器
keepSelector: "", // 保留元素选择器
terms: "", // 专业术语
translator: GLOBAL_KEY, // 翻译服务
apiSlug: GLOBAL_KEY, // 翻译服务
fromLang: GLOBAL_KEY, // 源语言
toLang: GLOBAL_KEY, // 目标语言
textStyle: GLOBAL_KEY, // 译文样式
transOpen: GLOBAL_KEY, // 开启翻译
bgColor: "", // 译文颜色
textDiyStyle: "", // 自定义译文样式
textDiyStyle: DEFAULT_DIY_STYLE, // 自定义译文样式
selectStyle: "", // 选择器节点样式
parentStyle: "", // 选择器父节点样式
injectJs: "", // 注入JS
@@ -104,7 +116,7 @@ export const GLOBLA_RULE = {
selector: DEFAULT_SELECTOR, // 选择器
keepSelector: DEFAULT_KEEP_SELECTOR, // 保留元素选择器
terms: "", // 专业术语
translator: OPT_TRANS_MICROSOFT, // 翻译服务
apiSlug: OPT_TRANS_MICROSOFT, // 翻译服务
fromLang: "auto", // 源语言
toLang: "zh-CN", // 目标语言
textStyle: OPT_STYLE_NONE, // 译文样式
@@ -136,21 +148,8 @@ export const GLOBLA_RULE = {
export const DEFAULT_RULES = [GLOBLA_RULE];
const DEFAULT_DIY_STYLE = `color: #666;
background: linear-gradient(
45deg,
LightGreen 20%,
LightPink 20% 40%,
LightSalmon 40% 60%,
LightSeaGreen 60% 80%,
LightSkyBlue 80%
);
&:hover {
color: #333;
};`;
export const DEFAULT_OW_RULE = {
translator: REMAIN_KEY,
apiSlug: REMAIN_KEY,
fromLang: REMAIN_KEY,
toLang: REMAIN_KEY,
textStyle: REMAIN_KEY,
@@ -159,258 +158,33 @@ export const DEFAULT_OW_RULE = {
textDiyStyle: DEFAULT_DIY_STYLE,
};
// todo: 校验几个内置规则
const RULES_MAP = {
"www.google.com/search": {
selector: `h3, .IsZvec, .VwiC3b`,
},
"news.google.com": {
selector: `[data-n-tid], ${DEFAULT_SELECTOR}`,
},
"www.foxnews.com": {
selector: `h1, h2, .title, .sidebar [data-type="Title"], .article-content ${DEFAULT_SELECTOR}; [data-spotim-module="conversation"]>div >>> [data-spot-im-class="message-text"] p, [data-spot-im-class="message-text"]`,
},
"bearblog.dev, www.theverge.com, www.tampermonkey.net/documentation.php": {
selector: `${DEFAULT_SELECTOR}`,
},
"themessenger.com": {
selector: `.leading-tight, .leading-tighter, .my-2 p, .font-body p, article ${DEFAULT_SELECTOR}`,
},
"www.telegraph.co.uk, go.dev/doc/": {
selector: `article ${DEFAULT_SELECTOR}`,
},
"www.theguardian.com": {
selector: `.show-underline, .dcr-hup5wm div, .dcr-7vl6y8 div, .dcr-12evv1c, figcaption, article ${DEFAULT_SELECTOR}, [data-cy="mostviewed-footer"] h4`,
},
"www.semafor.com": {
selector: `${DEFAULT_SELECTOR}, .styles_intro__IYj__, [class*="styles_description"]`,
},
"www.noemamag.com": {
selector: `.splash__title, .single-card__title, .single-card__type, .single-card__topic, .highlighted-content__title, .single-card__author, article ${DEFAULT_SELECTOR}, .quote__text, .wp-caption-text div`,
},
"restofworld.org": {
selector: `${DEFAULT_SELECTOR}, .recirc-story__headline, .recirc-story__dek`,
},
"www.axios.com": {
selector: `.h7, ${DEFAULT_SELECTOR}`,
},
"www.newyorker.com": {
selector: `.summary-item__hed, .summary-item__dek, .summary-collection-grid__dek, .dqtvfu, .rubric__link, .caption, article ${DEFAULT_SELECTOR}, .HEhan ${DEFAULT_SELECTOR}, .ContributorBioBio-fBolsO, .BaseText-ewhhUZ`,
},
"time.com": {
selector: `h1, h3, .summary, .video-title, #article-body ${DEFAULT_SELECTOR}, .image-wrap-container .credit.body-caption, .media-heading`,
},
"www.dw.com": {
selector: `.ts-teaser-title a, .news-title a, .title a, .teaser-description a, .hbudab h3, .hbudab p, figcaption ,article ${DEFAULT_SELECTOR}`,
},
"www.bbc.com": {
selector: `h1, h2, .media__link, .media__summary, article ${DEFAULT_SELECTOR}, .ssrcss-y7krbn-Stack, .ssrcss-17zglt8-PromoHeadline, .ssrcss-18cjaf3-Headline, .gs-c-promo-heading__title, .gs-c-promo-summary, .media__content h3, .article__intro, .lx-c-summary-points>li`,
},
"www.chinadaily.com.cn": {
selector: `h1, .tMain [shape="rect"], .cMain [shape="rect"], .photo_art [shape="rect"], .mai_r [shape="rect"], .lisBox li, #Content ${DEFAULT_SELECTOR}`,
},
"www.facebook.com": {
selector: `[role="main"] [dir="auto"]`,
},
"www.reddit.com, new.reddit.com, sh.reddit.com": {
selector: `:is(#AppRouter-main-content, #overlayScrollContainer) :is([class^=tbIA],[class^=_1zP],[class^=ULWj],[class^=_2Jj], [class^=_334],[class^=_2Gr],[class^=_7T4],[class^=_1WO], ${DEFAULT_SELECTOR}); [id^="post-title"], :is([slot="text-body"], [slot="comment"]) ${DEFAULT_SELECTOR}, recent-posts h3, aside :is(span:has(>h2), p); shreddit-subreddit-header >>> :is(#title, #description)`,
},
"www.quora.com": {
selector: `.qu-wordBreak--break-word`,
},
"edition.cnn.com": {
selector: `.container__title, .container__headline, .headline__text, .image__caption, [data-type="Title"], .article__content ${DEFAULT_SELECTOR}`,
},
"www.reuters.com": {
selector: `#main-content [data-testid="Heading"], #main-content [data-testid="Body"], .article-body__content__17Yit ${DEFAULT_SELECTOR}`,
},
"www.bloomberg.com": {
selector: `[data-component="headline"], [data-component="related-item-headline"], [data-component="title"], article ${DEFAULT_SELECTOR}`,
},
"deno.land, docs.github.com": {
selector: `main ${DEFAULT_SELECTOR}`,
keepSelector: DEFAULT_KEEP_SELECTOR,
},
"doc.rust-lang.org": {
selector: `.content ${DEFAULT_SELECTOR}`,
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: DEFAULT_KEEP_SELECTOR,
},
"en.wikipedia.org": {
selector: `h1, .mw-parser-output ${DEFAULT_SELECTOR}`,
keepSelector: `.mwe-math-element`,
},
"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: `${DEFAULT_KEEP_SELECTOR}, .math-container`,
},
"www.npmjs.com/package, developer.chrome.com/docs, medium.com, react.dev, create-react-app.dev, pytorch.org":
{
selector: `article ${DEFAULT_SELECTOR}`,
},
"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: DEFAULT_KEEP_SELECTOR,
},
"twitter.com": {
"twitter.com, https://x.com": {
selector: `[data-testid="tweetText"], [data-testid="birdwatch-pivot"]>div.css-1rynq56`,
keepSelector: `img, a, .r-18u37iz, .css-175oi2r`,
},
"m.youtube.com": {
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}`,
},
"www.bing.com, copilot.microsoft.com": {
selector: `.b_algoSlug, .rwrl_padref; .cib-serp-main >>> .ac-textBlock ${DEFAULT_SELECTOR}, .text-message-content div`,
},
"www.phoronix.com": {
selector: `article ${DEFAULT_SELECTOR}`,
fixerSelector: `.content`,
fixerFunc: FIXER_BR,
},
"wx2.qq.com": {
selector: `.js_message_plain`,
},
"app.slack.com/client/": {
selector: `.p-rich_text_section, .c-message_attachment__text, .p-rich_text_list li`,
},
"discord.com/channels/": {
selector: `div[class^=message], div[class^=headerText], div[class^=name_], section[aria-label='Search Results'] div[id^=message-content], div[id^=message]`,
keepSelector: `li[class^='card'] div[class^='message'], [class^='embedFieldValue'], [data-list-item-id^='forum-channel-list'] div[class^='headerText']`,
},
"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`,
},
"www.instagram.com/p/,www.instagram.com/reels/": {
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: `.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: DEFAULT_KEEP_SELECTOR,
},
"greasyfork.org": {
selector: `h2, .script-link, .script-description, #additional-info ${DEFAULT_SELECTOR}`,
},
"www.fmkorea.com": {
selector: `#container ${DEFAULT_SELECTOR}`,
},
"forum.arduino.cc": {
selector: `.top-row>.title, .featured-topic>.title, .link-top-line>.title, .category-description, .topic-excerpt, .fancy-title, .cooked ${DEFAULT_SELECTOR}`,
},
"docs.arduino.cc": {
selector: `[class^="tutorial-module--left"] ${DEFAULT_SELECTOR}`,
},
"www.historydefined.net": {
selector: `.wp-element-caption, ${DEFAULT_SELECTOR}`,
},
"gobyexample.com": {
selector: `.docs p`,
keepSelector: `code`,
},
"go.dev/tour": {
selector: `#left-side ${DEFAULT_SELECTOR}`,
keepSelector: `code, img, svg >>> code`,
},
"pkg.go.dev": {
selector: `.Documentation-content ${DEFAULT_SELECTOR}`,
keepSelector: `${DEFAULT_KEEP_SELECTOR}, a, span`,
},
"docs.rs": {
selector: `.docblock ${DEFAULT_SELECTOR}, .docblock-short`,
keepSelector: `code >>> code`,
},
"randomnerdtutorials.com": {
selector: `article ${DEFAULT_SELECTOR}`,
},
"notebooks.githubusercontent.com/view/ipynb": {
selector: `#notebook-container ${DEFAULT_SELECTOR}`,
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)

View File

@@ -2,7 +2,7 @@ import {
OPT_DICT_BAIDU,
DEFAULT_HTTP_TIMEOUT,
OPT_TRANS_MICROSOFT,
DEFAULT_TRANS_APIS,
DEFAULT_API_LIST,
} from "./api";
import { DEFAULT_OW_RULE } from "./rules";
@@ -50,7 +50,7 @@ export const OPT_INPUT_TRANS_SIGNS = ["/", "//", "\\", "\\\\", ">", ">>"];
export const DEFAULT_INPUT_SHORTCUT = ["AltLeft", "KeyI"];
export const DEFAULT_INPUT_RULE = {
transOpen: true,
translator: OPT_TRANS_MICROSOFT,
apiSlug: OPT_TRANS_MICROSOFT,
fromLang: "auto",
toLang: "en",
triggerShortcut: DEFAULT_INPUT_SHORTCUT,
@@ -75,7 +75,7 @@ export const OPT_TRANBOX_TRIGGER_ALL = [
export const DEFAULT_TRANBOX_SHORTCUT = ["AltLeft", "KeyS"];
export const DEFAULT_TRANBOX_SETTING = {
// transOpen: true, // 是否启用划词翻译作废移至rule
translator: OPT_TRANS_MICROSOFT,
apiSlug: OPT_TRANS_MICROSOFT,
fromLang: "auto",
toLang: "zh-CN",
toLang2: "en",
@@ -109,17 +109,17 @@ export const DEFAULT_SUBRULES_LIST = [
},
];
export const DEFAULT__MOUSEHOVER_KEY = ["ControlLeft"];
export const DEFAULT_MOUSEHOVER_KEY = ["ControlLeft"];
export const DEFAULT_MOUSE_HOVER_SETTING = {
useMouseHover: true, // 是否启用鼠标悬停翻译
mouseHoverKey: DEFAULT__MOUSEHOVER_KEY, // 鼠标悬停翻译组合键
mouseHoverKey: DEFAULT_MOUSEHOVER_KEY, // 鼠标悬停翻译组合键
};
export const DEFAULT_SETTING = {
darkMode: false, // 深色模式
uiLang: "en", // 界面语言
// fetchLimit: DEFAULT_FETCH_LIMIT, // 最大任务数量(移至transApis,作废)
// fetchInterval: DEFAULT_FETCH_INTERVAL, // 任务间隔时间(移至transApis,作废)
// fetchLimit: DEFAULT_FETCH_LIMIT, // 最大任务数量(移至rule,作废)
// fetchInterval: DEFAULT_FETCH_INTERVAL, // 任务间隔时间(移至rule,作废)
minLength: TRANS_MIN_LENGTH,
maxLength: TRANS_MAX_LENGTH,
newlineLength: TRANS_NEWLINE_LENGTH,
@@ -136,7 +136,7 @@ export const DEFAULT_SETTING = {
// transTitle: false, // 是否同时翻译页面标题(移至rule作废)
subrulesList: DEFAULT_SUBRULES_LIST, // 订阅列表
owSubrule: DEFAULT_OW_RULE, // 覆写订阅规则
transApis: DEFAULT_TRANS_APIS, // 翻译接口
transApis: DEFAULT_API_LIST, // 翻译接口 (v2.0 对象改为数组)
// mouseKey: OPT_TIMING_PAGESCROLL, // 翻译时机/鼠标悬停翻译(移至rule作废)
shortcuts: DEFAULT_SHORTCUTS, // 快捷键
inputRule: DEFAULT_INPUT_RULE, // 输入框设置
@@ -145,7 +145,7 @@ export const DEFAULT_SETTING = {
blacklist: DEFAULT_BLACKLIST.join(",\n"), // 禁用翻译名单
csplist: DEFAULT_CSPLIST.join(",\n"), // 禁用CSP名单
// disableLangs: [], // 不翻译的语言(移至rule作废)
transInterval: 200, // 翻译等待时间
transInterval: 100, // 翻译等待时间
langDetector: OPT_TRANS_MICROSOFT, // 远程语言识别服务
mouseHoverSetting: DEFAULT_MOUSE_HOVER_SETTING, // 鼠标悬停翻译
};