From 558ef98614b8ad34ba4fb35a8f0b6fe94211f938 Mon Sep 17 00:00:00 2001 From: Jan200101 Date: Sun, 4 Aug 2024 14:27:54 +0200 Subject: handle onLine event dynamically, don't enable buttons when going offline --- src/app/js/browser.js | 1 - src/app/js/request.js | 12 +++++++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/app/js/browser.js b/src/app/js/browser.js index 3415ebf..c559031 100644 --- a/src/app/js/browser.js +++ b/src/app/js/browser.js @@ -524,7 +524,6 @@ ipcRenderer.on("legacy-duped-mod", (_, modname) => { }) ipcRenderer.on("no-internet", () => { - set_buttons(true); toasts.show({ timeout: 10000, scheme: "error", diff --git a/src/app/js/request.js b/src/app/js/request.js index 29b8883..aeea35a 100644 --- a/src/app/js/request.js +++ b/src/app/js/request.js @@ -1,10 +1,16 @@ const ipcRenderer = require("electron").ipcRenderer; -// show a toast message if no Internet connection has been detected. -if (! navigator.onLine) { - ipcRenderer.send("no-internet"); +const updateOnlineStatus = () => { + // show a toast message if no Internet connection has been detected. + if (!navigator.onLine) { + ipcRenderer.send("no-internet"); + } } +window.addEventListener('online', updateOnlineStatus); +window.addEventListener('offline', updateOnlineStatus); +updateOnlineStatus(); + // invokes `requests.get()` from `src/modules/requests.js` through the // main process, and returns the output let request = async (...args) => { -- cgit v1.2.3 From 554fe88942135583404ddf8c1a97f342287cea5a Mon Sep 17 00:00:00 2001 From: 0neGal Date: Fri, 20 Dec 2024 18:33:38 +0100 Subject: minor syntax changes --- src/app/js/request.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/app/js/request.js b/src/app/js/request.js index aeea35a..cc73f1b 100644 --- a/src/app/js/request.js +++ b/src/app/js/request.js @@ -1,15 +1,15 @@ const ipcRenderer = require("electron").ipcRenderer; -const updateOnlineStatus = () => { +let update_status = () => { // show a toast message if no Internet connection has been detected. - if (!navigator.onLine) { + if (! navigator.onLine) { ipcRenderer.send("no-internet"); } } -window.addEventListener('online', updateOnlineStatus); -window.addEventListener('offline', updateOnlineStatus); -updateOnlineStatus(); +window.addEventListener("online", update_status); +window.addEventListener("offline", update_status); +update_status(); // invokes `requests.get()` from `src/modules/requests.js` through the // main process, and returns the output -- cgit v1.2.3 From 1be8c774251c3a1e1e3559f628aa24b465ab8685 Mon Sep 17 00:00:00 2001 From: 0neGal Date: Fri, 20 Dec 2024 18:39:57 +0100 Subject: added offline indicator icon --- src/app/icons/offline.png | Bin 0 -> 2707 bytes src/app/index.html | 3 +++ src/app/js/request.js | 5 ++++- src/app/main.css | 8 +++++++- src/lang/en.json | 1 + 5 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 src/app/icons/offline.png diff --git a/src/app/icons/offline.png b/src/app/icons/offline.png new file mode 100644 index 0000000..a9d2f1f Binary files /dev/null and b/src/app/icons/offline.png differ diff --git a/src/app/index.html b/src/app/index.html index 98c59d4..f9d3d51 100644 --- a/src/app/index.html +++ b/src/app/index.html @@ -14,6 +14,9 @@
+
diff --git a/src/app/js/request.js b/src/app/js/request.js index cc73f1b..b9b5081 100644 --- a/src/app/js/request.js +++ b/src/app/js/request.js @@ -1,9 +1,12 @@ const ipcRenderer = require("electron").ipcRenderer; let update_status = () => { - // show a toast message if no Internet connection has been detected. + // if offline, show toast and offline icon if (! navigator.onLine) { ipcRenderer.send("no-internet"); + offline.classList.remove("hidden"); + } else { // remove offline icon + offline.classList.add("hidden"); } } diff --git a/src/app/main.css b/src/app/main.css index 9ade423..b6b0ac7 100644 --- a/src/app/main.css +++ b/src/app/main.css @@ -59,8 +59,14 @@ button:active {filter: brightness(90%)} margin-right: calc(var(--padding) / 2); } +#winbtns div.hidden { + width: 0px; + opacity: 0.0; + margin-right: 0px; + pointer-events: none; +} + #winbtns div img { - width: 100%; height: 100%; transition: transform 0.25s ease-in-out; } diff --git a/src/lang/en.json b/src/lang/en.json index 5d622e2..28e4ecb 100644 --- a/src/lang/en.json +++ b/src/lang/en.json @@ -342,6 +342,7 @@ "close": "Close Viper", "minimize": "Minimize Viper", "settings": "Settings", + "offline": "Internet Offline", "pages": { "viper": "Viper", "northstar": "Northstar", -- cgit v1.2.3 From 010126567cca3a02073d71a29030ba86d96c195f Mon Sep 17 00:00:00 2001 From: 0neGal Date: Fri, 20 Dec 2024 19:03:41 +0100 Subject: added requests.check() and uses of it Very minimal uses of it, currently it simply checks every 30s and on startup whether a set of domains/endpoints we use work, and if all fail, then we assume something is wrong with the internet. --- src/app/js/request.js | 61 +++++++++++++++++++++++++++++++++++++++++++++---- src/modules/requests.js | 48 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 105 insertions(+), 4 deletions(-) diff --git a/src/app/js/request.js b/src/app/js/request.js index b9b5081..a8d4dd3 100644 --- a/src/app/js/request.js +++ b/src/app/js/request.js @@ -1,18 +1,27 @@ const ipcRenderer = require("electron").ipcRenderer; +let sent_error_toast = false; + let update_status = () => { // if offline, show toast and offline icon if (! navigator.onLine) { - ipcRenderer.send("no-internet"); + if (! sent_error_toast) { + sent_error_toast = true; + ipcRenderer.send("no-internet"); + } + offline.classList.remove("hidden"); - } else { // remove offline icon - offline.classList.add("hidden"); + return; } + + // remove offline icon + offline.classList.add("hidden"); + sent_error_toast = false; } +update_status(); window.addEventListener("online", update_status); window.addEventListener("offline", update_status); -update_status(); // invokes `requests.get()` from `src/modules/requests.js` through the // main process, and returns the output @@ -20,8 +29,52 @@ let request = async (...args) => { return await ipcRenderer.invoke("request", ...args); } +// invokes `requests.check()` from `src/modules/requests.js` through the +// main process, and returns the output +request.check = async (...args) => { + return await ipcRenderer.invoke("request-check", ...args); +} + request.delete_cache = () => { ipcRenderer.send("delete-request-cache"); } +// checks a list of endpoints/domains we need to be functioning for a +// lot of the WAN functionality +let check_endpoints = async () => { + // if we're not online according to the navigator, it's highly + // unlikely the endpoints will succeed + if (! navigator.onLine) { + if (! sent_error_toast) { + sent_error_toast = true; + ipcRenderer.send("no-internet"); + } + + return offline.classList.remove("hidden"); + } + + // check endpoints + let status = await request.check([ + "https://github.com", + "https://northstar.tf", + "https://thunderstore.io" + ]) + + // if just 1 endpoint succeeded, we probably have internet + if (status.succeeded.length) { + sent_error_toast = false; + offline.classList.add("hidden"); + } else { // no endpoint succeeded! + if (! sent_error_toast) { + sent_error_toast = true; + ipcRenderer.send("no-internet"); + } + + offline.classList.remove("hidden"); + } +}; check_endpoints(); + +// check endpoints every 30 seconds +setInterval(check_endpoints, 30000); + module.exports = request; diff --git a/src/modules/requests.js b/src/modules/requests.js index dac3a4f..ffa57c6 100644 --- a/src/modules/requests.js +++ b/src/modules/requests.js @@ -25,6 +25,15 @@ ipcMain.handle("request", async (e, ...args) => { return res; }) +ipcMain.handle("request-check", async (_, ...args) => { + let res = false; + + try { + res = await requests.check(...args); + }catch(err) {} + + return res; +}) // updates `cache_dir` and `cache_file` function set_paths() { @@ -244,4 +253,43 @@ requests.get = (host, path, cache_key, ignore_max_time_when_offline = true, max_ }) } +// checks whether a list of `endpoints` can be contacted +requests.check = async (endpoints) => { + // turn `endpoints` into an array, if it isn't already + if (typeof endpoints == "string") { + endpoints = [endpoints]; + } + + // list of what failed and succeeded, will be returned later + let res = { + failed: [], + succeeded: [] + } + + // run through all the endpoints + for (let endpoint of endpoints) { + let req; + + // attempt to do a request + try { + req = await fetch(endpoint); + } catch(err) { // something went wrong! + res.failed.push(endpoint); + continue; + } + + // if we're within the `200-299` response code range, we + // consider it a success + if (req.status < 300 && req.status >= 200) { + res.succeeded.push(endpoint); + continue; + } + + // we failed! + res.failed.push(endpoint); + } + + return res; +} + module.exports = requests; -- cgit v1.2.3 From ada511f0c2f86a0a28e9d2129a1ebdd1edb19f3d Mon Sep 17 00:00:00 2001 From: 0neGal Date: Fri, 20 Dec 2024 19:11:56 +0100 Subject: simplify checking offline state --- src/app/js/request.js | 70 +++++++++++++++++++++------------------------------ 1 file changed, 29 insertions(+), 41 deletions(-) diff --git a/src/app/js/request.js b/src/app/js/request.js index a8d4dd3..d0ef39b 100644 --- a/src/app/js/request.js +++ b/src/app/js/request.js @@ -1,28 +1,5 @@ const ipcRenderer = require("electron").ipcRenderer; -let sent_error_toast = false; - -let update_status = () => { - // if offline, show toast and offline icon - if (! navigator.onLine) { - if (! sent_error_toast) { - sent_error_toast = true; - ipcRenderer.send("no-internet"); - } - - offline.classList.remove("hidden"); - return; - } - - // remove offline icon - offline.classList.add("hidden"); - sent_error_toast = false; -} - -update_status(); -window.addEventListener("online", update_status); -window.addEventListener("offline", update_status); - // invokes `requests.get()` from `src/modules/requests.js` through the // main process, and returns the output let request = async (...args) => { @@ -39,18 +16,39 @@ request.delete_cache = () => { ipcRenderer.send("delete-request-cache"); } +// keeps track of whether we've already sent a toast since we last went +// offline, to prevent multiple toasts +let sent_error_toast = false; + +// shows or hides offline icon, and shows toast depending on `is_online` +let state_action = (is_online) => { + if (is_online) { + // hide offline icon + sent_error_toast = false; + offline.classList.add("hidden"); + } else { + // show toast + if (! sent_error_toast) { + sent_error_toast = true; + ipcRenderer.send("no-internet"); + } + + // show offline icon + offline.classList.remove("hidden"); + } +} + +state_action(navigator.onLine); +window.addEventListener("online", () => state_action(navigator.onLine)); +window.addEventListener("offline", () => state_action(navigator.onLine)); + // checks a list of endpoints/domains we need to be functioning for a // lot of the WAN functionality let check_endpoints = async () => { // if we're not online according to the navigator, it's highly // unlikely the endpoints will succeed if (! navigator.onLine) { - if (! sent_error_toast) { - sent_error_toast = true; - ipcRenderer.send("no-internet"); - } - - return offline.classList.remove("hidden"); + return state_action(false); } // check endpoints @@ -60,18 +58,8 @@ let check_endpoints = async () => { "https://thunderstore.io" ]) - // if just 1 endpoint succeeded, we probably have internet - if (status.succeeded.length) { - sent_error_toast = false; - offline.classList.add("hidden"); - } else { // no endpoint succeeded! - if (! sent_error_toast) { - sent_error_toast = true; - ipcRenderer.send("no-internet"); - } - - offline.classList.remove("hidden"); - } + // handle result of check + state_action(!! status.succeeded.length); }; check_endpoints(); // check endpoints every 30 seconds -- cgit v1.2.3 From 957e8a9ad0b7bfe33079ca2d6a5dc097fdc2daa9 Mon Sep 17 00:00:00 2001 From: 0neGal Date: Fri, 20 Dec 2024 19:29:48 +0100 Subject: if offline close browser and disable some buttons --- src/app/index.html | 6 +++--- src/app/js/browser.js | 4 ++-- src/app/js/mods.js | 2 +- src/app/js/request.js | 25 +++++++++++++++++++++++-- src/app/js/set_buttons.js | 12 +++++++----- 5 files changed, 36 insertions(+), 13 deletions(-) diff --git a/src/app/index.html b/src/app/index.html index f9d3d51..5421a3f 100644 --- a/src/app/index.html +++ b/src/app/index.html @@ -38,7 +38,7 @@