Skip to content

Commit

Permalink
Add support for use in China
Browse files Browse the repository at this point in the history
  • Loading branch information
gucong3000 committed Aug 22, 2018
1 parent 8c8b31e commit 8177d30
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
19 changes: 16 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,22 +73,35 @@ var window = {
TKK: config.get('TKK') || '0'
};

var url = config.get('url');

function updateTKK() {
return new Promise(function (resolve, reject) {
var now = Math.floor(Date.now() / 3600000);

if (Number(window.TKK.split('.')[0]) === now) {
if (url && Number(window.TKK.split('.')[0]) === now) {
resolve();
} else {
got('https://translate.google.com').then(function (res) {
const reqs = [
got('https://translate.google.com'),
got('https://translate.google.cn')
];
Promise.race(reqs).then(function (res) {
reqs.forEach(function (res) {
res.cancel();
});

var code = res.body.match(/TKK=(.*?)\(\)\)'\);/g);

if (code) {
var TKK;
eval(code[0]);
/* eslint-disable no-undef */
if (typeof TKK !== 'undefined') {
window.TKK = TKK;
config.set('TKK', TKK);
url = res.url;
config.set('url', url);
}
/* eslint-enable no-undef */
}
Expand All @@ -113,7 +126,7 @@ function get(text) {
return updateTKK().then(function () {
var tk = sM(text);
tk = tk.replace('&tk=', '');
return {name: 'tk', value: tk};
return {name: 'tk', value: tk, url: url};
}).catch(function (err) {
throw err;
});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"homepage": "https://github.com/matheuss/google-translate-tk#readme",
"dependencies": {
"configstore": "^2.0.0",
"got": "^6.3.0"
"got": "^9.0.0"
},
"devDependencies": {
"ava": "^0.15.2",
Expand Down

0 comments on commit 8177d30

Please sign in to comment.