Skip to content

Commit

Permalink
fix(dicts): encode url
Browse files Browse the repository at this point in the history
Closes #170
  • Loading branch information
crimx committed Jul 26, 2018
1 parent 9f0c6b6 commit 0c0cd20
Show file tree
Hide file tree
Showing 12 changed files with 13 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/components/dictionaries/bing/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export default function search (
): Promise<DictSearchResult<BingResult>> {
const bingConfig = config.dicts.all.bing

return fetchDirtyDOM(DICT_LINK + text)
return fetchDirtyDOM(DICT_LINK + encodeURIComponent(text))
.then(doc => {
if (doc.querySelector('.client_def_hd_hd')) {
return handleLexResult(doc, bingConfig.options)
Expand Down
1 change: 1 addition & 0 deletions src/components/dictionaries/cobuild/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export default function search (
text: string,
config: AppConfig
): Promise<COBUILDSearchResult> {
text = encodeURIComponent(text)
return fetchDirtyDOM('https://www.iciba.com/' + text)
.then(doc => handleDOM(doc, config.dicts.all.cobuild.options))
.catch(() => {
Expand Down
1 change: 1 addition & 0 deletions src/components/dictionaries/etymonline/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export default function search (
config: AppConfig,
): Promise<EtymonlineSearchResult> {
const options = config.dicts.all.etymonline.options
text = encodeURIComponent(text)

// http to bypass the referer checking
return fetchDirtyDOM('http://www.etymonline.com/search?q=' + text)
Expand Down
2 changes: 1 addition & 1 deletion src/components/dictionaries/eudic/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default function search (
text: string,
config: AppConfig,
): Promise<EudicSearchResult> {
text = text.split(/\s+/).slice(0, 2).join(' ')
text = encodeURIComponent(text.split(/\s+/).slice(0, 2).join(' '))
const options = config.dicts.all.eudic.options

return fetchDirtyDOM('https://dict.eudic.net/dicts/en/' + text)
Expand Down
4 changes: 2 additions & 2 deletions src/components/dictionaries/google/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function fetchWithToken (base: string, sl: string, tl: string, text: string): Pr
if (tkk) {
const tk = getTK(text, Number(tkk[2]), (Number(tkk[0]) + Number(tkk[1])))
if (tk) {
return fetch(`${base}/translate_a/single?client=t&sl=${sl}&tl=${tl}&q=${text}&tk=${tk}&hl=en&dt=at&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t&ie=UTF-8&oe=UTF-8&otf=1&ssel=0&tsel=0&kc=5`)
return fetch(`${base}/translate_a/single?client=t&sl=${sl}&tl=${tl}&q=${encodeURIComponent(text)}&tk=${tk}&hl=en&dt=at&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t&ie=UTF-8&oe=UTF-8&otf=1&ssel=0&tsel=0&kc=5`)
}
}
return handleNoResult()
Expand All @@ -51,7 +51,7 @@ function fetchWithToken (base: string, sl: string, tl: string, text: string): Pr
}

function fetchWithoutToken (sl: string, tl: string, text: string): Promise<string> {
return fetch(`https://translate.googleapis.com/translate_a/single?client=gtx&sl=${sl}&tl=${tl}&dt=t&q=${text}`)
return fetch(`https://translate.googleapis.com/translate_a/single?client=gtx&sl=${sl}&tl=${tl}&dt=t&q=${encodeURIComponent(text)}`)
.then(r => r.text())
}

Expand Down
2 changes: 1 addition & 1 deletion src/components/dictionaries/googledict/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default function search (
config: AppConfig
): Promise<GoogleDictSearchResult> {
const isen = config.dicts.all.googledict.options.enresult ? 'hl=en&gl=en&' : ''
return fetch(`https://www.google.com/search?${isen}q=define+` + text.replace(/\s+/g, '+'))
return fetch(`https://www.google.com/search?${isen}q=define+` + encodeURIComponent(text.replace(/\s+/g, '+')))
.then(r => r.text())
.then(handleDOM)
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/dictionaries/guoyu/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export default function search (
text: string,
config: AppConfig
): Promise<DictSearchResult<GuoYuResult>> {
return moedictSearch<GuoYuResult>('a', text, config)
return moedictSearch<GuoYuResult>('a', encodeURIComponent(text), config)
}

export function moedictSearch<R extends GuoYuResult> (
Expand Down
2 changes: 1 addition & 1 deletion src/components/dictionaries/liangan/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default function search (
text: string,
config: AppConfig
): Promise<DictSearchResult<LiangAnResult>> {
return moedictSearch<LiangAnResult>('c', text, config)
return moedictSearch<LiangAnResult>('c', encodeURIComponent(text), config)
.then(result => {
if (result.result.h) {
result.result.h.forEach(h => {
Expand Down
2 changes: 1 addition & 1 deletion src/components/dictionaries/urban/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export default function search (
): Promise<UrbanSearchResult> {
const options = config.dicts.all.urban.options

return fetchDirtyDOM('http://www.urbandictionary.com/define.php?term=' + text)
return fetchDirtyDOM('http://www.urbandictionary.com/define.php?term=' + encodeURIComponent(text))
.then(doc => handleDOM(doc, options))
}

Expand Down
2 changes: 1 addition & 1 deletion src/components/dictionaries/vocabulary/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default function search (
text: string,
config: AppConfig,
): Promise<VocabularySearchResult> {
return fetchDirtyDOM('https://www.vocabulary.com/dictionary/' + text)
return fetchDirtyDOM('https://www.vocabulary.com/dictionary/' + encodeURIComponent(text))
.then(handleDOM)
}

Expand Down
2 changes: 1 addition & 1 deletion src/components/dictionaries/youdao/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export default function search (
): Promise<YoudaoSearchResult> {
const options = config.dicts.all.youdao.options

return fetchDirtyDOM('http://www.youdao.com/w/' + text)
return fetchDirtyDOM('http://www.youdao.com/w/' + encodeURIComponent(text))
.then(doc => checkResult(doc, options))
}

Expand Down
2 changes: 1 addition & 1 deletion src/components/dictionaries/zdic/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export default function search (
text: string,
config: AppConfig,
): Promise<ZdicSearchResult> {
return fetchDirtyDOM('http://www.zdic.net/search/?c=3&q=' + text)
return fetchDirtyDOM('http://www.zdic.net/search/?c=3&q=' + encodeURIComponent(text))
.then(deobfuscate)
.then(handleDOM)
}
Expand Down

0 comments on commit 0c0cd20

Please sign in to comment.