Skip to content

Commit dfbf2a3

Browse files
committed
Merge branch 'v2.x'
2 parents 63af11f + 279df3f commit dfbf2a3

File tree

6 files changed

+20
-12
lines changed

6 files changed

+20
-12
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ The workflow will attempt to correct spelling mistakes which can be accepted wit
4242
| domain | https://translate.google.com | if you cannot access the default domain, you can config this. <br />大陆访问不了默认域名,所以如果使用2.x版本需要将这个变量设置为https://translate.google.cn. 或者还是使用[1.x版本](https://github.com/xfslove/alfred-google-translate/tree/v1.x) |
4343
| voice | remote | avaliable values: <br />remote: fetch voice from google, <br />local: use macOS local voice (notice: maybe only works on English),<br />none: dont use voice |
4444
| save_count | 20 | limit the translation history, see [alfred-translate-history](https://github.com/xfslove/alfred-translate-history). <br />a value of 0 will keep no history |
45+
| socks_proxy| - | not turned by default. you can specify local or remote socks proxy. format: `socks://{host}:{port}` example: local shadowsocks proxy 'socks://127.0.0.1:1086' |
4546

4647
##### environment variables config snapshot:
4748

index.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ var uuidv4 = require('uuid/v4');
88
var languagePair = new configstore('language-config-pair');
99
var history = new configstore("translate-history");
1010
var languages = require("./languages");
11+
var SocksProxyAgent = require('socks-proxy-agent');
1112

1213
var g_config = {
1314
voice: process.env.voice || 'remote',
1415
save: process.env.save_count || 20,
15-
domain: process.env.domain || 'https://translate.google.com'
16+
domain: process.env.domain || 'https://translate.google.com',
17+
agent: process.env.socks_proxy ? new SocksProxyAgent(process.env.socks_proxy) : undefined
1618
};
1719

1820
var pair = languagePair.get('pair');
@@ -40,7 +42,8 @@ if (pair) {
4042
from: 'auto',
4143
to: 'en',
4244
domain: g_config.domain,
43-
client: 'gtx'
45+
client: 'gtx',
46+
agent: g_config.agent
4447
})
4548
.then(function (res) {
4649
var detect = res.from.language.iso;
@@ -99,7 +102,8 @@ function doTranslate(opts) {
99102
from: opts.from.language,
100103
to: opts.to.language,
101104
domain: g_config.domain,
102-
client: 'gtx'
105+
client: 'gtx',
106+
agent: g_config.agent
103107
})
104108
.then(function (res) {
105109
var items = [];
@@ -218,15 +222,17 @@ function doTranslate(opts) {
218222
to: res.from.language.iso,
219223
domain: g_config.domain,
220224
file: res.from.language.ttsfile,
221-
client: 'gtx'
225+
client: 'gtx',
226+
agent: g_config.agent
222227
});
223228
var toArray = [];
224229
res.to.text.array.forEach(o => tts.split(o).forEach(t => toArray.push(t)));
225230
tts.multi(toArray, {
226231
to: res.to.language.iso,
227232
domain: g_config.domain,
228233
file: res.to.language.ttsfile,
229-
client: 'gtx'
234+
client: 'gtx',
235+
agent: g_config.agent
230236
});
231237
}
232238
})

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "alfred-google-translate",
3-
"version": "2.0.8",
3+
"version": "2.0.9",
44
"description": "Alfred 3 workflow to translate with google translate api",
55
"license": "MIT",
66
"repository": "xfslove/alfred-google-translate",
@@ -39,7 +39,8 @@
3939
"alfy": "^0.9.0",
4040
"configstore": "^2.0.0",
4141
"got": "^6.3.0",
42-
"uuid": "^3.3.2"
42+
"uuid": "^3.3.2",
43+
"socks-proxy-agent": "^4.0.2"
4344
},
4445
"devDependencies": {
4546
"alfy-test": "^0.3.0",

token.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,14 @@ var window = {
7373
TKK: config.get('TKK') || '0'
7474
};
7575

76-
function updateTKK(url) {
76+
function updateTKK(opts) {
7777
return new Promise(function (resolve, reject) {
7878
var now = Math.floor(Date.now() / 3600000);
7979

8080
if (Number(window.TKK.split('.')[0]) === now) {
8181
resolve();
8282
} else {
83-
got(url).then(function (res) {
83+
got(opts.domain, {agent: opts.agent}).then(function (res) {
8484
var matches = res.body.match(/tkk:\s?'(.+?)'/i);
8585

8686
if (matches) {
@@ -107,7 +107,7 @@ function updateTKK(url) {
107107
function get(text, opts) {
108108
opts = opts || {};
109109

110-
return updateTKK(opts.domain).then(function () {
110+
return updateTKK(opts).then(function () {
111111
var tk = sM(text);
112112
tk = tk.replace('&tk=', '');
113113
return {name: 'tk', value: tk};

translate.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ function translate(text, opts) {
2525
data[token.name] = token.value;
2626
return url + '?' + querystring.stringify(data);
2727
}).then(function (url) {
28-
return got(url).then(function (res) {
28+
return got(url, {agent: opts.agent}).then(function (res) {
2929
var body = JSON.parse(res.body);
3030

3131
var result = {

tts.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ function single(text, opts) {
7777
data[token.name] = token.value;
7878
return url + '?' + querystring.stringify(data);
7979
}).then(function (url) {
80-
return got(url, {encoding: null}).then(function (res) {
80+
return got(url, {encoding: null, agent: opts.agent}).then(function (res) {
8181
fs.appendFile(opts.file, res.body, function (err) {
8282
if (err) throw err;
8383
});

0 commit comments

Comments
 (0)