summaryrefslogtreecommitdiff
path: root/ext/bg/js/util.js
diff options
context:
space:
mode:
Diffstat (limited to 'ext/bg/js/util.js')
-rw-r--r--ext/bg/js/util.js25
1 files changed, 25 insertions, 0 deletions
diff --git a/ext/bg/js/util.js b/ext/bg/js/util.js
index b8a60217..4f907923 100644
--- a/ext/bg/js/util.js
+++ b/ext/bg/js/util.js
@@ -428,6 +428,31 @@ function dictFieldFormat(field, definition, mode, options) {
return field;
}
+/*
+ * JSON
+ */
+
+function jsonRequest(url, action, params) {
+ return new Promise((resolve, reject) => {
+ const xhr = new XMLHttpRequest();
+ xhr.overrideMimeType('application/json');
+ xhr.addEventListener('load', () => resolve(xhr.responseText));
+ xhr.addEventListener('error', () => reject('failed to execute network request'));
+ xhr.open(action, url);
+ if (params) {
+ xhr.send(JSON.stringify(params));
+ } else {
+ xhr.send();
+ }
+ }).then(responseText => {
+ try {
+ return JSON.parse(responseText);
+ }
+ catch (e) {
+ return Promise.reject('invalid JSON response');
+ }
+ });
+}
/*
* Helpers