Create function to create a new DOM parser (#1114)

This commit is contained in:
toasted-nutbread 2020-12-14 23:05:04 -05:00 committed by GitHub
parent 7926821c48
commit 26dd0270d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 2 deletions

View File

@ -109,7 +109,7 @@ class AudioDownloader {
});
const responseText = await response.text();
const dom = new NativeSimpleDOMParser(responseText);
const dom = this._createSimpleDOMParser(responseText);
for (const row of dom.getElementsByClassName('dc-result-row')) {
try {
const audio = dom.getElementByTagName('audio', row);
@ -149,7 +149,7 @@ class AudioDownloader {
});
const responseText = await response.text();
const dom = new NativeSimpleDOMParser(responseText);
const dom = this._createSimpleDOMParser(responseText);
try {
const audio = dom.getElementById(`audio_${expression}:${reading}`);
if (audio !== null) {
@ -237,4 +237,12 @@ class AudioDownloader {
_arrayBufferToBase64(arrayBuffer) {
return btoa(String.fromCharCode(...new Uint8Array(arrayBuffer)));
}
_createSimpleDOMParser(content) {
if (NativeSimpleDOMParser.isSupported()) {
return new NativeSimpleDOMParser(content);
} else {
throw new Error('DOM parsing not supported');
}
}
}

View File

@ -43,4 +43,8 @@ class NativeSimpleDOMParser {
getTextContent(element) {
return element.textContent;
}
static isSupported() {
return typeof DOMParser !== 'undefined';
}
}