summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Yatskov <FooSoft@users.noreply.github.com>2019-02-20 19:17:59 -0800
committerGitHub <noreply@github.com>2019-02-20 19:17:59 -0800
commit27fb9643e471dbd1c0e6a19023a449bf7688bd71 (patch)
treef21d87c0f4f76d1686068853f4a0091a865f5f51
parent08f4919b53c598733c45c3418072c2d9e01e02f9 (diff)
parentee5e47251eb416952e0a4f8dfe923fe48565e64b (diff)
Merge pull request #156 from toasted-nutbread/audio-url-fix
Fix alternate audio URLs
-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..2e5db7cc 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 audioUrlNormalize(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 audioUrlNormalize(url, 'https://jisho.org', '/search/');
+ }
}
} catch (e) {
// NOP
@@ -112,6 +115,24 @@ async function audioBuildUrl(definition, mode, cache={}) {
}
}
+function audioUrlNormalize(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';