From bc010c15bcc830f0e75d901aee4e6b50da32552a Mon Sep 17 00:00:00 2001 From: tinyAdapter Date: Thu, 9 Jan 2020 18:53:01 +0800 Subject: [PATCH 1/5] bugfix: Youdao API not specified in the default config --- src/main/config/DefaultConfig.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/config/DefaultConfig.ts b/src/main/config/DefaultConfig.ts index 51e4933..7778e85 100644 --- a/src/main/config/DefaultConfig.ts +++ b/src/main/config/DefaultConfig.ts @@ -21,7 +21,7 @@ export default class DefaultConfig extends Config { { enable: true, external: true, - jsFile: 'config\\hjdictApi.js', + jsFile: 'config\\youdaoApi.js', name: '有道' }, { From fda52bb8a0cba4c5a0615ffccd4b4636cc3a0e10 Mon Sep 17 00:00:00 2001 From: tinyAdapter Date: Thu, 9 Jan 2020 18:57:27 +0800 Subject: [PATCH 2/5] feature; replace HJDict with Baidu API Ditch HJDict due to fast-call limitation on the website translation API Crack Baidu's website translation API authorizations, reenable it Not enable GoogleCN API as default, since its translations are not as good as others --- config/baiduApi.js | 212 +++++++++++++++++++++++++++++++ config/config.json | 6 +- config/hjdictApi.js | 39 ------ package.json | 2 +- src/main/config/DefaultConfig.ts | 8 +- 5 files changed, 220 insertions(+), 47 deletions(-) create mode 100644 config/baiduApi.js delete mode 100644 config/hjdictApi.js diff --git a/config/baiduApi.js b/config/baiduApi.js new file mode 100644 index 0000000..e6d79d5 --- /dev/null +++ b/config/baiduApi.js @@ -0,0 +1,212 @@ +SESSION_URL = "https://fanyi.baidu.com/"; +TRANSLATE_URL = "https://fanyi.baidu.com/v2transapi"; +USER_AGENT = + "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36"; + +var getSign; +var session; +var requestTranslation; +var initSession; +var token; +var window; + +if (!getSign) { + getSign = (function(r, o, t) { + "use strict"; + function a(r) { + if (Array.isArray(r)) { + for (var o = 0, t = Array(r.length); o < r.length; o++) t[o] = r[o]; + return t; + } + return Array.from(r); + } + function n(r, o) { + for (var t = 0; t < o.length - 2; t += 3) { + var a = o.charAt(t + 2); + (a = a >= "a" ? a.charCodeAt(0) - 87 : Number(a)), + (a = "+" === o.charAt(t + 1) ? r >>> a : r << a), + (r = "+" === o.charAt(t) ? (r + a) & 4294967295 : r ^ a); + } + return r; + } + function e(r) { + var o = r.match(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g); + if (null === o) { + var t = r.length; + t > 30 && + (r = + "" + + r.substr(0, 10) + + r.substr(Math.floor(t / 2) - 5, 10) + + r.substr(-10, 10)); + } else { + for ( + var e = r.split(/[\uD800-\uDBFF][\uDC00-\uDFFF]/), + C = 0, + h = e.length, + f = []; + h > C; + C++ + ) + "" !== e[C] && f.push.apply(f, a(e[C].split(""))), + C !== h - 1 && f.push(o[C]); + var g = f.length; + g > 30 && + (r = + f.slice(0, 10).join("") + + f.slice(Math.floor(g / 2) - 5, Math.floor(g / 2) + 5).join("") + + f.slice(-10).join("")); + } + var u = void 0, + l = + "" + + String.fromCharCode(103) + + String.fromCharCode(116) + + String.fromCharCode(107); + u = null !== i ? i : (i = window[l] || "") || ""; + for ( + var d = u.split("."), + m = Number(d[0]) || 0, + s = Number(d[1]) || 0, + S = [], + c = 0, + v = 0; + v < r.length; + v++ + ) { + var A = r.charCodeAt(v); + 128 > A + ? (S[c++] = A) + : (2048 > A + ? (S[c++] = (A >> 6) | 192) + : (55296 === (64512 & A) && + v + 1 < r.length && + 56320 === (64512 & r.charCodeAt(v + 1)) + ? ((A = + 65536 + ((1023 & A) << 10) + (1023 & r.charCodeAt(++v))), + (S[c++] = (A >> 18) | 240), + (S[c++] = ((A >> 12) & 63) | 128)) + : (S[c++] = (A >> 12) | 224), + (S[c++] = ((A >> 6) & 63) | 128)), + (S[c++] = (63 & A) | 128)); + } + for ( + var p = m, + F = + "" + + String.fromCharCode(43) + + String.fromCharCode(45) + + String.fromCharCode(97) + + ("" + + String.fromCharCode(94) + + String.fromCharCode(43) + + String.fromCharCode(54)), + D = + "" + + String.fromCharCode(43) + + String.fromCharCode(45) + + String.fromCharCode(51) + + ("" + + String.fromCharCode(94) + + String.fromCharCode(43) + + String.fromCharCode(98)) + + ("" + + String.fromCharCode(43) + + String.fromCharCode(45) + + String.fromCharCode(102)), + b = 0; + b < S.length; + b++ + ) + (p += S[b]), (p = n(p, F)); + return ( + (p = n(p, D)), + (p ^= s), + 0 > p && (p = (2147483647 & p) + 2147483648), + (p %= 1e6), + p.toString() + "." + (p ^ m) + ); + } + var i = null; + return e; + })(); +} + +if (!session) { + window = {}; + window.gtk = "320305.131321201"; + session = Request.jar(); + + requestTranslation = () => { + return new Promise((resolve, reject) => { + Request.post(TRANSLATE_URL, { + gzip: true, + headers: { + Referer: SESSION_URL, + "User-Agent": USER_AGENT + }, + form: { + from: "jp", + to: "zh", + query: text, + transtype: "translang", + simple_means_flag: 3, + sign: getSign(text), + token + }, + jar: session + }) + .then(body => { + var sentences = JSON.parse(body); + if (sentences.trans_result) { + sentences = sentences.trans_result.data; + let result = ""; + for (let i in sentences) { + result += sentences[i].dst; + } + callback(result); + } else { + callback( + `Error. Raw result: ${JSON.stringify(sentences)} Sign: ${getSign( + text + )} Token: ${token}` + ); + } + }) + .catch(err => { + callback(err); + }); + }); + }; + + initSession = () => { + return Request.get(SESSION_URL, { + jar: session, + gzip: true, + headers: { + Referer: SESSION_URL, + "User-Agent": USER_AGENT + } + }).then(body => { + Request.get(SESSION_URL, { + jar: session, + gzip: true, + headers: { + Referer: SESSION_URL, + "User-Agent": USER_AGENT + } + }) + .then(body => { + token = /token: '([^\']+)',/.exec(body)[1]; + }) + .then(requestTranslation) + .catch(err => { + callback(err); + }); + }); + }; + + initSession(); +} else { + requestTranslation(); +} diff --git a/config/config.json b/config/config.json index b666cc6..dfa0587 100644 --- a/config/config.json +++ b/config/config.json @@ -23,10 +23,10 @@ }, "onlineApis": [ { - "enable": false, + "enable": true, "external": true, - "jsFile": "config\\hjdictApi.js", - "name": "沪江" + "jsFile": "config\\baiduApi.js", + "name": "百度" }, { "enable": true, diff --git a/config/hjdictApi.js b/config/hjdictApi.js deleted file mode 100644 index 304a975..0000000 --- a/config/hjdictApi.js +++ /dev/null @@ -1,39 +0,0 @@ -SESSION_URL = "https://www.hjdict.com/app/trans" -TRANSLATE_URL = - "https://www.hjdict.com/v10/dict/translation/jp/cn" -USER_AGENT = - "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36" - -var session -var bv -var requestTranslation -var initSession - -if (!session) { - session = Request.jar() - session.setCookie( - "HJ_UID=bbdfd16e-4b5d-bb9b-6ac9-2d963ed57366; _REF=; _REG=direct|; _SREF_3=; _SREG_3=direct|; TRACKSITEMAP=3%2C; HJ_SID=ab8ccf38-4913-9b56-7ac9-cab621be9af2; HJ_SSID_3=345b746e-c837-9119-201a-b49b50b561cb; HJ_CST=0; HJ_CSST_3=0", - "https://www.hjdict.com" - ) -} - -Request.post(TRANSLATE_URL, { - jar: session, - gzip: true, - headers: { - Referer: SESSION_URL, - "User-Agent": USER_AGENT - }, - form: { - content: text - } -}) - .then(body => { - var result = JSON.parse(body).data.content - // eliminate weird per word translations - result = result.replace(/({[^}]*})|(\(\([^\)]*\)\))/g, '') - callback(result) - }) - .catch(err => { - callback(err) - }) \ No newline at end of file diff --git a/package.json b/package.json index 7afef3a..36ca6ff 100644 --- a/package.json +++ b/package.json @@ -35,7 +35,7 @@ "dict/**/*", "lib/**/*", "config/youdaoApi.js", - "config/hjdictApi.js", + "config/baiduApi.js", "config/qqApi.js" ], "win": { diff --git a/src/main/config/DefaultConfig.ts b/src/main/config/DefaultConfig.ts index 7778e85..cdfccb1 100644 --- a/src/main/config/DefaultConfig.ts +++ b/src/main/config/DefaultConfig.ts @@ -13,10 +13,10 @@ export default class DefaultConfig extends Config { }, onlineApis: [ { - enable: false, + enable: true, external: true, - jsFile: 'config\\hjdictApi.js', - name: '沪江' + jsFile: 'config\\baiduApi.js', + name: '百度' }, { enable: true, @@ -25,7 +25,7 @@ export default class DefaultConfig extends Config { name: '有道' }, { - enable: true, + enable: false, method: 'POST', name: '谷歌', requestBodyFormat: 'X{"q": %TEXT%, "sl": "ja", "tl": "zh-CN"}', From f5516e26657ad9a3efc757d979cdaf0d3c95bde5 Mon Sep 17 00:00:00 2001 From: tinyAdapter Date: Thu, 9 Jan 2020 19:11:47 +0800 Subject: [PATCH 3/5] some texts about HJDict still exists --- config/config.json | 2 +- src/renderer/components/TranslatorSettings.vue | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/config/config.json b/config/config.json index dfa0587..6067d57 100644 --- a/config/config.json +++ b/config/config.json @@ -41,7 +41,7 @@ "name": "有道" }, { - "enable": true, + "enable": false, "method": "POST", "name": "谷歌", "requestBodyFormat": "X{\"q\": %TEXT%, \"sl\": \"ja\", \"tl\": \"zh-CN\"}", diff --git a/src/renderer/components/TranslatorSettings.vue b/src/renderer/components/TranslatorSettings.vue index e05422d..4f10462 100644 --- a/src/renderer/components/TranslatorSettings.vue +++ b/src/renderer/components/TranslatorSettings.vue @@ -4,12 +4,12 @@ "zh": { "underConstruction": "开发中m( _ _ )m", "pleaseModifyConfigurationFile": "请通过修改配置文件 config\\config.json 来配置翻译器", - "defaultTranslators": "(默认提供腾讯,沪江,谷歌,有道,彩云翻译)" + "defaultTranslators": "(默认提供腾讯,百度,谷歌,有道,彩云翻译)" }, "en": { "underConstruction": "Under construction m( _ _ )m", "pleaseModifyConfigurationFile": "Please modify configuration file config\\config.json to configure translators", - "defaultTranslators": "(Defualt translators are QQ, HuJiang, Google, YouDao. These are all Japanese-Chinese translators)" + "defaultTranslators": "(Defualt translators are QQ, Baidu, Google, YouDao. These are all Japanese-Chinese translators)" } } From f168dbd72bc7c80561ff7f8a131cfc4f4d97d795 Mon Sep 17 00:00:00 2001 From: tinyAdapter Date: Fri, 10 Jan 2020 16:32:28 +0800 Subject: [PATCH 4/5] feature: fixed translations showing order This lets users find their favorite translation APIs easier --- src/translator/components/TranslatePage.vue | 114 +++++++++++--------- src/translator/store/modules/Hooks.ts | 23 +++- 2 files changed, 84 insertions(+), 53 deletions(-) diff --git a/src/translator/components/TranslatePage.vue b/src/translator/components/TranslatePage.vue index 8703218..55fd8c1 100644 --- a/src/translator/components/TranslatePage.vue +++ b/src/translator/components/TranslatePage.vue @@ -44,21 +44,13 @@ diff --git a/src/translator/components/SettingsPage.vue b/src/translator/components/SettingsPage.vue index 75c41ce..939509f 100644 --- a/src/translator/components/SettingsPage.vue +++ b/src/translator/components/SettingsPage.vue @@ -148,6 +148,7 @@ import { Route } from 'vue-router' import { namespace, State } from 'vuex-class' import { Chrome } from 'vue-color' +import { updateWindowHeight } from '../common/Window' @Component({ components: { @@ -269,12 +270,11 @@ export default class HookSettings extends Vue { } public beforeRouteEnter (to: Route, from: Route, next: () => void) { - const newHeight = Math.trunc( - remote.screen.getPrimaryDisplay().size.height * 0.6 + updateWindowHeight( + this, + false, + Math.trunc(remote.screen.getPrimaryDisplay().size.height * 0.6) ) - const window = remote.getCurrentWindow() - const width = window.getSize()[0] - window.setSize(width, newHeight) next() } diff --git a/src/translator/components/TranslatePage.vue b/src/translator/components/TranslatePage.vue index 55fd8c1..77db1af 100644 --- a/src/translator/components/TranslatePage.vue +++ b/src/translator/components/TranslatePage.vue @@ -51,6 +51,7 @@ import Component from 'vue-class-component' import { Prop, Watch } from 'vue-property-decorator' import { Route } from 'vue-router' import { namespace } from 'vuex-class' +import { updateWindowHeight } from '../common/Window' import YkHookSettings from '@/components/HookSettings.vue' import YkMecabText from '@/components/MecabText.vue' @@ -197,46 +198,27 @@ export default class TranslatePage extends Vue { } public beforeRouteEnter (to: Route, from: Route, next: () => void) { - const newHeight = document.body.offsetHeight + 24 - const window = remote.getCurrentWindow() - const width = window.getSize()[0] - window.setSize(width, newHeight) + updateWindowHeight(this, true, 24) next() } - public updateWindowHeight (offset: number) { - const newHeight = document.body.offsetHeight + offset - const window = remote.getCurrentWindow() - const width = window.getSize()[0] - if (newHeight > 640) { - this.$nextTick(() => { - this.$store.dispatch('View/setWindowTooHigh', true) - }) - } else { - this.$nextTick(() => { - this.$store.dispatch('View/setWindowTooHigh', false) - }) - } - window.setSize(width, newHeight) - } - @Watch('isButtonShown') public onButtonShownChanged () { if (this.isButtonsShown) { - this.updateWindowHeight(24) + updateWindowHeight(this, true, 24) } else { - this.updateWindowHeight(0) + updateWindowHeight(this, true, 0) } } public updated () { if (this.isButtonsShown) { this.$nextTick(() => { - this.updateWindowHeight(24) + updateWindowHeight(this, true, 24) }) } else { this.$nextTick(() => { - this.updateWindowHeight(0) + updateWindowHeight(this, true, 0) }) } }