-
Notifications
You must be signed in to change notification settings - Fork 0
/
translator.html
203 lines (198 loc) · 10.4 KB
/
translator.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="res/css/style.css" />
<title>Translator</title>
<meta charset="UTF-8" />
<meta http-equiv="Content-Type" content="text/html" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimal-ui" />
<script>
if (typeof module === 'object') {
window.module = module; module = undefined;
}
</script><!-- 解决electron环境下jQuery缺失问题 -->
<script src="js/jquery-3.3.1.min.js"></script>
<script src="js/md5.min.js"></script>
<script src="js/barCtrl.js"></script>
<script>
var ipc = require('electron').ipcRenderer;
ipc.send('window-englishExplains');//调整尺寸
const Store = require('electron-store');
const store = new Store();//设置数据保存
var autoCopyPaste = store.get('autoCopyPaste');
$(document).ready(function () {
var Mousetrap = require('mousetrap');
Mousetrap.bind(['command+b', 'ctrl+b'], function () {
ipc.send("back-home");
// return false to prevent default browser behavior
// and stop event from bubbling
return false;
});
s = location.search.substr(1);
a = s.split('&');
var wordUse = '';
for (i in a) {
b = a[i].split('=');
wordUse = b[1];
}
if (wordUse) {
$("#transArea").val(decodeURI(wordUse));
translator();
}
});
</script>
</head>
<body id="body">
<div id="logo"><a href="javascript:history.back(-1)"><img src="res/icons/previous.png" width="40px" height="40px"
alt="返回首页" /></a></div>
<div id="bar">
<div id="btn">
<a href="javascript:minimize()"><img src="res/icons/minimize.png" alt="最小化" /></a>
<a href="javascript:window.close()"><img src="res/icons/close.png" alt="关闭窗口" /></a>
</div>
</div>
<table id="trans">
<tbody>
<tr>
<td id="transIn">
<select name="fromLan" id="fromLan" class="transLanList">
<option value="auto" selected="selected">自动检测</option>
<option value="zh">中文</option>
<option value="en">英语</option>
<option value="cht">繁体中文</option>
<option value="yue">粤语</option>
<option value="wyw">文言文</option>
<option value="fra">法语</option>
<option value="jp">日语</option>
<option value="kor">韩语</option>
<option value="spa">西班牙语</option>
<option value="th">泰语</option>
<option value="ara">阿拉伯语</option>
<option value="ru">俄语</option>
<option value="pt">葡萄牙语</option>
<option value="de">德语</option>
<option value="it">意大利语</option>
<option value="el">希腊语</option>
<option value="nl">荷兰语</option>
<option value="pl">波兰语</option>
<option value="bul">保加利亚语</option>
<option value="est">爱沙尼亚语</option>
<option value="dan">丹麦语</option>
<option value="fin">芬兰语</option>
<option value="cs">捷克语</option>
<option value="rom">罗马尼亚语</option>
<option value="slo">斯洛文尼亚语</option>
<option value="swe">瑞典语</option>
<option value="hu">匈牙利语</option>
<option value="vie">越南语</option>
</select><br /><br />
<textarea id="transArea" name="word" onmousedown="pasteHelper(event)"
autofocus></textarea><br /><br />
<button id="transSubmit">翻译</button>
</td>
<td> </td>
<td id="transOut">
<select name="toLan" id="toLan" class="transLanList">
<option value="zh" selected="selected">中文</option>
<option value="en">英语</option>
<option value="cht">繁体中文</option>
<option value="yue">粤语</option>
<option value="wyw">文言文</option>
<option value="fra">法语</option>
<option value="jp">日语</option>
<option value="kor">韩语</option>
<option value="spa">西班牙语</option>
<option value="th">泰语</option>
<option value="ara">阿拉伯语</option>
<option value="ru">俄语</option>
<option value="pt">葡萄牙语</option>
<option value="de">德语</option>
<option value="it">意大利语</option>
<option value="el">希腊语</option>
<option value="nl">荷兰语</option>
<option value="pl">波兰语</option>
<option value="bul">保加利亚语</option>
<option value="est">爱沙尼亚语</option>
<option value="dan">丹麦语</option>
<option value="fin">芬兰语</option>
<option value="cs">捷克语</option>
<option value="rom">罗马尼亚语</option>
<option value="slo">斯洛文尼亚语</option>
<option value="swe">瑞典语</option>
<option value="hu">匈牙利语</option>
<option value="vie">越南语</option>
</select><br /><br />
<div id="transOutWord">
请输入要翻译的内容。
</div><!-- 动态加入内容 -->
</td>
</tr>
</tbody>
</table>
<script>
function pasteHelper(event) {
if (event.button == 2 && autoCopyPaste == 'auto') {
clipboard = require('electron').clipboard;
$("#transArea").val($("#transArea").val() + clipboard.readText());//右键点击输入框时自动粘贴
}
}
function translator() {
var appid = '20160902000028007';
var key = 'rx5SwTmvX6ew4uza8OYJ';
var salt = (new Date).getTime();
var query = $("#transArea").val();
if (query) {
var from = document.getElementById("fromLan").options[document.getElementById("fromLan").selectedIndex].value;
var to = document.getElementById("toLan").options[document.getElementById("toLan").selectedIndex].value;
var str1 = appid + query + salt + key;
var sign = md5(str1);
$.ajax({
url: 'https://api.fanyi.baidu.com/api/trans/vip/translate',
type: 'get',
dataType: 'jsonp',
data: {
q: query,
appid: appid,
salt: salt,
from: from,
to: to,
sign: sign
},
success: function (data) {
if (data["trans_result"]) {
var transResult = "";
for (i in data["trans_result"])
transResult += data["trans_result"][i]["dst"] + "<br/>";
document.getElementById("transOutWord").innerHTML = transResult;
}
else {
var errorThing = "<b>发生了一点小错误,请稍后再试。</b>";
if (data["error_code"]) {
errorThing += "<br/>错误具体信息:";
if (data["error_code"] == 52001) errorThing += "请求超时";
if (data["error_code"] == 52002) errorThing += "系统错误";
if (data["error_code"] == 52003) errorThing += "未授权用户";
if (data["error_code"] == 54000) errorThing += "必填参数为空";
if (data["error_code"] == 54001) errorThing += "签名错误";
if (data["error_code"] == 54003) errorThing += "访问频率受限";
if (data["error_code"] == 54004) errorThing += "账户余额不足";
if (data["error_code"] == 58005) errorThing += "长query请求频繁";
if (data["error_code"] == 58000) errorThing += "客户端IP非法";
if (data["error_code"] == 58001) errorThing += "译文语言方向不支持";
if (data["error_code"] == 52002) errorThing += "服务当前已关闭";
errorThing += "。<br/>请等待几秒钟后再试。如果问题一直存在,请根据关于页中的联系方式联系管理员。";
}
document.getElementById("transOutWord").innerHTML = errorThing;
}
},
error: function () {
document.getElementById("transOutWord").innerHTML = "<b>无法建立与服务器的连接,请稍后再试。</b>";
}
});
} else document.getElementById("transOutWord").innerHTML = "请输入要翻译的内容。";
}
document.getElementById("transSubmit").addEventListener("click", translator);//点击按钮时提交表单
</script>
</body>
</html>
<!-- 参考:https://fanyi-api.baidu.com/api/trans/product/apidoc -->