This commit is contained in:
Alex Yatskov 2017-03-23 19:54:36 -07:00
parent 80d864d6b1
commit 633c5a64f7

View File

@ -23,7 +23,7 @@ class Display {
this.container = container; this.container = container;
this.definitions = []; this.definitions = [];
this.audioCache = {}; this.audioCache = {};
this.resultCache = {}; this.responseCache = {};
this.sequence = 0; this.sequence = 0;
} }
@ -168,20 +168,22 @@ class Display {
const index = Display.entryIndexFind(link); const index = Display.entryIndexFind(link);
const definition = this.definitions[index]; const definition = this.definitions[index];
let promise = Promise.resolve();
if (mode !== 'kanji') { if (mode !== 'kanji') {
const url = Display.audioBuildUrlOld(definition);
const filename = Display.audioBuildFilename(definition); const filename = Display.audioBuildFilename(definition);
if (url && filename) { if (filename) {
definition.audio = {url, filename}; promise = this.audioBuildUrl(definition).then(url => definition.audio = {url, filename});
} }
} }
this.definitionAdd(definition, mode).then(success => { promise.then(() => {
return this.definitionAdd(definition, mode).then(success => {
if (success) { if (success) {
Display.adderButtonFind(index, mode).addClass('disabled'); Display.adderButtonFind(index, mode).addClass('disabled');
} else { } else {
this.handleError('note could not be added'); this.handleError('note could not be added');
} }
});
}).catch(this.handleError.bind(this)).then(() => this.spinner.hide()); }).catch(this.handleError.bind(this)).then(() => this.spinner.hide());
} }
@ -189,13 +191,14 @@ class Display {
this.spinner.show(); this.spinner.show();
for (const key in this.audioCache) { for (const key in this.audioCache) {
const audio = this.audioCache[key]; this.audioCache[key].pause();
if (audio !== null) {
audio.pause();
}
} }
this.audioBuildUrl(definition).then(url => { this.audioBuildUrl(definition).then(url => {
if (!url) {
url = '/mixed/mp3/button.mp3';
}
let audio = this.audioCache[url]; let audio = this.audioCache[url];
if (audio) { if (audio) {
audio.currentTime = 0; audio.currentTime = 0;
@ -216,7 +219,7 @@ class Display {
audioBuildUrl(definition) { audioBuildUrl(definition) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const response = this.resultCache[definition.expression]; const response = this.responseCache[definition.expression];
if (response) { if (response) {
resolve(response); resolve(response);
return; return;
@ -236,9 +239,9 @@ class Display {
const xhr = new XMLHttpRequest(); const xhr = new XMLHttpRequest();
xhr.open('POST', 'https://www.japanesepod101.com/learningcenter/reference/dictionary_post'); xhr.open('POST', 'https://www.japanesepod101.com/learningcenter/reference/dictionary_post');
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.addEventListener('error', () => reject('failed to execute network request')); xhr.addEventListener('error', () => reject('failed to scrape audio data'));
xhr.addEventListener('load', () => { xhr.addEventListener('load', () => {
this.resultCache[definition.expression] = xhr.responseText; this.responseCache[definition.expression] = xhr.responseText;
resolve(xhr.responseText); resolve(xhr.responseText);
}); });
@ -268,14 +271,12 @@ class Display {
return entry.url; return entry.url;
} }
} }
return '/mixed/mp3/button.mp3';
}); });
} }
static audioBuildFilename(definition) { static audioBuildFilename(definition) {
if (!definition.reading && !definition.expression) { if (!definition.reading && !definition.expression) {
return null; return;
} }
let filename = 'yomichan'; let filename = 'yomichan';