From e978d84f5f3a76ecd0f963c91fe9148e1b52af82 Mon Sep 17 00:00:00 2001 From: Zamitto <167933696+zamitto@users.noreply.github.com> Date: Sun, 8 Dec 2024 22:21:20 -0300 Subject: [PATCH] feat: intercepting cookies --- src/renderer/src/cookies.ts | 52 +++++++++++++++++++++++++++++++++++++ src/renderer/src/main.tsx | 3 +++ 2 files changed, 55 insertions(+) create mode 100644 src/renderer/src/cookies.ts diff --git a/src/renderer/src/cookies.ts b/src/renderer/src/cookies.ts new file mode 100644 index 000000000..b2122d831 --- /dev/null +++ b/src/renderer/src/cookies.ts @@ -0,0 +1,52 @@ +export function addCookieInterceptor() { + Object.defineProperty(document, "cookie", { + enumerable: true, + configurable: true, + get() { + const cookies = localStorage.getItem("cookies") || ""; + console.log("get cookie", cookies); + return cookies; + }, + set(cookieString) { + try { + console.log("setting cookie", cookieString); + const [cookieName, cookieValue] = cookieString.split(";")[0].split("="); + + const currentCookies = localStorage.getItem("cookies") || ""; + + console.log("pre cookies obj", currentCookies); + const cookiesObject = parseCookieStringsToObjects(currentCookies); + cookiesObject[cookieName] = cookieValue; + console.log("cookiesObject", cookiesObject); + + const newString = Object.entries(cookiesObject) + .map(([key, value]) => { + return key + "=" + value; + }) + .join("; "); + + console.log("set cookie", newString); + localStorage.setItem("cookies", newString); + } catch (err) { + console.error(err); + } + }, + }); +} + +const parseCookieStringsToObjects = ( + cookieStrings: string +): { [key: string]: string } => { + const result = {}; + + if (cookieStrings === "") return result; + + console.log(cookieStrings); + cookieStrings.split(";").forEach((cookieString) => { + console.log("forEach", cookieString); + const [name, value] = cookieString.split("="); + result[name.trim()] = value.trim(); + }); + + return result; +}; diff --git a/src/renderer/src/main.tsx b/src/renderer/src/main.tsx index 79ca509b3..8f729df9e 100644 --- a/src/renderer/src/main.tsx +++ b/src/renderer/src/main.tsx @@ -21,6 +21,7 @@ import resources from "@locales"; import { RepacksContextProvider } from "./context"; import { SuspenseWrapper } from "./components"; import { logger } from "./logger"; +import { addCookieInterceptor } from "./cookies"; const Home = React.lazy(() => import("./pages/home/home")); const GameDetails = React.lazy( @@ -37,6 +38,8 @@ const Achievements = React.lazy( console.log = logger.log; +addCookieInterceptor(); + i18n .use(LanguageDetector) .use(initReactI18next)