aboutsummaryrefslogtreecommitdiff
path: root/ext/bg
diff options
context:
space:
mode:
Diffstat (limited to 'ext/bg')
-rw-r--r--ext/bg/js/audio-uri-builder.js41
-rw-r--r--ext/bg/js/request.js42
2 files changed, 47 insertions, 36 deletions
diff --git a/ext/bg/js/audio-uri-builder.js b/ext/bg/js/audio-uri-builder.js
index 27e97680..7dd5e4c6 100644
--- a/ext/bg/js/audio-uri-builder.js
+++ b/ext/bg/js/audio-uri-builder.js
@@ -82,16 +82,24 @@ class AudioUriBuilder {
}
async _getUriJpod101Alternate(definition) {
- const response = 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)}`);
+ 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 response.text();
+ console.log(responseText);
- const dom = new DOMParser().parseFromString(response, 'text/html');
+ const dom = new DOMParser().parseFromString(responseText, 'text/html');
for (const row of dom.getElementsByClassName('dc-result-row')) {
try {
const url = row.querySelector('audio>source[src]').getAttribute('src');
@@ -108,15 +116,18 @@ class AudioUriBuilder {
}
async _getUriJisho(definition) {
- const response = 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 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 response.text();
- const dom = new DOMParser().parseFromString(response, 'text/html');
+ const dom = new DOMParser().parseFromString(responseText, 'text/html');
try {
const audio = dom.getElementById(`audio_${definition.expression}:${definition.reading}`);
if (audio !== null) {
diff --git a/ext/bg/js/request.js b/ext/bg/js/request.js
index 957ac0f5..d1c6ed4e 100644
--- a/ext/bg/js/request.js
+++ b/ext/bg/js/request.js
@@ -16,28 +16,28 @@
*/
-function requestText(url, action, params) {
- return new Promise((resolve, reject) => {
- const xhr = new XMLHttpRequest();
- xhr.overrideMimeType('text/plain');
- xhr.addEventListener('load', () => resolve(xhr.responseText));
- xhr.addEventListener('error', () => reject(new Error('Failed to connect')));
- xhr.open(action, url);
- if (params) {
- xhr.send(JSON.stringify(params));
- } else {
- xhr.send();
- }
+async function requestText(url, method, data) {
+ const response = await fetch(url, {
+ method,
+ mode: 'no-cors',
+ cache: 'default',
+ credentials: 'omit',
+ redirect: 'follow',
+ referrerPolicy: 'no-referrer',
+ body: (data ? JSON.stringify(data) : void 0)
});
+ return await response.text();
}
-async function requestJson(url, action, params) {
- const responseText = await requestText(url, action, params);
- try {
- return JSON.parse(responseText);
- } catch (e) {
- const error = new Error(`Invalid response (${e.message || e})`);
- error.data = {url, action, params, responseText};
- throw error;
- }
+async function requestJson(url, method, data) {
+ const response = await fetch(url, {
+ method,
+ mode: 'no-cors',
+ cache: 'default',
+ credentials: 'omit',
+ redirect: 'follow',
+ referrerPolicy: 'no-referrer',
+ body: (data ? JSON.stringify(data) : void 0)
+ });
+ return await response.json();
}