aboutsummaryrefslogtreecommitdiff
path: root/ext
diff options
context:
space:
mode:
authorAlex Yatskov <alex@foosoft.net>2017-08-15 21:36:30 -0700
committerAlex Yatskov <alex@foosoft.net>2017-08-15 21:36:30 -0700
commite19933f9804abf4e64d96143bbb58f8059de5b38 (patch)
tree97a4f486203b06be4281fff20ad0dc81aa770dce /ext
parente079e5f252ee39b222f3eb78e81d0a6bab6626d2 (diff)
jisho.org audio support
Diffstat (limited to 'ext')
-rw-r--r--ext/bg/js/api.js3
-rw-r--r--ext/bg/js/backend.js4
-rw-r--r--ext/bg/settings.html1
-rw-r--r--ext/fg/js/api.js4
-rw-r--r--ext/mixed/js/audio.js30
-rw-r--r--ext/mixed/js/display.js3
6 files changed, 42 insertions, 3 deletions
diff --git a/ext/bg/js/api.js b/ext/bg/js/api.js
index b55e306f..8b4c3896 100644
--- a/ext/bg/js/api.js
+++ b/ext/bg/js/api.js
@@ -127,3 +127,6 @@ async function apiCommandExec(command) {
}
}
+async function apiAudioGetUrl(definition, source) {
+ return audioBuildUrl(definition, source);
+}
diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js
index 6c77ba7e..9602d385 100644
--- a/ext/bg/js/backend.js
+++ b/ext/bg/js/backend.js
@@ -110,6 +110,10 @@ class Backend {
commandExec: ({command, callback}) => {
forward(apiCommandExec(command), callback);
+ },
+
+ audioGetUrl: ({definition, source, callback}) => {
+ forward(apiAudioGetUrl(definition, source), callback);
}
};
diff --git a/ext/bg/settings.html b/ext/bg/settings.html
index 218f9f7d..ba155d4a 100644
--- a/ext/bg/settings.html
+++ b/ext/bg/settings.html
@@ -48,6 +48,7 @@
<option value="disabled">Disabled</option>
<option value="jpod101">JapanesePod101</option>
<option value="jpod101-alternate">JapanesePod101 (alternate)</option>
+ <option value="jisho">Jisho</option>
</select>
</div>
diff --git a/ext/fg/js/api.js b/ext/fg/js/api.js
index 174531ba..151882eb 100644
--- a/ext/fg/js/api.js
+++ b/ext/fg/js/api.js
@@ -52,3 +52,7 @@ function apiTemplateRender(template, data) {
function apiCommandExec(command) {
return utilInvoke('commandExec', {command});
}
+
+function apiAudioGetUrl(definition, source) {
+ return utilInvoke('audioGetUrl', {definition, source});
+}
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();
}
}
diff --git a/ext/mixed/js/display.js b/ext/mixed/js/display.js
index 21748f5d..12950dfd 100644
--- a/ext/mixed/js/display.js
+++ b/ext/mixed/js/display.js
@@ -27,7 +27,6 @@ class Display {
this.sequence = 0;
this.index = 0;
this.audioCache = {};
- this.responseCache = {};
$(document).keydown(this.onKeyDown.bind(this));
}
@@ -368,7 +367,7 @@ class Display {
try {
this.spinner.show();
- let url = await audioBuildUrl(definition, this.options.general.audioSource, this.responseCache);
+ let url = await apiAudioGetUrl(definition, this.options.general.audioSource);
if (!url) {
url = '/mixed/mp3/button.mp3';
}