forked from crimx/ext-saladict
-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine.ts
281 lines (253 loc) · 7.17 KB
/
engine.ts
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import { fetchDirtyDOM } from '@/_helpers/fetch-dom'
import {
handleNoResult,
handleNetWorkError,
getText,
getInnerHTML,
SearchFunction,
GetSrcPageFunction,
DictSearchResult,
getChsToChz
} from '../helpers'
import { DictConfigs } from '@/app-config'
export const getSrcPage: GetSrcPageFunction = text =>
'https://cn.bing.com/dict/search?q=' +
encodeURIComponent(text.replace(/\s+/g, ' '))
const HOST = 'https://cn.bing.com'
const DICT_LINK =
'https://cn.bing.com/dict/clientsearch?mkt=zh-CN&setLang=zh&form=BDVEHC&ClientVer=BDDTV3.5.1.4320&q='
/** Lexical result */
export interface BingResultLex {
type: 'lex'
title: string
/** phonetic symbols */
phsym?: Array<{
/** Phonetic Alphabet, UK|US|PY */
lang: string
/** pronunciation */
pron: string
}>
/** common definitions */
cdef?: Array<{
/** part of speech */
pos: string
/** definition */
def: string
}>
/** infinitive */
infs?: string[]
sentences?: Array<{
en?: string
chs?: string
source?: string
mp3?: string
}>
}
/** Alternate machine translation result */
export interface BingResultMachine {
type: 'machine'
/** machine translation */
mt: string
}
/** Alternate result */
export interface BingResultRelated {
type: 'related'
title: string
defs: Array<{
title: string
meanings: Array<{
href: string
word: string
def: string
}>
}>
}
export type BingResult = BingResultLex | BingResultMachine | BingResultRelated
type BingConfig = DictConfigs['bing']
type BingSearchResultLex = DictSearchResult<BingResultLex>
type BingSearchResultMachine = DictSearchResult<BingResultMachine>
type BingSearchResultRelated = DictSearchResult<BingResultRelated>
export const search: SearchFunction<BingResult> = (
text,
config,
profile,
payload
) => {
const bingConfig = profile.dicts.all.bing
return fetchDirtyDOM(
DICT_LINK + encodeURIComponent(text.replace(/\s+/g, ' '))
)
.catch(handleNetWorkError)
.then(async doc => {
const transform = await getChsToChz(config.langCode)
if (doc.querySelector('.client_def_hd_hd')) {
return handleLexResult(doc, bingConfig.options, transform)
}
if (doc.querySelector('.client_trans_head')) {
return handleMachineResult(doc, transform)
}
if (bingConfig.options.related) {
if (doc.querySelector('.client_do_you_mean_title_bar')) {
return handleRelatedResult(doc, bingConfig, transform)
}
}
return handleNoResult<DictSearchResult<BingResult>>()
})
}
function handleLexResult(
doc: Document,
options: BingConfig['options'],
transform: null | ((text: string) => string)
): BingSearchResultLex | Promise<BingSearchResultLex> {
const searchResult: DictSearchResult<BingResultLex> = {
result: {
type: 'lex',
title: getText(doc, '.client_def_hd_hd', transform)
}
}
// pronunciation
if (options.phsym) {
const $prons = Array.from(doc.querySelectorAll('.client_def_hd_pn_list'))
if ($prons.length > 0) {
searchResult.result.phsym = $prons.map(el => {
let pron = ''
const $audio = el.querySelector('.client_aud_o')
if ($audio) {
pron = (($audio.getAttribute('onclick') || '').match(
/https.*\.mp3/
) || [''])[0]
}
return {
lang: getText(el, '.client_def_hd_pn'),
pron
}
})
searchResult.audio = searchResult.result.phsym.reduce(
(audio, { lang, pron }) => {
if (/us|美/i.test(lang)) {
audio['us'] = pron
} else if (/uk|英/i.test(lang)) {
audio['uk'] = pron
}
return audio
},
{}
)
}
}
// definitions
if (options.cdef) {
const $container = doc.querySelector('.client_def_container')
if ($container) {
const $defs = Array.from($container.querySelectorAll('.client_def_bar'))
if ($defs.length > 0) {
searchResult.result.cdef = $defs.map(el => ({
pos: getText(el, '.client_def_title_bar', transform),
def: getText(el, '.client_def_list', transform)
}))
}
}
}
// tense
if (options.tense) {
const $infs = Array.from(doc.querySelectorAll('.client_word_change_word'))
if ($infs.length > 0) {
searchResult.result.infs = $infs.map(el => (el.textContent || '').trim())
}
}
if (options.sentence > 0) {
const $sens = doc.querySelectorAll('.client_sentence_list')
const sentences: typeof searchResult.result.sentences = []
for (
let i = 0;
i < $sens.length && sentences.length < options.sentence;
i++
) {
const el = $sens[i]
let mp3 = ''
const $audio = el.querySelector('.client_aud_o')
if ($audio) {
mp3 = (($audio.getAttribute('onclick') || '').match(/https.*\.mp3/) || [
''
])[0]
}
el.querySelectorAll('.client_sen_en_word').forEach($word => {
$word.outerHTML = getText($word)
})
el.querySelectorAll('.client_sen_cn_word').forEach($word => {
$word.outerHTML = getText($word, transform)
})
el.querySelectorAll('.client_sentence_search').forEach($word => {
$word.outerHTML = `<span class="dictBing-SentenceItem_HL">${getText(
$word
)}</span>`
})
sentences.push({
en: getInnerHTML(HOST, el, '.client_sen_en'),
chs: getInnerHTML(HOST, el, {
selector: '.client_sen_cn',
transform
}),
source: getText(el, '.client_sentence_list_link'),
mp3
})
}
searchResult.result.sentences = sentences
}
if (Object.keys(searchResult.result).length > 2) {
return searchResult
}
return handleNoResult()
}
function handleMachineResult(
doc: Document,
transform: null | ((text: string) => string)
): BingSearchResultMachine | Promise<BingSearchResultMachine> {
const mt = getText(doc, '.client_sen_cn', transform)
if (mt) {
return {
result: {
type: 'machine',
mt
}
}
}
return handleNoResult()
}
function handleRelatedResult(
doc: Document,
config: BingConfig,
transform: null | ((text: string) => string)
): BingSearchResultRelated | Promise<BingSearchResultRelated> {
const searchResult: DictSearchResult<BingResultRelated> = {
result: {
type: 'related',
title: getText(doc, '.client_do_you_mean_title_bar', transform),
defs: []
}
}
doc.querySelectorAll('.client_do_you_mean_area').forEach($area => {
const $defsList = $area.querySelectorAll('.client_do_you_mean_list')
if ($defsList.length > 0) {
searchResult.result.defs.push({
title: getText($area, '.client_do_you_mean_title', transform),
meanings: Array.from($defsList).map($list => {
const word = getText(
$list,
'.client_do_you_mean_list_word',
transform
)
return {
href: `https://cn.bing.com/dict/search?q=${word}`,
word,
def: getText($list, '.client_do_you_mean_list_def', transform)
}
})
})
}
})
if (searchResult.result.defs.length > 0) {
return searchResult
}
return handleNoResult()
}