diff options
author | Pierre Ossman <ossman@cendio.se> | 2020-12-04 16:43:44 +0100 |
---|---|---|
committer | Pierre Ossman <ossman@cendio.se> | 2020-12-04 16:43:44 +0100 |
commit | 6cd9bacf8bbf1c87c9e7421aad61e792c6b10caa (patch) | |
tree | 01c635340007868241bc57b77b96262ea63183d8 | |
parent | 273acf3e899f70ab71b5cf53854877ddd1595ac4 (diff) | |
download | novnc-6cd9bacf8bbf1c87c9e7421aad61e792c6b10caa.tar.gz |
Use Fetch API for getting JSON data
We no longer need to support Internet Explorer so we can use a more
proper API here.
-rw-r--r-- | app/ui.js | 16 | ||||
-rw-r--r-- | app/webutil.js | 32 |
2 files changed, 14 insertions, 34 deletions
@@ -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 52eab2f..a9fee32 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(); - }); -} |