diff options
author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2020-08-02 13:30:55 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-08-02 13:30:55 -0400 |
commit | b1b33f8beb26f97d91cb282682472c65f6eccae8 (patch) | |
tree | 1a7b5027a0f5208135f856759cb7935beac7911a /ext/bg/js/audio-uri-builder.js | |
parent | a562a1149808b49f60346c82409f292290fbdd77 (diff) |
Fix fetch requests (#708)
* Revert audio fetching functionality to use XMLHttpRequest
* Replace requestJson
* Replace requestJson
* Replace requestJson
* Replace requestJson and requestText
* Fix tests
* Include support for vulgar word searches
* Remove request.js
Diffstat (limited to 'ext/bg/js/audio-uri-builder.js')
-rw-r--r-- | ext/bg/js/audio-uri-builder.js | 36 |
1 files changed, 13 insertions, 23 deletions
diff --git a/ext/bg/js/audio-uri-builder.js b/ext/bg/js/audio-uri-builder.js index 390e1e4d..11738ef3 100644 --- a/ext/bg/js/audio-uri-builder.js +++ b/ext/bg/js/audio-uri-builder.js @@ -82,21 +82,14 @@ class AudioUriBuilder { } async _getUriJpod101Alternate(definition) { - const fetchUrl = 'https://www.japanesepod101.com/learningcenter/reference/dictionary_post'; - const data = `post=dictionary_reference&match_type=exact&search_query=${encodeURIComponent(definition.expression)}`; - const response = await fetch(fetchUrl, { - method: 'POST', - mode: 'no-cors', - cache: 'default', - credentials: 'omit', - redirect: 'follow', - referrerPolicy: 'no-referrer', - headers: { - 'Content-Type': 'application/x-www-form-urlencoded' - }, - body: data + const responseText = await new Promise((resolve, reject) => { + const xhr = new XMLHttpRequest(); + xhr.open('POST', 'https://www.japanesepod101.com/learningcenter/reference/dictionary_post'); + xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); + xhr.addEventListener('error', () => reject(new Error('Failed to scrape audio data'))); + xhr.addEventListener('load', () => resolve(xhr.responseText)); + xhr.send(`post=dictionary_reference&match_type=exact&search_query=${encodeURIComponent(definition.expression)}&vulgar=true`); }); - const responseText = await response.text(); const dom = new DOMParser().parseFromString(responseText, 'text/html'); for (const row of dom.getElementsByClassName('dc-result-row')) { @@ -115,16 +108,13 @@ class AudioUriBuilder { } async _getUriJisho(definition) { - const fetchUrl = `https://jisho.org/search/${definition.expression}`; - const response = await fetch(fetchUrl, { - method: 'GET', - mode: 'no-cors', - cache: 'default', - credentials: 'omit', - redirect: 'follow', - referrerPolicy: 'no-referrer' + const responseText = await new Promise((resolve, reject) => { + const xhr = new XMLHttpRequest(); + xhr.open('GET', `https://jisho.org/search/${definition.expression}`); + xhr.addEventListener('error', () => reject(new Error('Failed to scrape audio data'))); + xhr.addEventListener('load', () => resolve(xhr.responseText)); + xhr.send(); }); - const responseText = await response.text(); const dom = new DOMParser().parseFromString(responseText, 'text/html'); try { |