Skip to content

Commit a6c160f

Browse files
authored
add clear history/storage for web widgets (#2383)
1 parent 28385ce commit a6c160f

4 files changed

Lines changed: 49 additions & 4 deletions

File tree

emain/emain.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,19 @@ electron.ipcMain.on("quicklook", (event, filePath: string) => {
373373
}
374374
});
375375

376+
electron.ipcMain.handle("clear-webview-storage", async (event, webContentsId: number) => {
377+
try {
378+
const wc = electron.webContents.fromId(webContentsId);
379+
if (wc && wc.session) {
380+
await wc.session.clearStorageData();
381+
console.log("Cleared cookies and storage for webContentsId:", webContentsId);
382+
}
383+
} catch (e) {
384+
console.error("Failed to clear cookies and storage:", e);
385+
throw e;
386+
}
387+
});
388+
376389
electron.ipcMain.on("open-native-path", (event, filePath: string) => {
377390
console.log("open-native-path", filePath);
378391
filePath = filePath.replace("~", electronApp.getPath("home"));

emain/preload.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ contextBridge.exposeInMainWorld("api", {
5656
openNativePath: (filePath: string) => ipcRenderer.send("open-native-path", filePath),
5757
captureScreenshot: (rect: Rectangle) => ipcRenderer.invoke("capture-screenshot", rect),
5858
setKeyboardChordMode: () => ipcRenderer.send("set-keyboard-chord-mode"),
59+
clearWebviewStorage: (webContentsId: number) => ipcRenderer.invoke("clear-webview-storage", webContentsId),
5960
setWaveAIOpen: (isOpen: boolean) => ipcRenderer.send("set-waveai-open", isOpen),
6061
});
6162

frontend/app/view/webview/webview.tsx

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -411,19 +411,19 @@ export class WebViewModel implements ViewModel {
411411
const searchTemplate = globalStore.get(defaultSearchAtom);
412412
const nextUrl = this.ensureUrlScheme(newUrl, searchTemplate);
413413
console.log("webview loadUrlPromise", reason, nextUrl, "cur=", this.webviewRef.current?.getURL());
414-
414+
415415
if (!this.webviewRef.current) {
416416
return Promise.reject(new Error("WebView ref not available"));
417417
}
418-
418+
419419
if (newUrl != nextUrl) {
420420
globalStore.set(this.url, nextUrl);
421421
}
422-
422+
423423
if (this.webviewRef.current.getURL() != nextUrl) {
424424
return this.webviewRef.current.loadURL(nextUrl);
425425
}
426-
426+
427427
return Promise.resolve();
428428
}
429429

@@ -495,6 +495,25 @@ export class WebViewModel implements ViewModel {
495495
}
496496
}
497497

498+
clearHistory() {
499+
try {
500+
this.webviewRef.current?.clearHistory();
501+
} catch (e) {
502+
console.error("Failed to clear history", e);
503+
}
504+
}
505+
506+
async clearCookiesAndStorage() {
507+
try {
508+
const webContentsId = this.webviewRef.current?.getWebContentsId();
509+
if (webContentsId) {
510+
await getApi().clearWebviewStorage(webContentsId);
511+
}
512+
} catch (e) {
513+
console.error("Failed to clear cookies and storage", e);
514+
}
515+
}
516+
498517
keyDownHandler(e: WaveKeyboardEvent): boolean {
499518
if (checkKeyPressed(e, "Cmd:l")) {
500519
this.urlInputRef?.current?.focus();
@@ -619,6 +638,17 @@ export class WebViewModel implements ViewModel {
619638
}
620639
},
621640
},
641+
{
642+
type: "separator",
643+
},
644+
{
645+
label: "Clear History",
646+
click: () => this.clearHistory(),
647+
},
648+
{
649+
label: "Clear Cookies and Storage (All Web Widgets)",
650+
click: () => fireAndForget(() => this.clearCookiesAndStorage()),
651+
},
622652
];
623653
}
624654
}

frontend/types/custom.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ declare global {
106106
openNativePath(filePath: string): void; // open-native-path
107107
captureScreenshot(rect: Electron.Rectangle): Promise<string>; // capture-screenshot
108108
setKeyboardChordMode: () => void; // set-keyboard-chord-mode
109+
clearWebviewStorage: (webContentsId: number) => Promise<void>; // clear-webview-storage
109110
setWaveAIOpen: (isOpen: boolean) => void; // set-waveai-open
110111
};
111112

0 commit comments

Comments
 (0)