aboutsummaryrefslogtreecommitdiff
path: root/ext/mixed/js
diff options
context:
space:
mode:
authorAlex Yatskov <alex@foosoft.net>2017-04-02 12:12:21 -0700
committerAlex Yatskov <alex@foosoft.net>2017-04-02 12:12:21 -0700
commitbea50cca462be631dadbd2f5bde1bc0a12ad17bf (patch)
tree73612a4fedc4d1fe9874979c27187b4176a84569 /ext/mixed/js
parent85001208db67027c430b3a4c8d569fb3930aeb27 (diff)
parentccb740da58068633a0ef5bd1c26bd4dd10625dce (diff)
Merge branch 'master' into firefox-amo
Diffstat (limited to 'ext/mixed/js')
-rw-r--r--ext/mixed/js/display.js4
-rw-r--r--ext/mixed/js/util.js101
2 files changed, 64 insertions, 41 deletions
diff --git a/ext/mixed/js/display.js b/ext/mixed/js/display.js
index 36609525..f5ad4849 100644
--- a/ext/mixed/js/display.js
+++ b/ext/mixed/js/display.js
@@ -69,7 +69,7 @@ class Display {
definitions,
addable: options.anki.enable,
grouped: options.general.groupResults,
- playback: options.general.audioPlayback
+ playback: options.general.audioSource !== 'disabled'
};
if (context) {
@@ -335,7 +335,7 @@ class Display {
this.audioCache[key].pause();
}
- audioBuildUrl(definition, this.responseCache).then(url => {
+ audioBuildUrl(definition, this.options.general.audioSource, this.responseCache).then(url => {
if (!url) {
url = '/mixed/mp3/button.mp3';
}
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);