Simplify URL normalization (#1112)
This commit is contained in:
parent
cd82fe8aac
commit
e29c752793
@ -67,22 +67,8 @@ class AudioDownloader {
|
|||||||
|
|
||||||
// Private
|
// Private
|
||||||
|
|
||||||
_normalizeUrl(url, baseUrl, basePath) {
|
_normalizeUrl(url, base) {
|
||||||
if (url) {
|
return new URL(url, base).href;
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async _getInfoJpod101(expression, reading) {
|
async _getInfoJpod101(expression, reading) {
|
||||||
@ -140,7 +126,7 @@ class AudioDownloader {
|
|||||||
|
|
||||||
const htmlReading = dom.getTextContent(htmlReadings[0]);
|
const htmlReading = dom.getTextContent(htmlReadings[0]);
|
||||||
if (htmlReading && (!reading || reading === htmlReading)) {
|
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}};
|
return {type: 'url', details: {url}};
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
@ -171,7 +157,7 @@ class AudioDownloader {
|
|||||||
if (source !== null) {
|
if (source !== null) {
|
||||||
let url = dom.getAttribute(source, 'src');
|
let url = dom.getAttribute(source, 'src');
|
||||||
if (url !== null) {
|
if (url !== null) {
|
||||||
url = this._normalizeUrl(url, 'https://jisho.org', '/search/');
|
url = this._normalizeUrl(url, response.url);
|
||||||
return {type: 'url', details: {url}};
|
return {type: 'url', details: {url}};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user