From c2fe2b980f63a816c9ad0541343fa047e7072d67 Mon Sep 17 00:00:00 2001 From: rift <117926989+Riftriot@users.noreply.github.com> Date: Sun, 7 Jan 2024 16:59:28 -0600 Subject: [PATCH] Validate that bare servers can actually function and fix CSS. --- src/locales/en.json | 3 +- src/locales/es.json | 3 +- src/locales/ja.json | 3 +- src/pages/Settings/BareInput.tsx | 56 +++++++++++++++++++--------- src/pages/Settings/BareTest.tsx | 29 ++++++++++++++ src/pages/Settings/Customization.tsx | 2 +- src/pages/Settings/Proxy.tsx | 8 ++-- 7 files changed, 78 insertions(+), 26 deletions(-) create mode 100644 src/pages/Settings/BareTest.tsx diff --git a/src/locales/en.json b/src/locales/en.json index 1371fcf..75a052d 100644 --- a/src/locales/en.json +++ b/src/locales/en.json @@ -75,5 +75,6 @@ "catppuccinFrappe": "Catppuccin Frappe", "catppuccinLatte": "Catppuccin Latte" }, - "clipboard": "URL copied to clipboard!" + "clipboard": "URL copied to clipboard!", + "bareError": "That didn't quite work. Double check that the bare server exists and isn't blocked." } diff --git a/src/locales/es.json b/src/locales/es.json index 3a4dfb1..bc61258 100644 --- a/src/locales/es.json +++ b/src/locales/es.json @@ -74,5 +74,6 @@ "catppuccinFrappe": "Catppuccin Frappe", "catppuccinLatte": "Catppuccin Latte" }, - "clipboard": "¡URL copiada al portapapeles!" + "clipboard": "¡URL copiada al portapapeles!", + "bareError": "Esto no es funcional. Vuelva a verificar que el servidor bare exista y no esté bloqueado." } diff --git a/src/locales/ja.json b/src/locales/ja.json index 6527cc4..c929ba5 100644 --- a/src/locales/ja.json +++ b/src/locales/ja.json @@ -74,5 +74,6 @@ "catppuccinFrappe": "Catppuccin Frappe", "catppuccinLatte": "Catppuccin Latte" }, - "clipboard": "URL がクリップボードにコピーされました!" + "clipboard": "URL がクリップボードにコピーされました!", + "bareError": "それはうまくいきませんでした。Bare サーバーが存在し、ブロックされていないことを再確認してください。" } diff --git a/src/pages/Settings/BareInput.tsx b/src/pages/Settings/BareInput.tsx index 4a46769..17364f0 100644 --- a/src/pages/Settings/BareInput.tsx +++ b/src/pages/Settings/BareInput.tsx @@ -2,6 +2,8 @@ import { useState, useEffect } from "preact/hooks"; import { set } from "../../util/IDB"; import { uninstallServiceWorkers } from "../../util/SWHelper"; import { useTranslation } from "react-i18next"; +import { ToastContainer, toast } from "react-toastify"; +import { BareTest } from "./BareTest"; interface BareInputProps { placeholder: string; @@ -31,26 +33,44 @@ function BareInput(props: BareInputProps) { return finalUrl; } function handleChange() { - const url = validateUrl((document.getElementById("input") as HTMLInputElement).value); - setInputValue((document.getElementById("input") as HTMLInputElement).value); - set(props.storageKey, url); - localStorage.setItem(props.storageKey, url); - uninstallServiceWorkers(); - window.location.reload(); + const url = validateUrl( + (document.getElementById("input") as HTMLInputElement).value + ); + BareTest(url + "v3/").then((result) => { + if (result) { + setInputValue( + (document.getElementById("input") as HTMLInputElement).value + ); + set(props.storageKey, url); + localStorage.setItem(props.storageKey, url); + uninstallServiceWorkers(); + window.location.reload(); + } else { + (document.getElementById("input") as HTMLInputElement).value = + localStorage.getItem("bare") || "/bare/"; + toast(t("bareError"), { + type: "error" + }); + } + }); } return ( -
- -
- {t("settings.bare.select")} +
+ +
+ +
+ {t("settings.bare.select")} +
); diff --git a/src/pages/Settings/BareTest.tsx b/src/pages/Settings/BareTest.tsx new file mode 100644 index 0000000..127c2e3 --- /dev/null +++ b/src/pages/Settings/BareTest.tsx @@ -0,0 +1,29 @@ +export function BareTest(bareUrl) { + const headers = new Headers({ + "x-bare-url": "https://www.google.com", + "X-Bare-Headers": JSON.stringify({ + Accept: + "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9" + }) + }); + + return fetch(bareUrl, { + method: "GET", + headers: headers + }) + .then((response) => { + if ( + response.headers.get("x-bare-status") === "200" || + response.headers.get("x-bare-status") === "302" + ) { + return true; + } else { + // the site is a real site but doesn't act like a bare server + return false; + } + }) + .catch((error) => { + // incase the site doesn't exist + return false; + }); +} diff --git a/src/pages/Settings/Customization.tsx b/src/pages/Settings/Customization.tsx index 2602700..0def36e 100644 --- a/src/pages/Settings/Customization.tsx +++ b/src/pages/Settings/Customization.tsx @@ -27,7 +27,7 @@ function Customization({ id, active }) { variants={settingsPageVariant} className="content-card flex w-full flex-col items-center justify-center text-center" > -
+
{t("settings.theme.title")}
diff --git a/src/pages/Settings/Proxy.tsx b/src/pages/Settings/Proxy.tsx index 25edd95..dc1df56 100644 --- a/src/pages/Settings/Proxy.tsx +++ b/src/pages/Settings/Proxy.tsx @@ -39,7 +39,7 @@ const Proxy = ({ id, active }) => { variants={settingsPageVariant} className="content-card flex w-full flex-row flex-wrap justify-center gap-4" > -
+
{t("settings.proxy.title")}
@@ -48,7 +48,7 @@ const Proxy = ({ id, active }) => {
-
+
{t("settings.proxymodes.title")}
@@ -61,7 +61,7 @@ const Proxy = ({ id, active }) => { refresh={false} />
-
+
{t("settings.search.title")}
@@ -74,7 +74,7 @@ const Proxy = ({ id, active }) => { refresh={false} />
-
+
{t("settings.bare.title")}