清空localStorage
渲染进程
1 2 3 4 5
| const mwv = this.$refs["mwv"]; if (!mwv) { return; } mwv.executeJavaScript("window.electronAPI.clearLocalStorage();");
|
预加载脚本
1 2 3 4 5 6 7 8
| const { contextBridge } = require("electron"); contextBridge.exposeInMainWorld("electronAPI", { clearLocalStorage: () => { localStorage.clear(); console.log("localStorage 已清空"); }, })
|
清空Cookie
主进程
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| ipcMain.handle("clear-webview-cookies", async () => { const { session } = require("electron"); return new Promise((resolve) => { session.defaultSession.cookies .get({}) .then((cookies) => { cookies.forEach((cookie) => { let url = ""; url += cookie.secure ? "https://" : "http://"; url += cookie.domain.charAt(0) === "." ? "www" : ""; url += cookie.domain; url += cookie.path; session.defaultSession.cookies.remove(url, cookie.name, (error) => { if (error) console.log(`error removing cookie ${cookie.name}`, error); }); }); resolve(); }) .catch((error) => { console.log(error); }); }); });
|
渲染进程
1 2 3
| const { ipcRenderer } = window.require("electron");
await ipcRenderer.invoke("clear-webview-cookies");
|
页面不跳转
如果a标签中有target="_blank"的话点击链接并不会打开对应的页面
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| document.addEventListener( "click", (e) => { const target = e.target.closest("a"); if (target && target.href) { e.preventDefault(); console.log("拦截到了 a 标签的点击事件", target.href); window.location.href = target.href; } }, true );
window.open = (url, target) => { console.log("拦截到了 window.open", url, target); window.location.href = url; return null; };
|