-
Notifications
You must be signed in to change notification settings - Fork 0
/
searcher-e2e.html
162 lines (155 loc) · 8.66 KB
/
searcher-e2e.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
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="res/css/style.css" />
<title>English Explanations</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 src="js/copyCommand.js"></script>
<script>
var ipc = require('electron').ipcRenderer;
ipc.send('window-englishExplains');//调整尺寸
$(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 wordIn = '';
for (i in a) {
b = a[i].split('=');
wordIn = b[1];
}
var wordUse = "";
for (i in wordIn) {
if (('a' <= wordIn[i] && wordIn[i] <= 'z') || ('0' <= wordIn[i] && wordIn[i] <= '9') || wordIn[i] == '-') wordUse += wordIn[i];
else if ('A' <= wordIn[i] && wordIn[i] <= 'Z') wordUse += wordIn[i].toLowerCase();
else if (wordIn[i] == '+') wordUse += ' ';
else {
wordUse = "";
break;
}
}//拼写检查、输入字串格式化
$.ajax({
url: 'https://www.dictionaryapi.com/api/v3/references/learners/json/' + wordUse,
type: 'get',
dataType: 'json',
data: { key: "5a5f4c91-d999-4a8d-9228-341d031b7c66" },
success: function (data) {
console.log(data);
var parentDiv = document.getElementById("inscroll");
parentDiv.innerHTML = "";//清空原有内容
var wordShowDiv = document.createElement("span");
wordShowDiv.id = "wordTitle";
wordShowDiv.innerHTML = "<img src='res/content/wordTitle.png'/> " + wordUse;
parentDiv.appendChild(wordShowDiv);
var phoneticDiv = document.createElement("span");
phoneticDiv.id = "phonetic";
var phonetic = "";
if (data) {
for (i in data) {
if (data[i].hwi) {
if (data[i].hwi.prs) {
for (j in data[i].hwi.prs) {
phonetic += ' [' + data[i].hwi.prs[j].ipa + ']';
}
}
}
}
}
phoneticDiv.innerHTML = phonetic;
parentDiv.appendChild(phoneticDiv);
var explainsDiv = document.createElement("span");
var explains = "<br/>";
var isThereAnyContent = false;
var theExplain;
for (i in data) {
if (data[i]["def"]) {
if (data[i]["def"][0]["sseq"]) {
if (data[i]["ins"]) {
explains += "<span id='smallSpan'>";
if (data[i]["fl"]) explains += "<b>" + data[i]["fl"] + "</b> ";
for (j in data[i]["ins"]) {
explains += "[ " + data[i]["ins"][j]["if"] + " ] ";
}
explains += "</span><br/>";
}//添加变位信息
for (j in data[i]["def"][0]["sseq"]) {
for (k in data[i]["def"][0]["sseq"][j]) {
if (data[i]["def"][0]["sseq"][j][k][1]["dt"]) {
theExplain = data[i]["def"][0]["sseq"][j][k][1]["dt"][0][1];
if (theExplain.indexOf("{dx") == -1 && theExplain.indexOf("{sx") == -1) {//摆脱难搞的sx、dx
if (typeof theExplain == "string") {
explains += theExplain + "<br/>";//加入定义
for (l in data[i]["def"][0]["sseq"][j][k][1]["dt"]) {
if (data[i]["def"][0]["sseq"][j][k][1]["dt"][l][0] == "vis") {
explains += "<span id='example'> ";
explains += data[i]["def"][0]["sseq"][j][k][1]["dt"][l][1][0]["t"] + "</span><br/>";
}//加入例句(限于篇幅,只加第一句)
}
} else explains += theExplain[0][0][1] + "<br/>";//有时会有不按规范定义data-structure的情况
}
}
}
}//添加单词释义
explains += "<br/>";
isThereAnyContent = true;
}
}
}
if (!isThereAnyContent) document.getElementById("inscroll").innerHTML = "<br/><h2>对不起,这个词暂时还不在词库中。</h2><h3><a href='index.html' class='backLink'>返回</a> | 您是打算 <a href='translator.html?word=" + wordUse + "' class='backLink'>翻译</a> 吗?</h3>";
explains = explains.replace(/{bc}/g, "- ");//转换{bc}
explains = explains.replace(/{it}/g, "<i>");//转换{it}
explains = explains.replace(/{\/it}/g, "</i>");//转换{/it}
explains = explains.replace(/{phrase}/g, "<i>");//转换{phrase}
explains = explains.replace(/{\/phrase}/g, "</i>");//转换{/phrase}
explains = explains.replace(/{ldquo}/g, "<i>\"");//转换{ldquo}
explains = explains.replace(/{rdquo}/g, "\"</i>");//转换{rdquo}
explains = explains.replace(/\*/g, "");//删去多余的"*"字符
explains = explains.replace(/<br\/>undefined/g, "");//删除多余的"undefined"
explainsDiv.innerHTML = explains;//以上部分可以修复格式,虽然仍是凑合的方式
parentDiv.appendChild(explainsDiv);
if (isThereAnyContent) {
var webDiv = document.createElement("span");
webDiv.innerHTML = "<a href=\"searcher-e2c.html?word=" + wordUse + "\" class='backLink'>查看英汉释义</a>";
parentDiv.appendChild(webDiv);//完整英英释义
}
},
error: function () {
document.getElementById("inscroll").innerHTML = "<br/><h2>无法建立与服务器的连接,请稍后再试。</h2><h3><a href='index.html' class='backLink'>返回</a></h3>";
}
});
});//使用jQuery::jsonp获取远程词汇
</script>
</head>
<body id="body">
<div id="logo"><a href="index.html"><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>
<div id="inscroll">
<br />
<h2>等待服务器响应...</h2>
<h3><a href="javascript:history.back(-1)" class="backLink">返回</a></h3>
<!-- 动态加入内容 -->
</div>
</body>
</html>
<!-- 参考:https://www.dictionaryapi.com/account/example -->