summaryrefslogtreecommitdiff
path: root/ext/bg/js/request.js
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2020-06-13 10:23:04 -0400
committerGitHub <noreply@github.com>2020-06-13 10:23:04 -0400
commit8a7ff6a18c78bbc2048dd4017597ccc4f5ee4106 (patch)
tree58333fc1a5a3da13180e8d0bbd5e65d79b34fa27 /ext/bg/js/request.js
parent5cba421201b596f4430dd45f3715ad515661b109 (diff)
Replace XMLHttpRequest (#562)
* Replace XMLHttpRequest with fetch * Implement fetch placeholder for tests
Diffstat (limited to 'ext/bg/js/request.js')
-rw-r--r--ext/bg/js/request.js42
1 files changed, 21 insertions, 21 deletions
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();
}