From e19933f9804abf4e64d96143bbb58f8059de5b38 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Tue, 15 Aug 2017 21:36:30 -0700 Subject: jisho.org audio support --- ext/mixed/js/audio.js | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) (limited to 'ext/mixed/js/audio.js') diff --git a/ext/mixed/js/audio.js b/ext/mixed/js/audio.js index eb8fde94..0952887e 100644 --- a/ext/mixed/js/audio.js +++ b/ext/mixed/js/audio.js @@ -79,7 +79,35 @@ async function audioBuildUrl(definition, mode, cache={}) { } } }); - } else { + } else if (mode === 'jisho') { + return new Promise((resolve, reject) => { + const response = cache[definition.expression]; + if (response) { + resolve(response); + } else { + const xhr = new XMLHttpRequest(); + xhr.open('GET', `http://jisho.org/search/${definition.expression}`); + xhr.addEventListener('error', () => reject('failed to scrape audio data')); + xhr.addEventListener('load', () => { + cache[definition.expression] = xhr.responseText; + resolve(xhr.responseText); + }); + + xhr.send(); + } + }).then(response => { + try { + const dom = new DOMParser().parseFromString(response, 'text/html'); + const audio = dom.getElementById(`audio_${definition.expression}:${definition.reading}`); + if (audio) { + return audio.getElementsByTagName('source').item(0).getAttribute('src'); + } + } catch (e) { + // NOP + } + }); + } + else { return Promise.resolve(); } } -- cgit v1.2.3