Serialize browser screenshot capture state

This commit is contained in:
Mohamed Boudra
2026-07-02 19:04:47 +02:00
parent 6fea8305a7
commit d9615c1e98
3 changed files with 125 additions and 55 deletions
@@ -23,7 +23,6 @@ import {
const MAX_CONSOLE_MESSAGES_PER_TAB = 200;
const consoleMessagesByContentsId = new Map<number, BrowserAutomationConsoleLogEntry[]>();
const observedContentsIds = new Set<number>();
const backgroundThrottlingByContentsId = new Map<number, boolean>();
interface IpcHandlerRegistry {
handle(channel: string, listener: (event: unknown, ...args: unknown[]) => unknown): void;
@@ -46,8 +45,8 @@ function adaptWebContents(contents: WebContents): TabContents {
reload: () => contents.reload(),
capturePage: (options) => contents.capturePage(undefined, options),
invalidate: () => contents.invalidate(),
isBackgroundThrottlingAllowed: () => readBackgroundThrottling(contents),
setBackgroundThrottling: (allowed) => setBackgroundThrottling(contents, allowed),
isBackgroundThrottlingAllowed: () => contents.getBackgroundThrottling(),
setBackgroundThrottling: (allowed) => contents.setBackgroundThrottling(allowed),
getConsoleMessages: () => consoleMessagesByContentsId.get(contents.id) ?? [],
getCookies: async (url: string) =>
(await contents.session.cookies.get({ url })).map(normalizeCookie),
@@ -107,15 +106,6 @@ function normalizeCookie(cookie: Electron.Cookie): BrowserAutomationCookieEntry
};
}
function readBackgroundThrottling(contents: WebContents): boolean {
return backgroundThrottlingByContentsId.get(contents.id) ?? true;
}
function setBackgroundThrottling(contents: WebContents, allowed: boolean): void {
backgroundThrottlingByContentsId.set(contents.id, allowed);
contents.setBackgroundThrottling(allowed);
}
function observeConsoleMessages(contents: WebContents): void {
if (observedContentsIds.has(contents.id)) {
return;
@@ -130,7 +120,6 @@ function observeConsoleMessages(contents: WebContents): void {
contents.once("destroyed", () => {
observedContentsIds.delete(contents.id);
consoleMessagesByContentsId.delete(contents.id);
backgroundThrottlingByContentsId.delete(contents.id);
});
}