Skip to content

Commit

Permalink
划词翻译增加判断是中文还是英文,互译
Browse files Browse the repository at this point in the history
  • Loading branch information
scoful committed Oct 2, 2023
1 parent b15277d commit 6a1a0f6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 24 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
## 实现功能

1. 划词翻译
- 支持配置是否启用
- 支持配置是否启用,自动检测中文还是英文,互译

2. 插件图标显示当前一共打开了多少个tab,多个窗口也能统计

Expand Down Expand Up @@ -209,7 +209,7 @@
- GitHub的api,如果返回的数据太大,会进行截断,需要注意,而Gitee的就不会,可能没考虑过这个问题
- 翻译用的是有道api,这是好早以前申请的,请求频率限制为每小时1000次,[文档](http://fanyi.youdao.com/openapi?path=data-mode)
- 翻译用的是腾讯交互翻译api,[腾讯交互翻译](https://transmart.qq.com/zh-CN/index),自己扒的api
- GitHub的根据gistId获取gist,居然不用token也能获取私有的,理论上存在被人撞到私有gist的可能,Gitee则需要
Expand Down Expand Up @@ -271,6 +271,8 @@
[Bootstrap中文网](https://www.bootcss.com/)
[腾讯交互翻译](https://transmart.qq.com/zh-CN/index)
## Star历史
[![Star History Chart](https://starchart.cc/scoful/cloudSkyMonster.svg)](https://starchart.cc/scoful/cloudSkyMonster)
Expand Down
52 changes: 30 additions & 22 deletions js/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -409,38 +409,48 @@ function setHandleGistStatus(status) {
chrome.storage.local.set({handleGistStatus: gistStatusMap});
}

// 判断是否中文
function isChinese(str) {
var reg = /[\u4e00-\u9fa5]/; // 使用Unicode范围匹配中文字符
return reg.test(str);
}

// 判断是否英文
function isEnglish(str) {
var reg = /^[a-zA-Z]+$/; // 匹配纯英文字符
return reg.test(str);
}

// 调用腾讯交互翻译api
function translateFunc(txt) {
console.log("开始翻译!");
let source = "en"
let target = "zh"
if (isChinese(txt)) {
source = "zh"
target = "en"
}
if (isEnglish(txt)) {
source = "en"
target = "zh"
}
let url = "https://transmart.qq.com/api/imt"
let data = JSON.stringify({
"header": {
"fn": "auto_translation",
"session": "",
"client_key": "browser-chrome-117.0.0-Windows 10-4daf3e2e-b66e-43a1-944a-a8f6b42c9199-1696226243060",
"user": ""
},
"type": "plain",
"model_category": "normal",
"text_domain": "general",
"source": {
"lang": "en",
"text_list": [
txt
]
},
"target": {
"lang": "zh"
}, "type": "plain", "model_category": "normal", "text_domain": "general", "source": {
"lang": source, "text_list": [txt]
}, "target": {
"lang": target
}
})
$.ajax({
type: "POST",
url: url,
data: data,
headers: {
type: "POST", url: url, data: data, headers: {
"Content-Type": "application/json"
},
success: function (data, status) {
}, success: function (data, status) {
console.log(data)
if (status === "success") {
if (data.header.ret_code) {
Expand All @@ -450,11 +460,9 @@ function translateFunc(txt) {
} else {
sendMessageToContentScript("translateResult", "--FAILED--!");
}
},
error: function (xhr, errorText, errorType) {
}, error: function (xhr, errorText, errorType) {
sendMessageToContentScript("translateResult", "--ERROR,may be lost network--!");
},
complete: function () {
}, complete: function () {
//do something
}
})
Expand Down

0 comments on commit 6a1a0f6

Please sign in to comment.