From 2b0ccbe44ca6877f4374f515dbd3f09a8e2605c0 Mon Sep 17 00:00:00 2001 From: Miodec Date: Mon, 30 Jan 2023 16:34:36 +0100 Subject: [PATCH] added islocalhost function to misc --- frontend/src/ts/controllers/account-controller.ts | 5 +---- frontend/src/ts/controllers/theme-controller.ts | 2 +- frontend/src/ts/elements/leaderboards.ts | 2 +- frontend/src/ts/elements/psa.ts | 4 ++-- frontend/src/ts/firebase.ts | 4 ++-- frontend/src/ts/popups/simple-popups.ts | 7 ++----- frontend/src/ts/ready.ts | 4 ++-- frontend/src/ts/ui.ts | 3 ++- frontend/src/ts/utils/misc.ts | 8 ++++++++ 9 files changed, 21 insertions(+), 18 deletions(-) diff --git a/frontend/src/ts/controllers/account-controller.ts b/frontend/src/ts/controllers/account-controller.ts index a73bf5ace..d67acd296 100644 --- a/frontend/src/ts/controllers/account-controller.ts +++ b/frontend/src/ts/controllers/account-controller.ts @@ -651,10 +651,7 @@ async function signUp(): Promise { } // Force user to use a capital letter, number, special character when setting up an account and changing password - if ( - window.location.hostname !== "localhost" && - !Misc.isPasswordStrong(password) - ) { + if (!Misc.isLocalhost() && !Misc.isPasswordStrong(password)) { Notifications.add( "Password must contain at least one capital letter, number, a special character and at least 8 characters long", 0, diff --git a/frontend/src/ts/controllers/theme-controller.ts b/frontend/src/ts/controllers/theme-controller.ts index 53b66dd5e..f9cc9912c 100644 --- a/frontend/src/ts/controllers/theme-controller.ts +++ b/frontend/src/ts/controllers/theme-controller.ts @@ -32,7 +32,7 @@ async function updateFavicon(size: number, curveSize: number): Promise { let maincolor, bgcolor; bgcolor = await ThemeColors.get("bg"); maincolor = await ThemeColors.get("main"); - if (window.location.hostname === "localhost") { + if (Misc.isLocalhost()) { const swap = maincolor; maincolor = bgcolor; bgcolor = swap; diff --git a/frontend/src/ts/elements/leaderboards.ts b/frontend/src/ts/elements/leaderboards.ts index 7f073d9bd..aef9c5439 100644 --- a/frontend/src/ts/elements/leaderboards.ts +++ b/frontend/src/ts/elements/leaderboards.ts @@ -139,7 +139,7 @@ function updateFooter(lb: LbKey): void { } if ( - window.location.hostname !== "localhost" && + !Misc.isLocalhost() && (DB.getSnapshot()?.typingStats?.timeTyping ?? 0) < 7200 ) { $(`#leaderboardsWrapper table.${side} tfoot`).html(` diff --git a/frontend/src/ts/elements/psa.ts b/frontend/src/ts/elements/psa.ts index 4a7067e09..7309597fe 100644 --- a/frontend/src/ts/elements/psa.ts +++ b/frontend/src/ts/elements/psa.ts @@ -1,5 +1,5 @@ import Ape from "../ape"; -import { secondsToString } from "../utils/misc"; +import { isLocalhost, secondsToString } from "../utils/misc"; import * as Notifications from "./notifications"; import format from "date-fns/format"; import * as Alerts from "./alerts"; @@ -21,7 +21,7 @@ function setMemory(id: string): void { async function getLatest(): Promise { const response = await Ape.psas.get(); if (response.status === 500) { - if (window.location.hostname === "localhost") { + if (isLocalhost()) { Notifications.addBanner( "Dev Info: Backend server not running", 0, diff --git a/frontend/src/ts/firebase.ts b/frontend/src/ts/firebase.ts index 0802bb84e..81c7c8418 100644 --- a/frontend/src/ts/firebase.ts +++ b/frontend/src/ts/firebase.ts @@ -3,7 +3,7 @@ import { FirebaseApp, initializeApp } from "firebase/app"; import { getAuth, Auth as AuthType } from "firebase/auth"; import { firebaseConfig } from "./constants/firebase-config"; // eslint-disable-line require-path-exists/exists import * as Notifications from "./elements/notifications"; -import { createErrorMessage } from "./utils/misc"; +import { createErrorMessage, isLocalhost } from "./utils/misc"; // Initialize Firebase export let app: FirebaseApp | undefined; @@ -16,7 +16,7 @@ try { app = undefined; Auth = undefined; console.error("Authentication failed to initialize", e); - if (window.location.hostname === "localhost") { + if (isLocalhost()) { Notifications.addBanner( createErrorMessage(e, "Authentication uninitialized") + " Check your firebase-config.ts", diff --git a/frontend/src/ts/popups/simple-popups.ts b/frontend/src/ts/popups/simple-popups.ts index 975b220c8..cc29a3ec1 100644 --- a/frontend/src/ts/popups/simple-popups.ts +++ b/frontend/src/ts/popups/simple-popups.ts @@ -20,7 +20,7 @@ import { unlink, updatePassword, } from "firebase/auth"; -import { isElementVisible, isPasswordStrong } from "../utils/misc"; +import { isElementVisible, isLocalhost, isPasswordStrong } from "../utils/misc"; import * as CustomTextState from "../states/custom-text-name"; import * as Skeleton from "./skeleton"; @@ -535,10 +535,7 @@ list["updatePassword"] = new SimplePopup( Notifications.add("New passwords don't match", 0); return; } - if ( - window.location.hostname !== "localhost" && - !isPasswordStrong(newPass) - ) { + if (!isLocalhost() && !isPasswordStrong(newPass)) { Notifications.add( "New password must contain at least one capital letter, number, a special character and at least 8 characters long", 0, diff --git a/frontend/src/ts/ready.ts b/frontend/src/ts/ready.ts index 2d076cfa0..dabb60e90 100644 --- a/frontend/src/ts/ready.ts +++ b/frontend/src/ts/ready.ts @@ -15,7 +15,7 @@ import Konami from "konami"; ManualRestart.set(); UpdateConfig.loadFromLocalStorage(); -if (window.location.hostname === "localhost") { +if (Misc.isLocalhost()) { $("head title").text("localhost"); $("#bottom .version .text").text("localhost"); $("#bottom #versionGroup").removeClass("hidden"); @@ -82,7 +82,7 @@ if ("serviceWorker" in navigator) { window.addEventListener("load", () => { // disabling service workers on localhost - they dont really work well with local development // and cause issues with hot reloading - if (window.location.hostname === "localhost") { + if (Misc.isLocalhost()) { navigator.serviceWorker.getRegistrations().then(function (registrations) { for (const registration of registrations) { // if (registration.scope !== "https://monkeytype.com/") diff --git a/frontend/src/ts/ui.ts b/frontend/src/ts/ui.ts index 12a2f2a6d..dd0b31554 100644 --- a/frontend/src/ts/ui.ts +++ b/frontend/src/ts/ui.ts @@ -7,6 +7,7 @@ import * as ConfigEvent from "./observables/config-event"; import { debounce, throttle } from "throttle-debounce"; import * as TestUI from "./test/test-ui"; import { get as getActivePage } from "./states/active-page"; +import { isLocalhost } from "./utils/misc"; export function updateKeytips(): void { const modifierKey = window.navigator.userAgent.toLowerCase().includes("mac") @@ -38,7 +39,7 @@ export function updateKeytips(): void { } } -if (window.location.hostname === "localhost") { +if (isLocalhost()) { window.onerror = function (error): void { Notifications.add(error.toString(), -1); }; diff --git a/frontend/src/ts/utils/misc.ts b/frontend/src/ts/utils/misc.ts index 53234935d..e4fed82a1 100644 --- a/frontend/src/ts/utils/misc.ts +++ b/frontend/src/ts/utils/misc.ts @@ -1374,3 +1374,11 @@ export function loadCSS(href: string, prepend = false): void { document.getElementsByTagName("head")[0].appendChild(link); } } + +export function isLocalhost(): boolean { + return ( + location.hostname === "localhost" || + location.hostname === "127.0.0.1" || + location.hostname === "" + ); +}