diff options
author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2020-12-14 23:05:04 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-14 23:05:04 -0500 |
commit | 26dd0270d9e8667237b4875ffce4b0c5a6a42621 (patch) | |
tree | 3cb23f3d8789d440a0b8de158bb5a33ae46ea66c | |
parent | 7926821c48ad512b0c9bd2420216a36fe2237e4f (diff) |
Create function to create a new DOM parser (#1114)
-rw-r--r-- | ext/bg/js/audio-downloader.js | 12 | ||||
-rw-r--r-- | ext/bg/js/native-simple-dom-parser.js | 4 |
2 files changed, 14 insertions, 2 deletions
diff --git a/ext/bg/js/audio-downloader.js b/ext/bg/js/audio-downloader.js index ddddfe18..839eab7b 100644 --- a/ext/bg/js/audio-downloader.js +++ b/ext/bg/js/audio-downloader.js @@ -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'); + } + } } diff --git a/ext/bg/js/native-simple-dom-parser.js b/ext/bg/js/native-simple-dom-parser.js index 2aa4134d..4e0d89ea 100644 --- a/ext/bg/js/native-simple-dom-parser.js +++ b/ext/bg/js/native-simple-dom-parser.js @@ -43,4 +43,8 @@ class NativeSimpleDOMParser { getTextContent(element) { return element.textContent; } + + static isSupported() { + return typeof DOMParser !== 'undefined'; + } } |