aboutsummaryrefslogtreecommitdiff
path: root/ext/bg/js/audio.js
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2019-02-16 22:34:00 -0500
committertoasted-nutbread <toasted-nutbread@users.noreply.github.com>2019-02-16 22:34:00 -0500
commitb58b91d3fce466457521b87dda850c8514c5e381 (patch)
tree3e0775acbd11de3c756f81fcaf21e5a593ab199c /ext/bg/js/audio.js
parent08f4919b53c598733c45c3418072c2d9e01e02f9 (diff)
Fix audio URLs
Diffstat (limited to 'ext/bg/js/audio.js')
-rw-r--r--ext/bg/js/audio.js29
1 files changed, 25 insertions, 4 deletions
diff --git a/ext/bg/js/audio.js b/ext/bg/js/audio.js
index 549288f5..c93b7550 100644
--- a/ext/bg/js/audio.js
+++ b/ext/bg/js/audio.js
@@ -69,10 +69,10 @@ async function audioBuildUrl(definition, mode, cache={}) {
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 url = row.querySelector('audio>source[src]').getAttribute('src');
const reading = row.getElementsByClassName('dc-vocab_kana').item(0).innerText;
if (url && reading && (!definition.reading || definition.reading === reading)) {
- return url;
+ return normalizeAudioUrl(url, 'https://www.japanesepod101.com', '/learningcenter/reference/');
}
} catch (e) {
// NOP
@@ -86,7 +86,7 @@ async function audioBuildUrl(definition, mode, cache={}) {
resolve(response);
} else {
const xhr = new XMLHttpRequest();
- xhr.open('GET', `http://jisho.org/search/${definition.expression}`);
+ xhr.open('GET', `https://jisho.org/search/${definition.expression}`);
xhr.addEventListener('error', () => reject('Failed to scrape audio data'));
xhr.addEventListener('load', () => {
cache[definition.expression] = xhr.responseText;
@@ -100,7 +100,10 @@ async function audioBuildUrl(definition, mode, cache={}) {
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');
+ const url = audio.getElementsByTagName('source').item(0).getAttribute('src');
+ if (url) {
+ return normalizeAudioUrl(url, 'https://jisho.org', '/search/');
+ }
}
} catch (e) {
// NOP
@@ -112,6 +115,24 @@ async function audioBuildUrl(definition, mode, cache={}) {
}
}
+function normalizeAudioUrl(url, baseUrl, basePath) {
+ if (url) {
+ if (url[0] === '/') {
+ if (url.length >= 2 && url[1] === '/') {
+ // Begins with "//"
+ url = baseUrl.substr(0, baseUrl.indexOf(':') + 1) + url;
+ } else {
+ // Begins with "/"
+ url = baseUrl + url;
+ }
+ } else if (!/^[a-z][a-z0-9\+\-\.]*:/i.test(url)) {
+ // No URI scheme => relative path
+ url = baseUrl + basePath + url;
+ }
+ }
+ return url;
+}
+
function audioBuildFilename(definition) {
if (definition.reading || definition.expression) {
let filename = 'yomichan';