Skip to content

Commit

Permalink
add socks_proxy
Browse files Browse the repository at this point in the history
See PR#33
  • Loading branch information
xfslove committed Aug 25, 2020
1 parent 0575126 commit 279df3f
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 37 deletions.
51 changes: 26 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
@@ -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 <kbd>enter</kbd> to read the item.
- press <kbd>cmd</kbd>+<kbd>C</kbd> to copy the item.
- press <kbd>shift</kbd> open the translate website.
- press <kbd>cmd</kbd>+<kbd>L</kbd> to see the full content.
- press <kbd>cmd</kbd>+<kbd>L</kbd> to show the translation in large text.

if You input wrong word, the workflow will correct your input, and You can press <kbd>enter</kbd> to see.
The workflow will attempt to correct spelling mistakes which can be accepted with <kbd>enter</kbd>.

## Environment Variables

| name | default value | description |
| ---------- | ---------------------------- | ------------------------------------------------------------ |
| 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) |
| 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 get voice |
| save_count | 20 | limit count of translate history, see [alfred-translate-history](https://github.com/xfslove/alfred-translate-history). <br />0 means dont save translate history |
| 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 |
| 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 |
| 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:

Expand All @@ -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:

Expand All @@ -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 <kbd>enter</kbd> to read,press<kbd>cmd</kbd>+<kbd>C</kbd> to copy
- press <kbd>enter</kbd> to read or <kbd>cmd</kbd>+<kbd>C</kbd> to copy

![general.png](media/general.png)

- press <kbd>shift</kbd> to open translate website
- press <kbd>shift</kbd> to open the translation website

![quicklook.png](media/quicklook.png)

- press <kbd>cmd</kbd>+<kbd>L</kbd> to show full content,like the [gif in hotkey](#hotkey-and-largetype-snapshot).
- press <kbd>cmd</kbd>+<kbd>L</kbd> to show the translation in large text [like this](#hotkey-and-largetype-snapshot).

## Related

Expand Down
16 changes: 11 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 = [];
Expand Down Expand Up @@ -218,15 +222,17 @@ 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)));
tts.multi(toArray, {
to: res.to.language.iso,
domain: g_config.domain,
file: res.to.language.ttsfile,
client: 'gtx'
client: 'gtx',
agent: g_config.agent
});
}
})
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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",
Expand Down
6 changes: 3 additions & 3 deletions token.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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};
Expand Down
2 changes: 1 addition & 1 deletion translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
2 changes: 1 addition & 1 deletion tts.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
Expand Down

0 comments on commit 279df3f

Please sign in to comment.