wip
This commit is contained in:
parent
046b1b45a5
commit
80d864d6b1
@ -23,6 +23,7 @@ class Display {
|
|||||||
this.container = container;
|
this.container = container;
|
||||||
this.definitions = [];
|
this.definitions = [];
|
||||||
this.audioCache = {};
|
this.audioCache = {};
|
||||||
|
this.resultCache = {};
|
||||||
this.sequence = 0;
|
this.sequence = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -80,7 +81,7 @@ class Display {
|
|||||||
$('.action-play-audio').click(e => {
|
$('.action-play-audio').click(e => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
const index = Display.entryIndexFind($(e.currentTarget));
|
const index = Display.entryIndexFind($(e.currentTarget));
|
||||||
this.audioPlay(this.definitions[index], this.audioCache);
|
this.audioPlay(this.definitions[index]);
|
||||||
});
|
});
|
||||||
$('.kanji-link').click(e => {
|
$('.kanji-link').click(e => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
@ -184,18 +185,18 @@ class Display {
|
|||||||
}).catch(this.handleError.bind(this)).then(() => this.spinner.hide());
|
}).catch(this.handleError.bind(this)).then(() => this.spinner.hide());
|
||||||
}
|
}
|
||||||
|
|
||||||
audioPlay(definition, cache) {
|
audioPlay(definition) {
|
||||||
this.spinner.show();
|
this.spinner.show();
|
||||||
|
|
||||||
for (const key in cache) {
|
for (const key in this.audioCache) {
|
||||||
const audio = cache[key];
|
const audio = this.audioCache[key];
|
||||||
if (audio !== null) {
|
if (audio !== null) {
|
||||||
audio.pause();
|
audio.pause();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Display.audioBuildUrl(definition).then(url => {
|
this.audioBuildUrl(definition).then(url => {
|
||||||
let audio = cache[url];
|
let audio = this.audioCache[url];
|
||||||
if (audio) {
|
if (audio) {
|
||||||
audio.currentTime = 0;
|
audio.currentTime = 0;
|
||||||
audio.play();
|
audio.play();
|
||||||
@ -206,23 +207,21 @@ class Display {
|
|||||||
audio = new Audio('/mixed/mp3/button.mp3');
|
audio = new Audio('/mixed/mp3/button.mp3');
|
||||||
}
|
}
|
||||||
|
|
||||||
cache[url] = audio;
|
this.audioCache[url] = audio;
|
||||||
audio.play();
|
audio.play();
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}).catch(this.handleError.bind(this)).then(() => this.spinner.hide());
|
}).catch(this.handleError.bind(this)).then(() => this.spinner.hide());
|
||||||
}
|
}
|
||||||
|
|
||||||
static entryIndexFind(element) {
|
audioBuildUrl(definition) {
|
||||||
return $('.entry').index(element.closest('.entry'));
|
|
||||||
}
|
|
||||||
|
|
||||||
static adderButtonFind(index, mode) {
|
|
||||||
return $('.entry').eq(index).find(`.action-add-note[data-mode="${mode}"]`);
|
|
||||||
}
|
|
||||||
|
|
||||||
static audioBuildUrl(definition) {
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
|
const response = this.resultCache[definition.expression];
|
||||||
|
if (response) {
|
||||||
|
resolve(response);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const data = {
|
const data = {
|
||||||
post: 'dictionary_reference',
|
post: 'dictionary_reference',
|
||||||
match_type: 'exact',
|
match_type: 'exact',
|
||||||
@ -235,10 +234,14 @@ class Display {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const xhr = new XMLHttpRequest();
|
const xhr = new XMLHttpRequest();
|
||||||
xhr.addEventListener('error', () => reject('failed to execute network request'));
|
|
||||||
xhr.addEventListener('load', () => resolve(xhr.responseText));
|
|
||||||
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('load', () => {
|
||||||
|
this.resultCache[definition.expression] = xhr.responseText;
|
||||||
|
resolve(xhr.responseText);
|
||||||
|
});
|
||||||
|
|
||||||
xhr.send(params.join('&'));
|
xhr.send(params.join('&'));
|
||||||
}).then(response => {
|
}).then(response => {
|
||||||
const dom = new DOMParser().parseFromString(response, 'text/html');
|
const dom = new DOMParser().parseFromString(response, 'text/html');
|
||||||
@ -270,30 +273,6 @@ class Display {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
static audioBuildUrlOld(definition) {
|
|
||||||
let kana = definition.reading;
|
|
||||||
let kanji = definition.expression;
|
|
||||||
|
|
||||||
if (!kana && !kanji) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!kana && wanakana.isHiragana(kanji)) {
|
|
||||||
kana = kanji;
|
|
||||||
kanji = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
const params = [];
|
|
||||||
if (kanji) {
|
|
||||||
params.push(`kanji=${encodeURIComponent(kanji)}`);
|
|
||||||
}
|
|
||||||
if (kana) {
|
|
||||||
params.push(`kana=${encodeURIComponent(kana)}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
return `https://assets.languagepod101.com/dictionary/japanese/audiomp3.php?${params.join('&')}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
static audioBuildFilename(definition) {
|
static audioBuildFilename(definition) {
|
||||||
if (!definition.reading && !definition.expression) {
|
if (!definition.reading && !definition.expression) {
|
||||||
return null;
|
return null;
|
||||||
@ -309,4 +288,12 @@ class Display {
|
|||||||
|
|
||||||
return filename += '.mp3';
|
return filename += '.mp3';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static entryIndexFind(element) {
|
||||||
|
return $('.entry').index(element.closest('.entry'));
|
||||||
|
}
|
||||||
|
|
||||||
|
static adderButtonFind(index, mode) {
|
||||||
|
return $('.entry').eq(index).find(`.action-add-note[data-mode="${mode}"]`);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user