Skip to content

Commit

Permalink
chore: Update image URLs to use a CDN mirror for faster loading
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaozhou26 committed Jun 28, 2024
1 parent 8e63124 commit 2b76240
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 8 deletions.
2 changes: 2 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
PROXY_LIST=http://127.0.0.1:2080
DEEPL_COOKIES=006986c2-5e5f-4ae0-ad4c-8405a41ada6d
4 changes: 2 additions & 2 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,12 @@ print(response.text)


安装插件后,点击浏览器右上角的插件按钮,选择"沉浸式翻译",然后进入设置页面。
![p1](https://www.jsdelivr.ren/gh/xiaozhou26/tuph@main/images/20240314170457.png)
![p1](https://cdn.jsdmirror.com/gh/xiaozhou26/tuph@main/images/20240314170457.png)

在左下角点击"开发者设置",然后开启"Beta测试特性"。

点击"基本设置",然后在翻译服务中选择"DeepLX"。在"API URL"字段中填入以下API地址:
![p2](https://www.jsdelivr.ren/gh/xiaozhou26/tuph@main/images/20240314170447.png)
![p2](https://cdn.jsdmirror.com/gh/xiaozhou26/tuph@main/images/20240314170447.png)

```
https://localhost:8000/translate
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
## 使用方法

F12打开开发者控制台,然后按照图片复制的cookie的dl_session。
![DeepL翻译API示例代码](https://jsd.cdn.zzko.cn/gh/xiaozhou26/tuph@main/images/2024-03-07%20120245.png)
![DeepL翻译API示例代码](https://cdn.jsdmirror.com/gh/xiaozhou26/tuph@main/images/2024-03-07%20120245.png)

## 环境变量
```
Expand Down
22 changes: 19 additions & 3 deletions proxyPool.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
// proxyPool.js

let proxies = process.env.PROXY_LIST ? process.env.PROXY_LIST.split(',') : [];
let invalidProxies = [];
let currentProxyIndex = 0;

function parseProxy(proxy) {
if (proxy.startsWith('http://')) {
proxy = proxy.slice(7); // 删除 'http://'
} else if (proxy.startsWith('socks://')) {
proxy = proxy.slice(8); // 删除 'socks://'
}
return proxy;
}

function getNextProxy() {
let attempts = 0;

// 如果所有代理都被标记为无效,重置无效代理列表
if (invalidProxies.length >= proxies.length) {
invalidProxies = [];
}

while (attempts < proxies.length) {
const proxy = proxies[currentProxyIndex];
let proxy = proxies[currentProxyIndex];
proxy = parseProxy(proxy); // 解析代理,删除前缀

if (!invalidProxies.includes(proxy)) {
currentProxyIndex = (currentProxyIndex + 1) % proxies.length;
return proxy;
Expand All @@ -21,6 +36,7 @@ function getNextProxy() {
function markProxyInvalid(proxy) {
if (!invalidProxies.includes(proxy)) {
invalidProxies.push(proxy);
console.log(`Proxy marked as invalid: ${proxy}`);
}
}

Expand Down
11 changes: 9 additions & 2 deletions translator.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ async function translate(text, sourceLang = 'AUTO', targetLang = 'ZH', numberAlt
}

const headers = { ...baseHeaders };
if (proxy) headers['proxy'] = proxy;
if (cookie) headers['cookie'] = cookie;

const postData = {
Expand All @@ -72,8 +71,16 @@ async function translate(text, sourceLang = 'AUTO', targetLang = 'ZH', numberAlt
},
};

const axiosConfig = {
headers,
proxy: proxy ? {
host: proxy.split(':')[0],
port: parseInt(proxy.split(':')[1], 10),
} : false,
};

try {
const response = await axios.post(DEEPL_BASE_URL, JSON.stringify(postData), { headers });
const response = await axios.post(DEEPL_BASE_URL, postData, axiosConfig);
if (response.status !== 200) {
console.error('Error', response.status);
return null;
Expand Down

0 comments on commit 2b76240

Please sign in to comment.