add clear history/storage for web widgets (#2383)

This commit is contained in:
Mike Sawka 2025-10-07 17:15:01 -07:00 committed by GitHub
parent 28385ce08a
commit a6c160f093
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 49 additions and 4 deletions

View file

@ -373,6 +373,19 @@ electron.ipcMain.on("quicklook", (event, filePath: string) => {
}
});
electron.ipcMain.handle("clear-webview-storage", async (event, webContentsId: number) => {
try {
const wc = electron.webContents.fromId(webContentsId);
if (wc && wc.session) {
await wc.session.clearStorageData();
console.log("Cleared cookies and storage for webContentsId:", webContentsId);
}
} catch (e) {
console.error("Failed to clear cookies and storage:", e);
throw e;
}
});
electron.ipcMain.on("open-native-path", (event, filePath: string) => {
console.log("open-native-path", filePath);
filePath = filePath.replace("~", electronApp.getPath("home"));

View file

@ -56,6 +56,7 @@ contextBridge.exposeInMainWorld("api", {
openNativePath: (filePath: string) => ipcRenderer.send("open-native-path", filePath),
captureScreenshot: (rect: Rectangle) => ipcRenderer.invoke("capture-screenshot", rect),
setKeyboardChordMode: () => ipcRenderer.send("set-keyboard-chord-mode"),
clearWebviewStorage: (webContentsId: number) => ipcRenderer.invoke("clear-webview-storage", webContentsId),
setWaveAIOpen: (isOpen: boolean) => ipcRenderer.send("set-waveai-open", isOpen),
});

View file

@ -411,19 +411,19 @@ export class WebViewModel implements ViewModel {
const searchTemplate = globalStore.get(defaultSearchAtom);
const nextUrl = this.ensureUrlScheme(newUrl, searchTemplate);
console.log("webview loadUrlPromise", reason, nextUrl, "cur=", this.webviewRef.current?.getURL());
if (!this.webviewRef.current) {
return Promise.reject(new Error("WebView ref not available"));
}
if (newUrl != nextUrl) {
globalStore.set(this.url, nextUrl);
}
if (this.webviewRef.current.getURL() != nextUrl) {
return this.webviewRef.current.loadURL(nextUrl);
}
return Promise.resolve();
}
@ -495,6 +495,25 @@ export class WebViewModel implements ViewModel {
}
}
clearHistory() {
try {
this.webviewRef.current?.clearHistory();
} catch (e) {
console.error("Failed to clear history", e);
}
}
async clearCookiesAndStorage() {
try {
const webContentsId = this.webviewRef.current?.getWebContentsId();
if (webContentsId) {
await getApi().clearWebviewStorage(webContentsId);
}
} catch (e) {
console.error("Failed to clear cookies and storage", e);
}
}
keyDownHandler(e: WaveKeyboardEvent): boolean {
if (checkKeyPressed(e, "Cmd:l")) {
this.urlInputRef?.current?.focus();
@ -619,6 +638,17 @@ export class WebViewModel implements ViewModel {
}
},
},
{
type: "separator",
},
{
label: "Clear History",
click: () => this.clearHistory(),
},
{
label: "Clear Cookies and Storage (All Web Widgets)",
click: () => fireAndForget(() => this.clearCookiesAndStorage()),
},
];
}
}

View file

@ -106,6 +106,7 @@ declare global {
openNativePath(filePath: string): void; // open-native-path
captureScreenshot(rect: Electron.Rectangle): Promise<string>; // capture-screenshot
setKeyboardChordMode: () => void; // set-keyboard-chord-mode
clearWebviewStorage: (webContentsId: number) => Promise<void>; // clear-webview-storage
setWaveAIOpen: (isOpen: boolean) => void; // set-waveai-open
};