From 6cd9bacf8bbf1c87c9e7421aad61e792c6b10caa Mon Sep 17 00:00:00 2001 From: Pierre Ossman Date: Fri, 4 Dec 2020 16:43:44 +0100 Subject: [PATCH] Use Fetch API for getting JSON data We no longer need to support Internet Explorer so we can use a more proper API here. --- app/ui.js | 16 ++++++++++++++-- app/webutil.js | 32 -------------------------------- 2 files changed, 14 insertions(+), 34 deletions(-) diff --git a/app/ui.js b/app/ui.js index c70743dc..fd3d036b 100644 --- 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); diff --git a/app/webutil.js b/app/webutil.js index 52eab2fb..a9fee322 100644 --- a/app/webutil.js +++ b/app/webutil.js @@ -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(); - }); -}