aboutsummaryrefslogtreecommitdiff
path: root/ext/bg/js/backend.js
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2020-08-02 13:30:55 -0400
committerGitHub <noreply@github.com>2020-08-02 13:30:55 -0400
commitb1b33f8beb26f97d91cb282682472c65f6eccae8 (patch)
tree1a7b5027a0f5208135f856759cb7935beac7911a /ext/bg/js/backend.js
parenta562a1149808b49f60346c82409f292290fbdd77 (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/backend.js')
-rw-r--r--ext/bg/js/backend.js29
1 files changed, 20 insertions, 9 deletions
diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js
index 2e772aa1..9e7ac76a 100644
--- a/ext/bg/js/backend.js
+++ b/ext/bg/js/backend.js
@@ -35,8 +35,6 @@
* jp
* profileConditionsDescriptor
* profileConditionsDescriptorPromise
- * requestJson
- * requestText
*/
class Backend {
@@ -199,8 +197,8 @@ class Backend {
await profileConditionsDescriptorPromise;
- this._optionsSchema = await requestJson(chrome.runtime.getURL('/bg/data/options-schema.json'), 'GET');
- this._defaultAnkiFieldTemplates = (await requestText(chrome.runtime.getURL('/bg/data/default-anki-field-templates.handlebars'), 'GET')).trim();
+ this._optionsSchema = await this._fetchAsset('/bg/data/options-schema.json', true);
+ this._defaultAnkiFieldTemplates = (await this._fetchAsset('/bg/data/default-anki-field-templates.handlebars')).trim();
this._options = await OptionsUtil.load();
this._options = JsonSchema.getValidValueOrDefault(this._optionsSchema, this._options);
@@ -615,7 +613,7 @@ class Backend {
if (!url.startsWith('/') || url.startsWith('//') || !url.endsWith('.css')) {
throw new Error('Invalid URL');
}
- return await requestText(url, 'GET');
+ return await this._fetchAsset(url);
}
_onApiGetEnvironmentInfo() {
@@ -653,13 +651,11 @@ class Backend {
}
async _onApiGetDisplayTemplatesHtml() {
- const url = chrome.runtime.getURL('/mixed/display-templates.html');
- return await requestText(url, 'GET');
+ return await this._fetchAsset('/mixed/display-templates.html');
}
async _onApiGetQueryParserTemplatesHtml() {
- const url = chrome.runtime.getURL('/bg/query-parser-templates.html');
- return await requestText(url, 'GET');
+ return await this._fetchAsset('/bg/query-parser-templates.html');
}
_onApiGetZoom(params, sender) {
@@ -1522,4 +1518,19 @@ class Backend {
}
});
}
+
+ async _fetchAsset(url, json=false) {
+ const response = await fetch(chrome.runtime.getURL(url), {
+ method: 'GET',
+ mode: 'no-cors',
+ cache: 'default',
+ credentials: 'omit',
+ redirect: 'follow',
+ referrerPolicy: 'no-referrer'
+ });
+ if (!response.ok) {
+ throw new Error(`Failed to fetch ${url}: ${response.status}`);
+ }
+ return await (json ? response.json() : response.text());
+ }
}