Compare commits

..

13 Commits

Author SHA1 Message Date
Gabe Yuan
2f5d875c47 v1.8.5 2024-04-02 17:01:34 +08:00
Gabe Yuan
fdb2ddc5f7 fix: rules 2024-04-01 12:35:54 +08:00
Gabe Yuan
7a12c5315a feat: close tranbox when click away 2024-04-01 12:25:59 +08:00
Gabe Yuan
60d788288d feat: add more custom apis 2024-04-01 11:50:29 +08:00
Gabe Yuan
dc3c510d57 fix: update observer callback 2024-03-27 14:24:41 +08:00
Gabe Yuan
ec6a49f01e fix: update readme 2024-03-26 17:50:56 +08:00
Gabe Yuan
2b9bfbc20d feat: csp list 2024-03-26 12:42:39 +08:00
Gabe Yuan
06a51df834 feat: csp list 2024-03-26 12:05:35 +08:00
Gabe Yuan
6fa183dc56 feat: csp list 2024-03-26 12:00:09 +08:00
Gabe Yuan
b3cb4049ed fix: tranbox input onfocus 2024-03-25 22:46:02 +08:00
Gabe Yuan
602b51b1f5 fix: dict audio 2024-03-25 21:00:39 +08:00
Gabe Yuan
a83039577c feat: word pronunciation supported 2024-03-25 18:14:12 +08:00
Gabe Yuan
1c77a289a6 fix: upgrade dependencies 2024-03-21 23:19:15 +08:00
24 changed files with 622 additions and 262 deletions

2
.env
View File

@@ -2,7 +2,7 @@ GENERATE_SOURCEMAP=false
REACT_APP_NAME=KISS Translator REACT_APP_NAME=KISS Translator
REACT_APP_NAME_CN=简约翻译 REACT_APP_NAME_CN=简约翻译
REACT_APP_VERSION=1.8.4 REACT_APP_VERSION=1.8.5
REACT_APP_HOMEPAGE=https://github.com/fishjar/kiss-translator REACT_APP_HOMEPAGE=https://github.com/fishjar/kiss-translator

View File

@@ -9,7 +9,7 @@ A simple, open source [bilingual translation extension & Greasemonkey script](ht
- [x] Keep it simple, smart - [x] Keep it simple, smart
- [x] Open source - [x] Open source
- [x] Adapt to common browsers - [x] Adapt to common browsers
- [x] Chrome/Edge/Firefox/Kiwi - [x] Chrome/Edge/Firefox/Kiwi/Orion
- [ ] Safari - [ ] Safari
- [x] Supports multiple translation services - [x] Supports multiple translation services
- [x] Google/Microsoft/DeepL/OpenAI/Gemini/CloudflareAI/Baidu/Tencent - [x] Google/Microsoft/DeepL/OpenAI/Gemini/CloudflareAI/Baidu/Tencent
@@ -44,13 +44,15 @@ A simple, open source [bilingual translation extension & Greasemonkey script](ht
> - Grease Monkey script will encounter more usage problems (cross domain issues, script conflicts, etc.) > - Grease Monkey script will encounter more usage problems (cross domain issues, script conflicts, etc.)
- [x] Browser extension - [x] Browser extension
- [x] Chrome/Kiwi [Installation address](https://chrome.google.com/webstore/detail/kiss-translator/bdiifdefkgmcblbcghdlonllpjhhjgof?hl=zh-CN) - [x] Chrome [Installation address](https://chrome.google.com/webstore/detail/kiss-translator/bdiifdefkgmcblbcghdlonllpjhhjgof?hl=zh-CN)
- [x] Kiwi (Android)
- [x] Orion (iOS)
- [x] Edge [Installation address](https://microsoftedge.microsoft.com/addons/detail/%E7%AE%80%E7%BA%A6%E7%BF%BB%E8%AF%91/jemckldkclkinpjighnoilpbldbdmmlh?hl=zh-CN) - [x] Edge [Installation address](https://microsoftedge.microsoft.com/addons/detail/%E7%AE%80%E7%BA%A6%E7%BF%BB%E8%AF%91/jemckldkclkinpjighnoilpbldbdmmlh?hl=zh-CN)
- [x] Firefox [Installation address](https://addons.mozilla.org/zh-CN/firefox/addon/kiss-translator/) - [x] Firefox [Installation address](https://addons.mozilla.org/zh-CN/firefox/addon/kiss-translator/)
- [ ] Safari - [ ] Safari
- [x] GreaseMonkey Script - [x] GreaseMonkey Script
- [x] Chrome/Edge/Firefox ([Tampermonkey](https://www.tampermonkey.net/)/[Violentmonkey](https://violentmonkey.github.io/)) [Installation link](https://fishjar.github.io/kiss-translator/kiss-translator.user.js) - [x] Chrome/Edge/Firefox ([Tampermonkey](https://www.tampermonkey.net/)/[Violentmonkey](https://violentmonkey.github.io/)) [Installation link](https://fishjar.github.io/kiss-translator/kiss-translator.user.js)
- Greasy Fork [Installation address](https://greasyfork.org/zh-CN/scripts/472840-kiss-translator) - [Greasy Fork](https://greasyfork.org/zh-CN/scripts/472840-kiss-translator)
- [x] iOS Safari ([Userscripts Safari](https://github.com/quoid/userscripts)) [Installation link](https://fishjar.github.io/kiss-translator/kiss-translator-ios-safari.user.js) - [x] iOS Safari ([Userscripts Safari](https://github.com/quoid/userscripts)) [Installation link](https://fishjar.github.io/kiss-translator/kiss-translator-ios-safari.user.js)
## Associated Projects ## Associated Projects

View File

@@ -9,7 +9,7 @@
- [x] 保持简约 - [x] 保持简约
- [x] 开放源代码 - [x] 开放源代码
- [x] 适配常见浏览器 - [x] 适配常见浏览器
- [x] Chrome/Edge/Firefox/Kiwi - [x] Chrome/Edge/Firefox/Kiwi/Orion
- [ ] Safari - [ ] Safari
- [x] 支持多种翻译服务 - [x] 支持多种翻译服务
- [x] Google/Microsoft/DeepL/OpenAI/Gemini/CloudflareAI/Baidu/Tencent - [x] Google/Microsoft/DeepL/OpenAI/Gemini/CloudflareAI/Baidu/Tencent
@@ -44,13 +44,15 @@
> - 油猴脚本会遇到更多使用上的问题(跨域问题、脚本冲突等) > - 油猴脚本会遇到更多使用上的问题(跨域问题、脚本冲突等)
- [x] 浏览器扩展 - [x] 浏览器扩展
- [x] Chrome/Kiwi [安装地址](https://chrome.google.com/webstore/detail/kiss-translator/bdiifdefkgmcblbcghdlonllpjhhjgof?hl=zh-CN) - [x] Chrome [安装地址](https://chrome.google.com/webstore/detail/kiss-translator/bdiifdefkgmcblbcghdlonllpjhhjgof?hl=zh-CN)
- [x] Kiwi (Android)
- [x] Orion (iOS)
- [x] Edge [安装地址](https://microsoftedge.microsoft.com/addons/detail/%E7%AE%80%E7%BA%A6%E7%BF%BB%E8%AF%91/jemckldkclkinpjighnoilpbldbdmmlh?hl=zh-CN) - [x] Edge [安装地址](https://microsoftedge.microsoft.com/addons/detail/%E7%AE%80%E7%BA%A6%E7%BF%BB%E8%AF%91/jemckldkclkinpjighnoilpbldbdmmlh?hl=zh-CN)
- [x] Firefox [安装地址](https://addons.mozilla.org/zh-CN/firefox/addon/kiss-translator/) - [x] Firefox [安装地址](https://addons.mozilla.org/zh-CN/firefox/addon/kiss-translator/)
- [ ] Safari - [ ] Safari
- [x] 油猴脚本 - [x] 油猴脚本
- [x] Chrome/Edge/Firefox ([Tampermonkey](https://www.tampermonkey.net/)/[Violentmonkey](https://violentmonkey.github.io/)) [安装链接](https://fishjar.github.io/kiss-translator/kiss-translator.user.js) - [x] Chrome/Edge/Firefox ([Tampermonkey](https://www.tampermonkey.net/)/[Violentmonkey](https://violentmonkey.github.io/)) [安装链接](https://fishjar.github.io/kiss-translator/kiss-translator.user.js)
- Greasy Fork [安装地址](https://greasyfork.org/zh-CN/scripts/472840-kiss-translator) - [Greasy Fork](https://greasyfork.org/zh-CN/scripts/472840-kiss-translator)
- [x] iOS Safari ([Userscripts Safari](https://github.com/quoid/userscripts)) [安装链接](https://fishjar.github.io/kiss-translator/kiss-translator-ios-safari.user.js) - [x] iOS Safari ([Userscripts Safari](https://github.com/quoid/userscripts)) [安装链接](https://fishjar.github.io/kiss-translator/kiss-translator-ios-safari.user.js)
## 关联项目 ## 关联项目

View File

@@ -1,20 +1,20 @@
{ {
"name": "kiss-translator", "name": "kiss-translator",
"description": "A minimalist bilingual translation Extension & Greasemonkey Script", "description": "A minimalist bilingual translation Extension & Greasemonkey Script",
"version": "1.8.4", "version": "1.8.5",
"author": "Gabe<yugang2002@gmail.com>", "author": "Gabe<yugang2002@gmail.com>",
"private": true, "private": true,
"dependencies": { "dependencies": {
"@emotion/cache": "^11.11.0", "@emotion/cache": "^11.11.0",
"@emotion/react": "^11.11.1", "@emotion/react": "^11.11.1",
"@emotion/styled": "^11.10.8", "@emotion/styled": "^11.11.0",
"@mui/icons-material": "^5.11.11", "@mui/icons-material": "^5.14.9",
"@mui/material": "^5.11.12", "@mui/material": "^5.14.10",
"query-string": "^8.1.0", "query-string": "^8.1.0",
"react": "^18.2.0", "react": "^18.2.0",
"react-dom": "^18.2.0", "react-dom": "^18.2.0",
"react-markdown": "^8.0.7", "react-markdown": "^8.0.7",
"react-router-dom": "^6.10.0", "react-router-dom": "^6.16.0",
"react-scripts": "5.0.1", "react-scripts": "5.0.1",
"webdav": "^5.3.0", "webdav": "^5.3.0",
"webextension-polyfill": "^0.10.0" "webextension-polyfill": "^0.10.0"
@@ -58,10 +58,10 @@
] ]
}, },
"devDependencies": { "devDependencies": {
"@babel/core": "^7.22.10", "@babel/core": "^7.22.20",
"@babel/node": "^7.22.10", "@babel/node": "^7.22.19",
"@babel/plugin-proposal-private-property-in-object": "^7.21.11", "@babel/plugin-proposal-private-property-in-object": "^7.21.11",
"@babel/preset-env": "^7.22.10", "@babel/preset-env": "^7.22.20",
"react-app-rewired": "^2.2.1" "react-app-rewired": "^2.2.1"
} }
} }

520
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

View File

@@ -2,7 +2,7 @@
"manifest_version": 2, "manifest_version": 2,
"name": "__MSG_app_name__", "name": "__MSG_app_name__",
"description": "__MSG_app_description__", "description": "__MSG_app_description__",
"version": "1.8.4", "version": "1.8.5",
"default_locale": "en", "default_locale": "en",
"author": "Gabe<yugang2002@gmail.com>", "author": "Gabe<yugang2002@gmail.com>",
"homepage_url": "https://github.com/fishjar/kiss-translator", "homepage_url": "https://github.com/fishjar/kiss-translator",
@@ -44,7 +44,13 @@
"description": "__MSG_open_options__" "description": "__MSG_open_options__"
} }
}, },
"permissions": ["<all_urls>", "storage", "contextMenus", "scripting"], "permissions": [
"<all_urls>",
"storage",
"contextMenus",
"scripting",
"declarativeNetRequest"
],
"icons": { "icons": {
"16": "images/logo16.png", "16": "images/logo16.png",
"32": "images/logo32.png", "32": "images/logo32.png",

View File

@@ -2,7 +2,7 @@
"manifest_version": 3, "manifest_version": 3,
"name": "__MSG_app_name__", "name": "__MSG_app_name__",
"description": "__MSG_app_description__", "description": "__MSG_app_description__",
"version": "1.8.4", "version": "1.8.5",
"default_locale": "en", "default_locale": "en",
"author": "Gabe<yugang2002@gmail.com>", "author": "Gabe<yugang2002@gmail.com>",
"homepage_url": "https://github.com/fishjar/kiss-translator", "homepage_url": "https://github.com/fishjar/kiss-translator",
@@ -45,7 +45,12 @@
"description": "__MSG_open_options__" "description": "__MSG_open_options__"
} }
}, },
"permissions": ["storage", "contextMenus", "scripting"], "permissions": [
"storage",
"contextMenus",
"scripting",
"declarativeNetRequest"
],
"host_permissions": ["<all_urls>"], "host_permissions": ["<all_urls>"],
"icons": { "icons": {
"16": "images/logo16.png", "16": "images/logo16.png",

View File

@@ -16,6 +16,7 @@ import {
KV_SALT_SYNC, KV_SALT_SYNC,
URL_BAIDU_LANGDETECT, URL_BAIDU_LANGDETECT,
URL_BAIDU_SUGGEST, URL_BAIDU_SUGGEST,
URL_BAIDU_TTS,
OPT_LANGS_BAIDU, OPT_LANGS_BAIDU,
URL_TENCENT_TRANSMART, URL_TENCENT_TRANSMART,
OPT_LANGS_TENCENT, OPT_LANGS_TENCENT,
@@ -95,6 +96,20 @@ export const apiBaiduSuggest = async (text) => {
return []; return [];
}; };
/**
* 百度语音
* @param {*} text
* @param {*} lan
* @param {*} spd
* @returns
*/
export const apiBaiduTTS = (text, lan = "uk", spd = 3) => {
const url = `${URL_BAIDU_TTS}?${queryString.stringify({ lan, text, spd })}`;
return fetchPolyfill(url, {
useCache: false, // 为避免缓存过快增长,禁用缓存语音数据
});
};
/** /**
* 腾讯语言识别 * 腾讯语言识别
* @param {*} text * @param {*} text

View File

@@ -12,6 +12,8 @@ import {
MSG_COMMAND_SHORTCUTS, MSG_COMMAND_SHORTCUTS,
MSG_INJECT_JS, MSG_INJECT_JS,
MSG_INJECT_CSS, MSG_INJECT_CSS,
MSG_UPDATE_CSP,
DEFAULT_CSPLIST,
CMD_TOGGLE_TRANSLATE, CMD_TOGGLE_TRANSLATE,
CMD_TOGGLE_STYLE, CMD_TOGGLE_STYLE,
CMD_OPEN_OPTIONS, CMD_OPEN_OPTIONS,
@@ -26,9 +28,17 @@ import { tryClearCaches } from "./libs";
import { saveRule } from "./libs/rules"; import { saveRule } from "./libs/rules";
import { getCurTabId } from "./libs/msg"; import { getCurTabId } from "./libs/msg";
import { injectInlineJs, injectInternalCss } from "./libs/injector"; import { injectInlineJs, injectInternalCss } from "./libs/injector";
import { kissLog } from "./libs/log";
globalThis.ContextType = "BACKGROUND"; globalThis.ContextType = "BACKGROUND";
const REMOVE_HEADERS = [
`content-security-policy`,
`content-security-policy-report-only`,
`x-webkit-csp`,
`x-content-security-policy`,
];
/** /**
* 添加右键菜单 * 添加右键菜单
*/ */
@@ -79,6 +89,41 @@ async function addContextMenus(contextMenuType = 1) {
} }
} }
/**
* 更新CSP策略
* @param {*} csplist
*/
async function updateCspRules(csplist = DEFAULT_CSPLIST.join(",\n")) {
try {
const newRules = csplist
.split(/\n|,/)
.map((url) => url.trim())
.filter(Boolean)
.map((url, idx) => ({
id: idx + 1,
action: {
type: "modifyHeaders",
responseHeaders: REMOVE_HEADERS.map((header) => ({
operation: "remove",
header,
})),
},
condition: {
urlFilter: url,
resourceTypes: ["main_frame", "sub_frame"],
},
}));
const oldRules = await browser.declarativeNetRequest.getDynamicRules();
const oldRuleIds = oldRules.map((rule) => rule.id);
await browser.declarativeNetRequest.updateDynamicRules({
removeRuleIds: oldRuleIds,
addRules: newRules,
});
} catch (err) {
kissLog(err, "update csp rules");
}
}
/** /**
* 插件安装 * 插件安装
*/ */
@@ -87,6 +132,9 @@ browser.runtime.onInstalled.addListener(() => {
// 右键菜单 // 右键菜单
addContextMenus(); addContextMenus();
// 禁用CSP
updateCspRules();
}); });
/** /**
@@ -96,7 +144,7 @@ browser.runtime.onStartup.addListener(async () => {
// 同步数据 // 同步数据
await trySyncSettingAndRules(); await trySyncSettingAndRules();
const { clearCache, contextMenuType, subrulesList } = const { clearCache, contextMenuType, subrulesList, csplist } =
await getSettingWithDefault(); await getSettingWithDefault();
// 清除缓存 // 清除缓存
@@ -108,6 +156,9 @@ browser.runtime.onStartup.addListener(async () => {
// firefox重启后菜单会消失,故重复添加 // firefox重启后菜单会消失,故重复添加
addContextMenus(contextMenuType); addContextMenus(contextMenuType);
// 禁用CSP
updateCspRules(csplist);
// 同步订阅规则 // 同步订阅规则
trySyncAllSubRules({ subrulesList }); trySyncAllSubRules({ subrulesList });
}); });
@@ -143,8 +194,10 @@ browser.runtime.onMessage.addListener(async ({ action, args }) => {
args: [args], args: [args],
world: "MAIN", world: "MAIN",
}); });
case MSG_UPDATE_CSP:
return await updateCspRules(args);
case MSG_CONTEXT_MENUS: case MSG_CONTEXT_MENUS:
return await addContextMenus(args.contextMenuType); return await addContextMenus(args);
case MSG_COMMAND_SHORTCUTS: case MSG_COMMAND_SHORTCUTS:
return await browser.commands.getAll(); return await browser.commands.getAll();
default: default:

View File

@@ -623,6 +623,10 @@ export const I18N = {
zh: `隐藏翻译按钮`, zh: `隐藏翻译按钮`,
en: `Hide Translate Button`, en: `Hide Translate Button`,
}, },
hide_click_away: {
zh: `点击外部关闭弹窗`,
en: `Click outside to close the pop-up window`,
},
show: { show: {
zh: `显示`, zh: `显示`,
en: `Show`, en: `Show`,
@@ -755,6 +759,14 @@ export const I18N = {
zh: `禁用翻译名单`, zh: `禁用翻译名单`,
en: `Translate Blacklist`, en: `Translate Blacklist`,
}, },
disabled_csplist: {
zh: `禁用CSP名单`,
en: `Disabled CSP List`,
},
disabled_csplist_helper: {
zh: `3、通过调整CSP策略使得某些页面能够注入JS/CSS/Media请谨慎使用除非您已知晓相关风险。`,
en: `3. By adjusting the CSP policy, some pages can inject JS/CSS/Media. Please use it with caution unless you are aware of the related risks.`,
},
skip_langs: { skip_langs: {
zh: `不翻译的语言`, zh: `不翻译的语言`,
en: `Disable Languages`, en: `Disable Languages`,

View File

@@ -65,6 +65,7 @@ export const MSG_CONTEXT_MENUS = "context_menus";
export const MSG_COMMAND_SHORTCUTS = "command_shortcuts"; export const MSG_COMMAND_SHORTCUTS = "command_shortcuts";
export const MSG_INJECT_JS = "inject_js"; export const MSG_INJECT_JS = "inject_js";
export const MSG_INJECT_CSS = "inject_css"; export const MSG_INJECT_CSS = "inject_css";
export const MSG_UPDATE_CSP = "update_csp";
export const THEME_LIGHT = "light"; export const THEME_LIGHT = "light";
export const THEME_DARK = "dark"; export const THEME_DARK = "dark";
@@ -83,6 +84,7 @@ export const URL_MICROSOFT_TRAN =
export const URL_MICROSOFT_AUTH = "https://edge.microsoft.com/translate/auth"; export const URL_MICROSOFT_AUTH = "https://edge.microsoft.com/translate/auth";
export const URL_BAIDU_LANGDETECT = "https://fanyi.baidu.com/langdetect"; export const URL_BAIDU_LANGDETECT = "https://fanyi.baidu.com/langdetect";
export const URL_BAIDU_SUGGEST = "https://fanyi.baidu.com/sug"; export const URL_BAIDU_SUGGEST = "https://fanyi.baidu.com/sug";
export const URL_BAIDU_TTS = "https://fanyi.baidu.com/gettts";
export const URL_BAIDU_WEB = "https://fanyi.baidu.com/"; export const URL_BAIDU_WEB = "https://fanyi.baidu.com/";
export const URL_BAIDU_TRANSAPI = "https://fanyi.baidu.com/transapi"; export const URL_BAIDU_TRANSAPI = "https://fanyi.baidu.com/transapi";
export const URL_BAIDU_TRANSAPI_V2 = "https://fanyi.baidu.com/v2transapi"; export const URL_BAIDU_TRANSAPI_V2 = "https://fanyi.baidu.com/v2transapi";
@@ -100,6 +102,10 @@ export const OPT_TRANS_OPENAI = "OpenAI";
export const OPT_TRANS_GEMINI = "Gemini"; export const OPT_TRANS_GEMINI = "Gemini";
export const OPT_TRANS_CLOUDFLAREAI = "CloudflareAI"; export const OPT_TRANS_CLOUDFLAREAI = "CloudflareAI";
export const OPT_TRANS_CUSTOMIZE = "Custom"; 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_TRANS_ALL = [
OPT_TRANS_GOOGLE, OPT_TRANS_GOOGLE,
OPT_TRANS_MICROSOFT, OPT_TRANS_MICROSOFT,
@@ -112,6 +118,10 @@ export const OPT_TRANS_ALL = [
OPT_TRANS_GEMINI, OPT_TRANS_GEMINI,
OPT_TRANS_CLOUDFLAREAI, OPT_TRANS_CLOUDFLAREAI,
OPT_TRANS_CUSTOMIZE, OPT_TRANS_CUSTOMIZE,
OPT_TRANS_CUSTOMIZE_2,
OPT_TRANS_CUSTOMIZE_3,
OPT_TRANS_CUSTOMIZE_4,
OPT_TRANS_CUSTOMIZE_5,
]; ];
export const OPT_LANGS_TO = [ export const OPT_LANGS_TO = [
@@ -378,7 +388,8 @@ export const DEFAULT_TRANBOX_SETTING = {
tranboxShortcut: DEFAULT_TRANBOX_SHORTCUT, tranboxShortcut: DEFAULT_TRANBOX_SHORTCUT,
btnOffsetX: 10, btnOffsetX: 10,
btnOffsetY: 10, btnOffsetY: 10,
hideTranBtn: false, hideTranBtn: false, // 是否隐藏翻译按钮
hideClickAway: false, // 是否点击外部关闭弹窗
}; };
// 订阅列表 // 订阅列表
@@ -398,6 +409,12 @@ export const DEFAULT_SUBRULES_LIST = [
]; ];
// 翻译接口 // 翻译接口
const defaultCustomApi = {
url: "",
key: "",
fetchLimit: DEFAULT_FETCH_LIMIT,
fetchInterval: DEFAULT_FETCH_INTERVAL,
};
export const DEFAULT_TRANS_APIS = { export const DEFAULT_TRANS_APIS = {
[OPT_TRANS_GOOGLE]: { [OPT_TRANS_GOOGLE]: {
url: "https://translate.googleapis.com/translate_a/single", url: "https://translate.googleapis.com/translate_a/single",
@@ -455,12 +472,11 @@ export const DEFAULT_TRANS_APIS = {
fetchLimit: 1, fetchLimit: 1,
fetchInterval: 500, fetchInterval: 500,
}, },
[OPT_TRANS_CUSTOMIZE]: { [OPT_TRANS_CUSTOMIZE]: defaultCustomApi,
url: "", [OPT_TRANS_CUSTOMIZE_2]: defaultCustomApi,
key: "", [OPT_TRANS_CUSTOMIZE_3]: defaultCustomApi,
fetchLimit: DEFAULT_FETCH_LIMIT, [OPT_TRANS_CUSTOMIZE_4]: defaultCustomApi,
fetchInterval: DEFAULT_FETCH_INTERVAL, [OPT_TRANS_CUSTOMIZE_5]: defaultCustomApi,
},
}; };
// 默认快捷键 // 默认快捷键
@@ -485,6 +501,7 @@ export const DEFAULT_BLACKLIST = [
"oapi.dingtalk.com", "oapi.dingtalk.com",
"login.dingtalk.com", "login.dingtalk.com",
]; // 禁用翻译名单 ]; // 禁用翻译名单
export const DEFAULT_CSPLIST = ["https://github.com"]; // 禁用CSP名单
export const DEFAULT_SETTING = { export const DEFAULT_SETTING = {
darkMode: false, // 深色模式 darkMode: false, // 深色模式
@@ -512,6 +529,7 @@ export const DEFAULT_SETTING = {
tranboxSetting: DEFAULT_TRANBOX_SETTING, // 划词翻译设置 tranboxSetting: DEFAULT_TRANBOX_SETTING, // 划词翻译设置
touchTranslate: 2, // 触屏翻译 touchTranslate: 2, // 触屏翻译
blacklist: DEFAULT_BLACKLIST.join(",\n"), // 禁用翻译名单 blacklist: DEFAULT_BLACKLIST.join(",\n"), // 禁用翻译名单
csplist: DEFAULT_CSPLIST.join(",\n"), // 禁用CSP名单
// disableLangs: [], // 不翻译的语言(移至rule作废) // disableLangs: [], // 不翻译的语言(移至rule作废)
transInterval: 500, // 翻译间隔时间 transInterval: 500, // 翻译间隔时间
}; };

View File

@@ -4,8 +4,8 @@ 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_SELECTOR = `:is(li, p, h1, h2, h3, h4, h5, h6, dd, blockquote, .kiss-p)`;
export const DEFAULT_KEEP_SELECTOR = `code, img, svg`; export const DEFAULT_KEEP_SELECTOR = `code, img, svg, pre`;
export const DEFAULT_RULE = { export const DEFAULT_RULE = {
pattern: "", // 匹配网址 pattern: "", // 匹配网址
selector: "", // 选择器 selector: "", // 选择器

61
src/hooks/Audio.js Normal file
View File

@@ -0,0 +1,61 @@
import { useCallback, useEffect, useRef, useState } from "react";
import { apiBaiduTTS } from "../apis";
import { kissLog } from "../libs/log";
/**
* 声音播放hook
* @param {*} src
* @returns
*/
export function useAudio(src) {
const audioRef = useRef(null);
const [error, setError] = useState(null);
const [ready, setReady] = useState(false);
const [playing, setPlaying] = useState(false);
const onPlay = useCallback(() => {
audioRef.current?.play();
}, []);
useEffect(() => {
if (!src) {
return;
}
const audio = new Audio(src);
audio.addEventListener("error", (err) => setError(err));
audio.addEventListener("canplaythrough", () => setReady(true));
audio.addEventListener("play", () => setPlaying(true));
audio.addEventListener("ended", () => setPlaying(false));
audioRef.current = audio;
}, [src]);
return {
error,
ready,
playing,
onPlay,
};
}
/**
* 获取语音hook
* @param {*} text
* @param {*} lan
* @param {*} spd
* @returns
*/
export function useTextAudio(text, lan = "uk", spd = 3) {
const [src, setSrc] = useState("");
useEffect(() => {
(async () => {
try {
setSrc(await apiBaiduTTS(text, lan, spd));
} catch (err) {
kissLog(err, "baidu tts");
}
})();
}, [text, lan, spd]);
return useAudio(src);
}

View File

@@ -12,6 +12,7 @@ import {
import { isBg } from "./browser"; import { isBg } from "./browser";
import { newCacheReq, newTransReq } from "./req"; import { newCacheReq, newTransReq } from "./req";
import { kissLog } from "./log"; import { kissLog } from "./log";
import { blobToBase64 } from "./utils";
const TIMEOUT = 5000; const TIMEOUT = 5000;
@@ -163,6 +164,9 @@ export const fetchData = async (
const contentType = res.headers.get("Content-Type"); const contentType = res.headers.get("Content-Type");
if (contentType?.includes("json")) { if (contentType?.includes("json")) {
return await res.json(); return await res.json();
} else if (contentType?.includes("audio")) {
const blob = await res.blob();
return await blobToBase64(blob);
} }
return await res.text(); return await res.text();
}; };

View File

@@ -58,11 +58,11 @@ export class Translator {
// 显示 // 显示
_interseObserver = new IntersectionObserver( _interseObserver = new IntersectionObserver(
(intersections) => { (entries, observer) => {
intersections.forEach((intersection) => { entries.forEach((entry) => {
if (intersection.isIntersecting) { if (entry.isIntersecting) {
this._render(intersection.target); observer.unobserve(entry.target);
this._interseObserver.unobserve(intersection.target); this._render(entry.target);
} }
}); });
}, },

View File

@@ -233,3 +233,16 @@ export const isValidWord = (str) => {
const regex = /^[a-zA-Z-]+$/; const regex = /^[a-zA-Z-]+$/;
return regex.test(str); return regex.test(str);
}; };
/**
* blob转为base64
* @param {*} blob
* @returns
*/
export const blobToBase64 = (blob) => {
return new Promise((resolve) => {
const reader = new FileReader();
reader.onloadend = () => resolve(reader.result);
reader.readAsDataURL(blob);
});
};

View File

@@ -208,7 +208,7 @@ function ApiFields({ translator }) {
</Button> </Button>
</Stack> </Stack>
{translator === OPT_TRANS_CUSTOMIZE && ( {translator.startsWith(OPT_TRANS_CUSTOMIZE) && (
<pre>{i18n("custom_api_help")}</pre> <pre>{i18n("custom_api_help")}</pre>
)} )}
</Stack> </Stack>

View File

@@ -22,7 +22,9 @@ import {
OPT_SHORTCUT_POPUP, OPT_SHORTCUT_POPUP,
OPT_SHORTCUT_SETTING, OPT_SHORTCUT_SETTING,
DEFAULT_BLACKLIST, DEFAULT_BLACKLIST,
DEFAULT_CSPLIST,
MSG_CONTEXT_MENUS, MSG_CONTEXT_MENUS,
MSG_UPDATE_CSP,
} from "../../config"; } from "../../config";
import { useShortcut } from "../../hooks/Shortcut"; import { useShortcut } from "../../hooks/Shortcut";
import ShortcutInput from "./ShortcutInput"; import ShortcutInput from "./ShortcutInput";
@@ -69,7 +71,10 @@ export default function Settings() {
value = limitNumber(value, 0, 4); value = limitNumber(value, 0, 4);
break; break;
case "contextMenuType": case "contextMenuType":
isExt && sendBgMsg(MSG_CONTEXT_MENUS, { contextMenuType: value }); isExt && sendBgMsg(MSG_CONTEXT_MENUS, value);
break;
case "csplist":
isExt && sendBgMsg(MSG_UPDATE_CSP, value);
break; break;
default: default:
} }
@@ -96,6 +101,7 @@ export default function Settings() {
contextMenuType = 1, contextMenuType = 1,
touchTranslate = 2, touchTranslate = 2,
blacklist = DEFAULT_BLACKLIST.join(",\n"), blacklist = DEFAULT_BLACKLIST.join(",\n"),
csplist = DEFAULT_CSPLIST.join(",\n"),
transInterval = 500, transInterval = 500,
} = setting; } = setting;
const { isHide = false } = fab || {}; const { isHide = false } = fab || {};
@@ -219,6 +225,18 @@ export default function Settings() {
</Link> </Link>
</FormHelperText> </FormHelperText>
</FormControl> </FormControl>
<TextField
size="small"
label={i18n("disabled_csplist")}
helperText={
i18n("pattern_helper") + " " + i18n("disabled_csplist_helper")
}
name="csplist"
defaultValue={csplist}
onChange={handleChange}
multiline
/>
</> </>
) : ( ) : (
<> <>

View File

@@ -50,6 +50,7 @@ export default function Tranbox() {
btnOffsetX, btnOffsetX,
btnOffsetY, btnOffsetY,
hideTranBtn = false, hideTranBtn = false,
hideClickAway = false,
} = tranboxSetting; } = tranboxSetting;
return ( return (
@@ -160,6 +161,18 @@ export default function Tranbox() {
<MenuItem value={true}>{i18n("hide")}</MenuItem> <MenuItem value={true}>{i18n("hide")}</MenuItem>
</TextField> </TextField>
<TextField
select
size="small"
name="hideClickAway"
value={hideClickAway}
label={i18n("hide_click_away")}
onChange={handleChange}
>
<MenuItem value={false}>{i18n("disable")}</MenuItem>
<MenuItem value={true}>{i18n("enable")}</MenuItem>
</TextField>
{!isExt && ( {!isExt && (
<ShortcutInput <ShortcutInput
value={tranboxShortcut} value={tranboxShortcut}

View File

@@ -0,0 +1,29 @@
import IconButton from "@mui/material/IconButton";
import VolumeUpIcon from "@mui/icons-material/VolumeUp";
import { useTextAudio } from "../../hooks/Audio";
export default function AudioBtn({ text, lan = "uk" }) {
const { error, ready, playing, onPlay } = useTextAudio(text, lan);
if (error || !ready) {
return (
<IconButton disabled>
<VolumeUpIcon />
</IconButton>
);
}
if (playing) {
return (
<IconButton color="primary">
<VolumeUpIcon />
</IconButton>
);
}
return (
<IconButton onClick={onPlay}>
<VolumeUpIcon />
</IconButton>
);
}

View File

@@ -2,10 +2,11 @@ import Box from "@mui/material/Box";
import Stack from "@mui/material/Stack"; import Stack from "@mui/material/Stack";
import FavBtn from "./FavBtn"; import FavBtn from "./FavBtn";
import Typography from "@mui/material/Typography"; import Typography from "@mui/material/Typography";
import AudioBtn from "./AudioBtn";
const phonicMap = { const phonicMap = {
en_phonic: "英", en_phonic: ["英", "uk"],
us_phonic: "美", us_phonic: ["美", "en"],
}; };
export default function DictCont({ dictResult }) { export default function DictCont({ dictResult }) {
@@ -27,13 +28,17 @@ export default function DictCont({ dictResult }) {
</Stack> </Stack>
<Typography component="div"> <Typography component="div">
<Typography> <Stack direction="row">
{dictResult.voice {dictResult.voice
?.map(Object.entries) ?.map(Object.entries)
.map((item) => item[0]) .map((item) => item[0])
.map(([key, val]) => `${phonicMap[key] || key} ${val}`) .map(([key, val]) => (
.join(" ")} <span>
</Typography> <span>{`${phonicMap[key]?.[0] || key} ${val}`}</span>
<AudioBtn text={dictResult.src} lan={phonicMap[key]?.[1]} />
</span>
))}
</Stack>
<ul style={{ margin: "0.5em 0" }}> <ul style={{ margin: "0.5em 0" }}>
{dictResult.content[0].mean.map(({ pre, cont }, idx) => ( {dictResult.content[0].mean.map(({ pre, cont }, idx) => (
<li key={idx}> <li key={idx}>

View File

@@ -148,6 +148,7 @@ export default function DraggableResizable({
}, },
onChangeSize, onChangeSize,
onChangePosition, onChangePosition,
...props
}) { }) {
const lineWidth = 4; const lineWidth = 4;
const [position, setPosition] = useState(defaultPosition); const [position, setPosition] = useState(defaultPosition);
@@ -182,6 +183,7 @@ export default function DraggableResizable({
gridTemplateRows: `${lineWidth * 2}px auto ${lineWidth * 2}px`, gridTemplateRows: `${lineWidth * 2}px auto ${lineWidth * 2}px`,
zIndex: 2147483647, zIndex: 2147483647,
}} }}
{...props}
> >
<Pointer <Pointer
direction="TopLeft" direction="TopLeft"

View File

@@ -102,17 +102,12 @@ function TranForm({ text, setText, tranboxSetting, transApis }) {
fullWidth fullWidth
multiline multiline
value={editMode ? editText : text} value={editMode ? editText : text}
disabled={!editMode}
onChange={(e) => { onChange={(e) => {
setEditText(e.target.value); setEditText(e.target.value);
}} }}
onClick={() => { onFocus={() => {
setEditMode(true); setEditMode(true);
setEditText(text); setEditText(text);
const timer = setTimeout(() => {
clearTimeout(timer);
inputRef.current?.focus();
}, 100);
}} }}
onBlur={() => { onBlur={() => {
setEditMode(false); setEditMode(false);
@@ -180,6 +175,7 @@ export default function TranBox({
header={<Header setShowPopup={setShowBox} />} header={<Header setShowPopup={setShowBox} />}
onChangeSize={setBoxSize} onChangeSize={setBoxSize}
onChangePosition={setBoxPosition} onChangePosition={setBoxPosition}
onClick={(e) => e.stopPropagation()}
> >
<Divider /> <Divider />
<TranForm <TranForm

View File

@@ -132,6 +132,18 @@ export default function Slection({
} }
}, [handleTranbox, contextMenuType]); }, [handleTranbox, contextMenuType]);
useEffect(() => {
if (tranboxSetting.hideClickAway) {
const handleHideBox = () => {
setShowBox(false);
};
window.addEventListener("click", handleHideBox);
return () => {
window.removeEventListener("click", handleHideBox);
};
}
}, [tranboxSetting.hideClickAway]);
return ( return (
<> <>
{showBox && ( {showBox && (