Simplify URL normalization (#1112)

This commit is contained in:
toasted-nutbread 2020-12-14 22:39:03 -05:00 committed by GitHub
parent cd82fe8aac
commit e29c752793
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -67,22 +67,8 @@ class AudioDownloader {
// Private
_normalizeUrl(url, baseUrl, basePath) {
if (url) {
if (url[0] === '/') {
if (url.length >= 2 && url[1] === '/') {
// Begins with "//"
url = baseUrl.substring(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;
_normalizeUrl(url, base) {
return new URL(url, base).href;
}
async _getInfoJpod101(expression, reading) {
@ -140,7 +126,7 @@ class AudioDownloader {
const htmlReading = dom.getTextContent(htmlReadings[0]);
if (htmlReading && (!reading || reading === htmlReading)) {
url = this._normalizeUrl(url, 'https://www.japanesepod101.com', '/learningcenter/reference/');
url = this._normalizeUrl(url, response.url);
return {type: 'url', details: {url}};
}
} catch (e) {
@ -171,7 +157,7 @@ class AudioDownloader {
if (source !== null) {
let url = dom.getAttribute(source, 'src');
if (url !== null) {
url = this._normalizeUrl(url, 'https://jisho.org', '/search/');
url = this._normalizeUrl(url, response.url);
return {type: 'url', details: {url}};
}
}