Skip to content

Commit

Permalink
Merge pull request #63 from project-yuki/b0.14
Browse files Browse the repository at this point in the history
B0.14
  • Loading branch information
tinyAdapter authored Jan 11, 2020
2 parents adf4e5d + 191ae63 commit 97830f2
Show file tree
Hide file tree
Showing 13 changed files with 359 additions and 178 deletions.
212 changes: 212 additions & 0 deletions config/baiduApi.js
Original file line number Diff line number Diff line change
@@ -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();
}
8 changes: 4 additions & 4 deletions config/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
},
"onlineApis": [
{
"enable": false,
"enable": true,
"external": true,
"jsFile": "config\\hjdictApi.js",
"name": "沪江"
"jsFile": "config\\baiduApi.js",
"name": "百度"
},
{
"enable": true,
Expand All @@ -41,7 +41,7 @@
"name": "有道"
},
{
"enable": true,
"enable": false,
"method": "POST",
"name": "谷歌",
"requestBodyFormat": "X{\"q\": %TEXT%, \"sl\": \"ja\", \"tl\": \"zh-CN\"}",
Expand Down
39 changes: 0 additions & 39 deletions config/hjdictApi.js

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"dict/**/*",
"lib/**/*",
"config/youdaoApi.js",
"config/hjdictApi.js",
"config/baiduApi.js",
"config/qqApi.js"
],
"win": {
Expand Down
10 changes: 5 additions & 5 deletions src/main/config/DefaultConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@ 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,
external: true,
jsFile: 'config\\hjdictApi.js',
jsFile: 'config\\youdaoApi.js',
name: '有道'
},
{
enable: true,
enable: false,
method: 'POST',
name: '谷歌',
requestBodyFormat: 'X{"q": %TEXT%, "sl": "ja", "tl": "zh-CN"}',
Expand Down
4 changes: 2 additions & 2 deletions src/renderer/components/TranslatorSettings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
}
}
</i18n>
Expand Down
17 changes: 3 additions & 14 deletions src/translator/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import { ipcRenderer, remote } from 'electron'
import IpcTypes from '../common/IpcTypes'
import YkTitlebar from '@/components/Titlebar.vue'
import { updateWindowHeight } from './common/Window'
@Component({
components: {
Expand Down Expand Up @@ -129,18 +130,6 @@ export default class App extends Vue {
})
}
public updateWindowHeight (offset: number) {
const newHeight = document.body.offsetHeight + offset
const window = remote.getCurrentWindow()
const width = window.getSize()[0]
if (newHeight > 640) {
this.$store.dispatch('View/setWindowTooHigh', true)
} else {
this.$store.dispatch('View/setWindowTooHigh', false)
}
window.setSize(width, newHeight)
}
@Watch('currentIndex')
public onCurrentIndexChanged () {
this.$store.dispatch('View/setButtonsShown', false)
Expand All @@ -158,11 +147,11 @@ export default class App extends Vue {
) {
if (this.isButtonsShown && !this.isWindowTooHigh) {
this.$nextTick(() => {
this.updateWindowHeight(24)
updateWindowHeight(this, true, 24)
})
} else {
this.$nextTick(() => {
this.updateWindowHeight(0)
updateWindowHeight(this, true, 0)
})
}
}
Expand Down
22 changes: 22 additions & 0 deletions src/translator/common/Window.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { remote } from 'electron'

export function updateWindowHeight (
component: Vue.default, addBodyHeight: boolean, offset: number) {
let newHeight = offset
if (addBodyHeight) newHeight += document.body.offsetHeight
const window = remote.getCurrentWindow()
const width = window.getSize()[0]
if (component) {
if (newHeight > 640) {
component.$nextTick(() => {
component.$store.dispatch('View/setWindowTooHigh', true)
})
} else {
component.$nextTick(() => {
component.$store.dispatch('View/setWindowTooHigh', false)
})
}
}
window.setSize(width, newHeight)
return newHeight
}
Loading

0 comments on commit 97830f2

Please sign in to comment.