aboutsummaryrefslogtreecommitdiff
path: root/common/ajax.js
diff options
context:
space:
mode:
authorjahoti <jahoti@tilde.team>2021-07-21 00:00:00 +0000
committerjahoti <jahoti@tilde.team>2021-07-21 00:00:00 +0000
commitefce4e9807889e9269534b19c8e0cbb4df527ecd (patch)
tree0a951d1b2b5c1470f7baf2be41585f5f08a09a9a /common/ajax.js
parentefd6ae83e6e48008988a2a11ac1658ec71dc82d2 (diff)
parentc483ae19e110ef5c1e539883a38fbc79b3dd4e4e (diff)
downloadbrowser-extension-efce4e9807889e9269534b19c8e0cbb4df527ecd.tar.gz
browser-extension-efce4e9807889e9269534b19c8e0cbb4df527ecd.zip
Merge remote-tracking branch 'origin/koszko' into jahoti
Diffstat (limited to 'common/ajax.js')
-rw-r--r--common/ajax.js39
1 files changed, 39 insertions, 0 deletions
diff --git a/common/ajax.js b/common/ajax.js
new file mode 100644
index 0000000..8082bbe
--- /dev/null
+++ b/common/ajax.js
@@ -0,0 +1,39 @@
+/**
+ * part of Hachette
+ * Wrapping XMLHttpRequest into a Promise.
+ *
+ * Copyright (C) 2021 Wojtek Kosior
+ * Redistribution terms are gathered in the `copyright' file.
+ */
+
+function ajax_callback()
+{
+ if (this.readyState == 4)
+ this.resolve_callback(this);
+}
+
+function initiate_ajax_request(resolve, reject, method, url)
+{
+ const xhttp = new XMLHttpRequest();
+ xhttp.resolve_callback = resolve;
+ xhttp.onreadystatechange = ajax_callback;
+ xhttp.open(method, url, true);
+ try {
+ xhttp.send();
+ } catch(e) {
+ console.log(e);
+ setTimeout(reject, 0);
+ }
+}
+
+function make_ajax_request(method, url)
+{
+ return new Promise((resolve, reject) =>
+ initiate_ajax_request(resolve, reject, method, url));
+}
+
+/*
+ * EXPORTS_START
+ * EXPORT make_ajax_request
+ * EXPORTS_END
+ */