Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/intercepting cookies #1282

Merged
merged 4 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@
"manage_files_description": "Manage which files will be backed up and restored",
"select_folder": "Select folder",
"backup_from": "Backup from {{date}}",
"custom_backup_location_set": "Custom backup location set"
"custom_backup_location_set": "Custom backup location set",
"no_directory_selected": "No directory selected"
},
"activation": {
"title": "Activate Hydra",
Expand Down
6 changes: 4 additions & 2 deletions src/locales/es/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
"open_screenshot": "Abrir captura {{number}}",
"download_settings": "Ajustes de descarga",
"downloader": "Método de descarga",
"select_executable": "Seleccionar ejecutable",
"select_executable": "Seleccionar",
"no_executable_selected": "No se seleccionó un ejecutable",
"open_folder": "Abrir carpeta",
"open_download_location": "Ver archivos descargados",
Expand Down Expand Up @@ -166,7 +166,9 @@
"manage_files_description": "Gestiona los archivos que serán respaldados y restaurados",
"select_folder": "Seleccionar carpeta",
"backup_from": "Copia de seguridad de {{date}}",
"custom_backup_location_set": "Se configuró la carpeta de copia de seguridad"
"custom_backup_location_set": "Se configuró la carpeta de copia de seguridad",
"clear": "Limpiar",
"no_directory_selected": "No se seleccionó un directório"
},
"activation": {
"title": "Activar Hydra",
Expand Down
4 changes: 3 additions & 1 deletion src/locales/pt-BR/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,9 @@
"backup_from": "Backup de {{date}}",
"custom_backup_location_set": "Localização customizada selecionada",
"select_folder": "Selecione a pasta",
"manage_files_description": "Gerencie quais arquivos serão feitos backup"
"manage_files_description": "Gerencie quais arquivos serão feitos backup",
"clear": "Limpar",
"no_directory_selected": "Nenhum diretório selecionado"
},
"activation": {
"title": "Ativação",
Expand Down
4 changes: 2 additions & 2 deletions src/main/events/library/select-game-wine-prefix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import { registerEvent } from "../register-event";
const selectGameWinePrefix = async (
_event: Electron.IpcMainInvokeEvent,
id: number,
winePrefixPath: string
winePrefixPath: string | null
) => {
return gameRepository.update({ id }, { winePrefixPath });
return gameRepository.update({ id }, { winePrefixPath: winePrefixPath });
};

registerEvent("selectGameWinePrefix", selectGameWinePrefix);
8 changes: 6 additions & 2 deletions src/main/events/library/update-executable-path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@ import { parseExecutablePath } from "../helpers/parse-executable-path";
const updateExecutablePath = async (
_event: Electron.IpcMainInvokeEvent,
id: number,
executablePath: string
executablePath: string | null
) => {
const parsedPath = executablePath
? parseExecutablePath(executablePath)
: null;

return gameRepository.update(
{
id,
},
{
executablePath: parseExecutablePath(executablePath),
executablePath: parsedPath,
}
);
};
Expand Down
4 changes: 2 additions & 2 deletions src/preload/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ contextBridge.exposeInMainWorld("electron", {
ipcRenderer.invoke("addGameToLibrary", objectId, title, shop),
createGameShortcut: (id: number) =>
ipcRenderer.invoke("createGameShortcut", id),
updateExecutablePath: (id: number, executablePath: string) =>
updateExecutablePath: (id: number, executablePath: string | null) =>
ipcRenderer.invoke("updateExecutablePath", id, executablePath),
selectGameWinePrefix: (id: number, winePrefixPath: string) =>
selectGameWinePrefix: (id: number, winePrefixPath: string | null) =>
ipcRenderer.invoke("selectGameWinePrefix", id, winePrefixPath),
verifyExecutablePathInUse: (executablePath: string) =>
ipcRenderer.invoke("verifyExecutablePathInUse", executablePath),
Expand Down
44 changes: 44 additions & 0 deletions src/renderer/src/cookies.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
export function addCookieInterceptor() {
Object.defineProperty(document, "cookie", {
enumerable: true,
configurable: true,
get() {
return localStorage.getItem("cookies") || "";
},
set(cookieString) {
try {
const [cookieName, cookieValue] = cookieString.split(";")[0].split("=");

const currentCookies = localStorage.getItem("cookies") || "";

const cookiesObject = parseCookieStringsToObjects(currentCookies);
cookiesObject[cookieName] = cookieValue;

const newString = Object.entries(cookiesObject)
.map(([key, value]) => {
return key + "=" + value;
})
.join("; ");

localStorage.setItem("cookies", newString);
} catch (err) {
console.error(err);
}
},
});
}

const parseCookieStringsToObjects = (
cookieStrings: string
): { [key: string]: string } => {
const result = {};

if (cookieStrings === "") return result;

cookieStrings.split(";").forEach((cookieString) => {
const [name, value] = cookieString.split("=");
result[name.trim()] = value.trim();
});

return result;
};
10 changes: 8 additions & 2 deletions src/renderer/src/declaration.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,14 @@ declare global {
shop: GameShop
) => Promise<void>;
createGameShortcut: (id: number) => Promise<boolean>;
updateExecutablePath: (id: number, executablePath: string) => Promise<void>;
selectGameWinePrefix: (id: number, winePrefixPath: string) => Promise<void>;
updateExecutablePath: (
id: number,
executablePath: string | null
) => Promise<void>;
selectGameWinePrefix: (
id: number,
winePrefixPath: string | null
) => Promise<void>;
verifyExecutablePathInUse: (executablePath: string) => Promise<Game>;
getLibrary: () => Promise<LibraryGame[]>;
openGameInstaller: (gameId: number) => Promise<boolean>;
Expand Down
3 changes: 3 additions & 0 deletions src/renderer/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -37,6 +38,8 @@ const Achievements = React.lazy(

console.log = logger.log;

addCookieInterceptor();

i18n
.use(LanguageDetector)
.use(initReactI18next)
Expand Down
63 changes: 35 additions & 28 deletions src/renderer/src/pages/game-details/modals/game-options-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export function GameOptionsModal({
};

const handleClearExecutablePath = async () => {
await window.electron.updateExecutablePath(game.id, "");
await window.electron.updateExecutablePath(game.id, null);
updateGame();
};

Expand All @@ -112,7 +112,7 @@ export function GameOptionsModal({
};

const handleClearWinePrefixPath = async () => {
await window.electron.selectGameWinePrefix(game.id, "");
await window.electron.selectGameWinePrefix(game.id, null);
updateGame();
};

Expand Down Expand Up @@ -155,14 +155,21 @@ export function GameOptionsModal({
disabled
placeholder={t("no_executable_selected")}
rightContent={
<Button
type="button"
theme="outline"
onClick={handleChangeExecutableLocation}
>
<FileIcon />
{t("select_executable")}
</Button>
<>
<Button
type="button"
theme="outline"
onClick={handleChangeExecutableLocation}
>
<FileIcon />
{t("select_executable")}
</Button>
{game.executablePath && (
<Button onClick={handleClearExecutablePath} theme="outline">
{t("clear")}
</Button>
)}
</>
}
/>

Expand All @@ -178,9 +185,6 @@ export function GameOptionsModal({
<Button onClick={handleCreateShortcut} theme="outline">
{t("create_shortcut")}
</Button>
<Button onClick={handleClearExecutablePath} theme="outline">
{t("clear")}
</Button>
</div>
)}

Expand All @@ -199,23 +203,26 @@ export function GameOptionsModal({
disabled
placeholder={t("no_directory_selected")}
rightContent={
<Button
type="button"
theme="outline"
onClick={handleChangeWinePrefixPath}
>
<FileDirectoryIcon />
{t("select_executable")}
</Button>
<>
<Button
type="button"
theme="outline"
onClick={handleChangeWinePrefixPath}
>
<FileDirectoryIcon />
{t("select_executable")}
</Button>
{game.winePrefixPath && (
<Button
onClick={handleClearWinePrefixPath}
theme="outline"
>
{t("clear")}
</Button>
)}
</>
}
/>
{game.winePrefixPath && (
<div className={styles.gameOptionRow}>
<Button onClick={handleClearWinePrefixPath} theme="outline">
{t("clear")}
</Button>
</div>
)}
</div>
)}

Expand Down
Loading