]> git.proxmox.com Git - mirror_novnc.git/commitdiff
Use Fetch API for getting JSON data
authorPierre Ossman <ossman@cendio.se>
Fri, 4 Dec 2020 15:43:44 +0000 (16:43 +0100)
committerPierre Ossman <ossman@cendio.se>
Fri, 4 Dec 2020 15:43:44 +0000 (16:43 +0100)
We no longer need to support Internet Explorer so we can use a more
proper API here.

app/ui.js
app/webutil.js

index c70743dcd24e42deb7e0b0fcd42393ec23ac8b0c..fd3d036b3e2c9b407ad55d7c401a21796c22aa03 100644 (file)
--- a/app/ui.js
+++ b/app/ui.js
@@ -61,7 +61,13 @@ const UI = {
         // Translate the DOM
         l10n.translateDOM();
 
-        WebUtil.fetchJSON('./package.json')
+        fetch('./package.json')
+            .then((response) => {
+                if (!response.ok) {
+                    throw Error("" + response.status + " " + response.statusText);
+                }
+                return response.json();
+            })
             .then((packageInfo) => {
                 Array.from(document.getElementsByClassName('noVNC_version')).forEach(el => el.innerText = packageInfo.version);
             })
@@ -1699,7 +1705,13 @@ l10n.setup(LINGUAS);
 if (l10n.language === "en" || l10n.dictionary !== undefined) {
     UI.prime();
 } else {
-    WebUtil.fetchJSON('app/locale/' + l10n.language + '.json')
+    fetch('app/locale/' + l10n.language + '.json')
+        .then((response) => {
+            if (!response.ok) {
+                throw Error("" + response.status + " " + response.statusText);
+            }
+            return response.json();
+        })
         .then((translations) => { l10n.dictionary = translations; })
         .catch(err => Log.Error("Failed to load translations: " + err))
         .then(UI.prime);
index 52eab2fb6fe7ae21398ba71d95b45b27bbc9bb42..a9fee3224682a27ab3ffa98e45bd40224cb11aed 100644 (file)
@@ -175,35 +175,3 @@ export function eraseSetting(name) {
         localStorage.removeItem(name);
     }
 }
-
-// sadly, we can't use the Fetch API until we decide to drop
-// IE11 support or polyfill promises and fetch in IE11.
-// resolve will receive an object on success, while reject
-// will receive either an event or an error on failure.
-export function fetchJSON(path) {
-    return new Promise((resolve, reject) => {
-        // NB: IE11 doesn't support JSON as a responseType
-        const req = new XMLHttpRequest();
-        req.open('GET', path);
-
-        req.onload = () => {
-            if (req.status === 200) {
-                let resObj;
-                try {
-                    resObj = JSON.parse(req.responseText);
-                } catch (err) {
-                    reject(err);
-                }
-                resolve(resObj);
-            } else {
-                reject(new Error("XHR got non-200 status while trying to load '" + path + "': " + req.status));
-            }
-        };
-
-        req.onerror = evt => reject(new Error("XHR encountered an error while trying to load '" + path + "': " + evt.message));
-
-        req.ontimeout = evt => reject(new Error("XHR timed out while trying to load '" + path + "'"));
-
-        req.send();
-    });
-}