Skip to content

Commit

Permalink
fixbug: 修复百度翻译失败问题
Browse files Browse the repository at this point in the history
  • Loading branch information
Bistutu committed May 18, 2024
1 parent 98b2b0c commit 66c931f
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,12 @@ export const selectCompatFn: SelectCompatFn = {
["mvnrepository.com"]: (node: any) => {
if (node.tagName.toLowerCase() === 'div' && node.classList.contains('im-description')) return true
},
["www.aozora.gr.jp"]: (node: any) => {
["aozora.gr.jp"]: (node: any) => {
if (node.tagName.toLowerCase() === 'div' && node.classList.contains('main_text')) return true
},
["youtube.com"]: (node: any) => {
if (node.tagName.toLowerCase() === 'yt-formatted-string') return true
},
['www.webtrees.net']: (node: any) => {
// class='kmsg'
if (node.tagName.toLowerCase() === 'div' && node.classList.contains('kmsg')) return true
},
['webtrees.net']: (node: any) => {
// class='kmsg'
if (node.tagName.toLowerCase() === 'div' && node.classList.contains('kmsg')) return true
Expand Down
20 changes: 11 additions & 9 deletions entrypoints/main/dom.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {checkConfig, skipNode} from "./check";
import {Config} from "../utils/model";
import {getMainDomain, replaceCompatFn, selectCompatFn} from "../compat/compat";
import {getMainDomain, replaceCompatFn, selectCompatFn} from "./compatible";
import {cache} from "../utils/cache";
import {detectlang} from "./detectlang";
import {services} from "../utils/option";
Expand Down Expand Up @@ -38,6 +38,8 @@ export function handler(config: Config, mouseX: number, mouseY: number, time: nu
// 获取起始节点
let node = grabNode(config, document.elementFromPoint(mouseX, mouseY)); // 获取最终需要翻译的节点

// console.log("翻译节点:", node);

// 跳过不需要翻译的节点
if (skipNode(node)) return;

Expand Down Expand Up @@ -111,21 +113,21 @@ function grabNode(config: Config, node: any): any {
let curTag = node.tagName.toLowerCase(); // 当前 tag

// 1、全局节点与空节点、input 节点、文字过多的节点、class="notranslate" 的节点不翻译
if (!node || skipSet.has(curTag) || curTag === 'input' || node.classList.contains('notranslate') || node.textContent.length > 8192) {
if (!node || skipSet.has(curTag) || curTag === 'input' || node.classList.contains('notranslate')
|| node.textContent.length > 3072 || (node.outerHTML && node.outerHTML.length > 4096)) {

return false;
}

// 2、普通适配,遇到这些标签则直接翻译节点
if (directSet.has(curTag)) return node;
if (directSet.has(curTag)) {
return node;
}

// 3、button 按钮适配
if (curTag === 'button'
|| (curTag === 'span' && node.parentNode && node.parentNode.tagName.toLowerCase() === 'button')) {
if (curTag === 'button' || (curTag === 'span' && node.parentNode && node.parentNode.tagName.toLowerCase() === 'button')) {
// 翻译按钮内部而不是整个按钮,避免按钮失去响应式点击事件
if (node.textContent.trim() !== '') {
btnTransThrottle(config, node)
}

if (node.textContent.trim() !== '') btnTransThrottle(config, node)
return false;
}

Expand Down
14 changes: 12 additions & 2 deletions entrypoints/translator/baidu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ async function baidu(config: Config, message: any) {
else if (config.to === "fr") to = "fra";
else to = config.to;

console.log("q",query)

const resp = await fetch(urls[services.baidu], {
method: 'POST',
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
Expand All @@ -31,8 +33,16 @@ async function baidu(config: Config, message: any) {

if (resp.ok) {
const result = await resp.json();
// 百度翻译结果需替换中文引号为英文引号
return result.trans_result[0].dst.replace(//g, '"').replace(//g, '"');

// 初始化一个数组用于收集翻译结果
const translatedParts: string[] = [];

// 循环 result.trans_result,获取每个翻译结果的 dst 字段,替换中文引号为英文引号(百度适配)
for (let i = 0; i < result.trans_result.length; i++) {
translatedParts.push(result.trans_result[i].dst.replace(//g, '"').replace(//g, '"'));
}

return translatedParts.join('');
} else {
console.log(resp)
throw new Error(`请求失败: ${resp.status} ${resp.statusText} body: ${await resp.text()}`);
Expand Down
12 changes: 6 additions & 6 deletions entrypoints/utils/option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,14 @@ export const options = {
label: 'DeepL翻译',
},
{
value: services.xiaoniu,
label: '小牛翻译',
value: services.baidu,
label: '百度翻译',
},
{
value: services.baidu,
label: '百度翻译(Beta)',
value: services.xiaoniu,
label: '小牛翻译',
},
// free 接口,翻译 html 会出现问题,隐藏
// free 接口,翻译 html 会出现问题,暂不启用
// {
// value: services.google,
// label: 'Google翻译',
Expand Down Expand Up @@ -191,7 +191,7 @@ export const options = {
},
{
value: services.infini,
label: '无向芯穹Infini',
label: '无向芯穹',
},
{
value: services.ollama,
Expand Down

0 comments on commit 66c931f

Please sign in to comment.