diff options
author | Alex Yatskov <alex@foosoft.net> | 2017-04-02 12:08:49 -0700 |
---|---|---|
committer | Alex Yatskov <alex@foosoft.net> | 2017-04-02 12:08:49 -0700 |
commit | ccb740da58068633a0ef5bd1c26bd4dd10625dce (patch) | |
tree | 6e0e90351eb6d0294ffbf27fefbe7fc6d064645b /ext/mixed/js/util.js | |
parent | 372cdbf14bd4412c8078e26a6324edd534b6daf4 (diff) | |
parent | 9ac55fb5d11e5037e808939598591d55d1368b43 (diff) |
Merge branch 'dev'
Diffstat (limited to 'ext/mixed/js/util.js')
-rw-r--r-- | ext/mixed/js/util.js | 101 |
1 files changed, 62 insertions, 39 deletions
diff --git a/ext/mixed/js/util.js b/ext/mixed/js/util.js index 4ce60e4f..edd49873 100644 --- a/ext/mixed/js/util.js +++ b/ext/mixed/js/util.js @@ -21,48 +21,71 @@ * Audio */ -function audioBuildUrl(definition, cache={}) { - return new Promise((resolve, reject) => { - const response = cache[definition.expression]; - if (response) { - resolve(response); - } else { - const data = { - post: 'dictionary_reference', - match_type: 'exact', - search_query: definition.expression - }; +function audioBuildUrl(definition, mode, cache={}) { + if (mode === 'jpod101') { + let kana = definition.reading; + let kanji = definition.expression; - const params = []; - for (const key in data) { - params.push(`${encodeURIComponent(key)}=${encodeURIComponent(data[key])}`); - } - - const xhr = new XMLHttpRequest(); - xhr.open('POST', 'https://www.japanesepod101.com/learningcenter/reference/dictionary_post'); - xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); - xhr.addEventListener('error', () => reject('failed to scrape audio data')); - xhr.addEventListener('load', () => { - cache[definition.expression] = xhr.responseText; - resolve(xhr.responseText); - }); + if (!kana && wanakana.isHiragana(kanji)) { + kana = kanji; + kanji = null; + } - xhr.send(params.join('&')); + const params = []; + if (kanji) { + params.push(`kanji=${encodeURIComponent(kanji)}`); } - }).then(response => { - const dom = new DOMParser().parseFromString(response, 'text/html'); - for (const row of dom.getElementsByClassName('dc-result-row')) { - try { - const url = row.getElementsByClassName('ill-onebuttonplayer').item(0).getAttribute('data-url'); - const reading = row.getElementsByClassName('dc-vocab_kana').item(0).innerText; - if (url && reading && (!definition.reading || definition.reading === reading)) { - return url; + if (kana) { + params.push(`kana=${encodeURIComponent(kana)}`); + } + + const url = `https://assets.languagepod101.com/dictionary/japanese/audiomp3.php?${params.join('&')}`; + return Promise.resolve(url); + } else if (mode === 'jpod101-alternate') { + return new Promise((resolve, reject) => { + const response = cache[definition.expression]; + if (response) { + resolve(response); + } else { + const data = { + post: 'dictionary_reference', + match_type: 'exact', + search_query: definition.expression + }; + + const params = []; + for (const key in data) { + params.push(`${encodeURIComponent(key)}=${encodeURIComponent(data[key])}`); } - } catch (e) { - // NOP + + const xhr = new XMLHttpRequest(); + xhr.open('POST', 'https://www.japanesepod101.com/learningcenter/reference/dictionary_post'); + xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); + xhr.addEventListener('error', () => reject('failed to scrape audio data')); + xhr.addEventListener('load', () => { + cache[definition.expression] = xhr.responseText; + resolve(xhr.responseText); + }); + + xhr.send(params.join('&')); } - } - }); + }).then(response => { + const dom = new DOMParser().parseFromString(response, 'text/html'); + for (const row of dom.getElementsByClassName('dc-result-row')) { + try { + const url = row.getElementsByClassName('ill-onebuttonplayer').item(0).getAttribute('data-url'); + const reading = row.getElementsByClassName('dc-vocab_kana').item(0).innerText; + if (url && reading && (!definition.reading || definition.reading === reading)) { + return url; + } + } catch (e) { + // NOP + } + } + }); + } else { + return Promise.reject('unsupported audio source'); + } } function audioBuildFilename(definition) { @@ -79,7 +102,7 @@ function audioBuildFilename(definition) { } } -function audioInject(definition, fields) { +function audioInject(definition, fields, mode) { const filename = audioBuildFilename(definition); if (!filename) { return Promise.resolve(true); @@ -97,7 +120,7 @@ function audioInject(definition, fields) { return Promise.resolve(true); } - return audioBuildUrl(definition).then(url => { + return audioBuildUrl(definition, mode).then(url => { definition.audio = {url, filename}; return true; }).catch(() => false); |