mirror of
https://github.com/hydralauncher/hydra.git
synced 2025-03-09 15:40:26 +00:00
feat: intercepting cookies
This commit is contained in:
parent
0157d546e4
commit
e978d84f5f
2 changed files with 55 additions and 0 deletions
52
src/renderer/src/cookies.ts
Normal file
52
src/renderer/src/cookies.ts
Normal file
|
@ -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;
|
||||||
|
};
|
|
@ -21,6 +21,7 @@ import resources from "@locales";
|
||||||
import { RepacksContextProvider } from "./context";
|
import { RepacksContextProvider } from "./context";
|
||||||
import { SuspenseWrapper } from "./components";
|
import { SuspenseWrapper } from "./components";
|
||||||
import { logger } from "./logger";
|
import { logger } from "./logger";
|
||||||
|
import { addCookieInterceptor } from "./cookies";
|
||||||
|
|
||||||
const Home = React.lazy(() => import("./pages/home/home"));
|
const Home = React.lazy(() => import("./pages/home/home"));
|
||||||
const GameDetails = React.lazy(
|
const GameDetails = React.lazy(
|
||||||
|
@ -37,6 +38,8 @@ const Achievements = React.lazy(
|
||||||
|
|
||||||
console.log = logger.log;
|
console.log = logger.log;
|
||||||
|
|
||||||
|
addCookieInterceptor();
|
||||||
|
|
||||||
i18n
|
i18n
|
||||||
.use(LanguageDetector)
|
.use(LanguageDetector)
|
||||||
.use(initReactI18next)
|
.use(initReactI18next)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue