-
Notifications
You must be signed in to change notification settings - Fork 0
/
table.html
62 lines (60 loc) · 1.78 KB
/
table.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
<!doctype html>
<head>
<meta charset="utf-8"/>
</head>
<body>
<div>
<textarea id="query-text" cols="30" rows="3">hello</textarea>
<button onclick="translation()">翻译</button>
</div>
<div>
<textarea id="translation-text" cols="30" rows="3"></textarea>
</div>
<script src="http://shared.ydstatic.com/js/jquery/jquery-3.1.1.min.js"></script>
<script src="https://cdn.bootcss.com/blueimp-md5/2.10.0/js/md5.js"></script>
<script src="https://cdn.bootcss.com/js-sha256/0.9.0/sha256.js"></script>
<script type="text/javascript">
function translation(){
var appKey = '4c9111811aa13b58';
var key = 'MKOdFqCcKQtYWjrQKBOtXoA3jT7qptpg';//注意:暴露appSecret,有被盗用造成损失的风险
var salt = (new Date).getTime();
var curTime = Math.round(new Date().getTime()/1000);
var query = $("#query-text").val();
// 多个query可以用\n连接 如 query='apple\norange\nbanana\npear'
var from = 'AUTO';
var to = 'AUTO';
var str1 = appKey + truncate(query) + salt + curTime + key;
console.log(str1);
var sign = sha256(str1);
$.ajax({
url: 'http://openapi.youdao.com/api',
type: 'post',
dataType: 'jsonp',
data: {
q: query,
appKey: appKey,
salt: salt,
from: from,
to: to,
sign: sign,
signType: "v3",
curtime: curTime,
},
success: function (data) {
var translationText = "";
if(data.translation){
data.translation.forEach(function(item){
translationText += item;
})
}
$("#translation-text").val(translationText);
}
});
}
function truncate(q){
var len = q.length;
if(len<=20) return q;
return q.substring(0, 10) + len + q.substring(len-10, len);
}
</script>
</body>