From 279df3f817a745f1f77393914d00d888c9ea1677 Mon Sep 17 00:00:00 2001 From: wongiven Date: Tue, 25 Aug 2020 10:48:21 +0800 Subject: [PATCH] add socks_proxy See PR#33 --- README.md | 51 ++++++++++++++++++++++++++------------------------- index.js | 16 +++++++++++----- package.json | 5 +++-- token.js | 6 +++--- translate.js | 2 +- tts.js | 2 +- 6 files changed, 45 insertions(+), 37 deletions(-) diff --git a/README.md b/README.md index dbff6ce..69bd7d4 100644 --- a/README.md +++ b/README.md @@ -1,45 +1,48 @@ # alfred-google-translate [![NPM](https://nodei.co/npm/alfred-google-translate.png)](https://nodei.co/npm/alfred-google-translate/) -## Install +### JetBrains' support -*Requires [Node.js](https://nodejs.org) 8+ and the Alfred 3 or 4 [Powerpack](https://www.alfredapp.com/powerpack/).* +[![jetbrain](media/jetbrains.svg)](https://www.jetbrains.com/?from=alfred-google-translate) -- install with `npm install -g alfred-google-translate` -- or [download](https://github.com/xfslove/alfred-google-translate/releases/tag/v2.0.8) workflow +## Installation -## [Changelog](https://github.com/xfslove/alfred-google-translate/releases) +*Requires the Alfred 3 or 4 [Powerpack](https://www.alfredapp.com/powerpack/), [Node.js](https://nodejs.org) 8+, and the [alfred-language-configuration](https://github.com/xfslove/alfred-language-configuration) workflow.* -## Usage - -Before using this workflow, You must config the language pair. see [alfred-language-configuration](https://github.com/xfslove/alfred-language-configuration), (**The Language Auto Detected** btw, [#36](https://github.com/xfslove/alfred-google-translate/issues/36)) +To use this workflow you must also install the [alfred-language-configuration](https://github.com/xfslove/alfred-language-configuration) workflow, and configure the language pair. See that [README](https://github.com/xfslove/alfred-language-configuration) for instructions. -Alfred workflow Keyword `tr`. +### With NPM +- Install with `npm install -g alfred-google-translate`. -the shown items explain: +### Manually +- Or [download the workflow directly](https://github.com/xfslove/alfred-google-translate/releases/tag/v2.0.8). -The first item is input word or sentence (webster phonetic if avaliable). +## Changelog +See [all past and current releases](https://github.com/xfslove/alfred-google-translate/releases). -The second item is translation of input (webster phonetic if avaliable). +## Usage -The rest of items are Definitions and Translations of input. +Alfred workflow Keyword: `tr [word or sentence]` +Example: `tr kitchen sink` or `tr Hello, my name is Alfred` -at the first and second item You can: +When translating a word you will see the translation as well as alternate translations if available. +With the first two results (which are the input word and the translation) you can… - press enter to read the item. - press cmd+C to copy the item. - press shift open the translate website. -- press cmd+L to see the full content. +- press cmd+L to show the translation in large text. -if You input wrong word, the workflow will correct your input, and You can press enter to see. +The workflow will attempt to correct spelling mistakes which can be accepted with enter. ## Environment Variables | name | default value | description | | ---------- | ---------------------------- | ------------------------------------------------------------ | | domain | https://translate.google.com | if you cannot access the default domain, you can config this.
大陆访问不了默认域名,所以如果使用2.x版本需要将这个变量设置为https://translate.google.cn. 或者还是使用[1.x版本](https://github.com/xfslove/alfred-google-translate/tree/v1.x) | -| voice | remote | avaliable values:
remote: fetch voice from google,
local: use macOS local voice (notice: maybe only works on English),
none: dont get voice | -| save_count | 20 | limit count of translate history, see [alfred-translate-history](https://github.com/xfslove/alfred-translate-history).
0 means dont save translate history | +| voice | remote | avaliable values:
remote: fetch voice from google,
local: use macOS local voice (notice: maybe only works on English),
none: dont use voice | +| save_count | 20 | limit the translation history, see [alfred-translate-history](https://github.com/xfslove/alfred-translate-history).
a value of 0 will keep no history | +| 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' | ##### environment variables config snapshot: @@ -49,7 +52,7 @@ if You input wrong word, the workflow will correct your input, and You can press ## Hotkey -if you download the workflow, you may lost the hotkey, so you can manual config this. +If you download the workflow, you may have to manually set the hotkey yourself. ##### hotkey config snapshot: @@ -63,23 +66,21 @@ if you download the workflow, you may lost the hotkey, so you can manual config -## Snapshot - -- correct +## Screenshots ![](media/detect-lang.png) ![corrected.png](media/corrected.png) -- press enter to read,presscmd+C to copy +- press enter to read or cmd+C to copy ![general.png](media/general.png) -- press shift to open translate website +- press shift to open the translation website ![quicklook.png](media/quicklook.png) -- press cmd+L to show full content,like the [gif in hotkey](#hotkey-and-largetype-snapshot). +- press cmd+L to show the translation in large text [like this](#hotkey-and-largetype-snapshot). ## Related diff --git a/index.js b/index.js index d9bbf5e..5cbe26f 100644 --- a/index.js +++ b/index.js @@ -8,11 +8,13 @@ var uuidv4 = require('uuid/v4'); var languagePair = new configstore('language-config-pair'); var history = new configstore("translate-history"); var languages = require("./languages"); +var SocksProxyAgent = require('socks-proxy-agent'); var g_config = { voice: process.env.voice || 'remote', save: process.env.save_count || 20, - domain: process.env.domain || 'https://translate.google.com' + domain: process.env.domain || 'https://translate.google.com', + agent: process.env.socks_proxy ? new SocksProxyAgent(process.env.socks_proxy) : undefined }; var pair = languagePair.get('pair'); @@ -40,7 +42,8 @@ if (pair) { from: 'auto', to: 'en', domain: g_config.domain, - client: 'gtx' + client: 'gtx', + agent: g_config.agent }) .then(function (res) { var detect = res.from.language.iso; @@ -99,7 +102,8 @@ function doTranslate(opts) { from: opts.from.language, to: opts.to.language, domain: g_config.domain, - client: 'gtx' + client: 'gtx', + agent: g_config.agent }) .then(function (res) { var items = []; @@ -218,7 +222,8 @@ function doTranslate(opts) { to: res.from.language.iso, domain: g_config.domain, file: res.from.language.ttsfile, - client: 'gtx' + client: 'gtx', + agent: g_config.agent }); var toArray = []; res.to.text.array.forEach(o => tts.split(o).forEach(t => toArray.push(t))); @@ -226,7 +231,8 @@ function doTranslate(opts) { to: res.to.language.iso, domain: g_config.domain, file: res.to.language.ttsfile, - client: 'gtx' + client: 'gtx', + agent: g_config.agent }); } }) diff --git a/package.json b/package.json index da8ffdb..5e01b0d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "alfred-google-translate", - "version": "2.0.8", + "version": "2.0.9", "description": "Alfred 3 workflow to translate with google translate api", "license": "MIT", "repository": "xfslove/alfred-google-translate", @@ -39,7 +39,8 @@ "alfy": "^0.9.0", "configstore": "^2.0.0", "got": "^6.3.0", - "uuid": "^3.3.2" + "uuid": "^3.3.2", + "socks-proxy-agent": "^4.0.2" }, "devDependencies": { "alfy-test": "^0.3.0", diff --git a/token.js b/token.js index 14a15be..8a3f6eb 100644 --- a/token.js +++ b/token.js @@ -73,14 +73,14 @@ var window = { TKK: config.get('TKK') || '0' }; -function updateTKK(url) { +function updateTKK(opts) { return new Promise(function (resolve, reject) { var now = Math.floor(Date.now() / 3600000); if (Number(window.TKK.split('.')[0]) === now) { resolve(); } else { - got(url).then(function (res) { + got(opts.domain, {agent: opts.agent}).then(function (res) { var matches = res.body.match(/tkk:\s?'(.+?)'/i); if (matches) { @@ -107,7 +107,7 @@ function updateTKK(url) { function get(text, opts) { opts = opts || {}; - return updateTKK(opts.domain).then(function () { + return updateTKK(opts).then(function () { var tk = sM(text); tk = tk.replace('&tk=', ''); return {name: 'tk', value: tk}; diff --git a/translate.js b/translate.js index 26e35b4..8a53cd9 100644 --- a/translate.js +++ b/translate.js @@ -25,7 +25,7 @@ function translate(text, opts) { data[token.name] = token.value; return url + '?' + querystring.stringify(data); }).then(function (url) { - return got(url).then(function (res) { + return got(url, {agent: opts.agent}).then(function (res) { var body = JSON.parse(res.body); var result = { diff --git a/tts.js b/tts.js index 5df55c5..a792ee2 100644 --- a/tts.js +++ b/tts.js @@ -77,7 +77,7 @@ function single(text, opts) { data[token.name] = token.value; return url + '?' + querystring.stringify(data); }).then(function (url) { - return got(url, {encoding: null}).then(function (res) { + return got(url, {encoding: null, agent: opts.agent}).then(function (res) { fs.appendFile(opts.file, res.body, function (err) { if (err) throw err; });