From cfb0f05ff1f2fee66ee2b287bf570d619f4adabc Mon Sep 17 00:00:00 2001 From: Allen Date: Mon, 12 May 2025 01:41:53 +0800 Subject: [PATCH] =?UTF-8?q?=E7=9B=91=E5=90=AC=E5=B9=B6=E6=8B=A6=E6=88=AA?= =?UTF-8?q?=E6=89=80=E6=9C=89webview=E7=9A=84=E6=89=93=E5=BC=80=E7=AA=97?= =?UTF-8?q?=E5=8F=A3=E8=AF=B7=E6=B1=82=EF=BC=8C=E7=84=B6=E5=90=8E=E5=9C=A8?= =?UTF-8?q?=E6=96=B0=E7=9A=84webview=E6=89=93=E5=BC=80=E5=AE=83=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/index.ts | 19 +++++++++++++++++++ src/main.ts | 8 ++++++++ src/preload.ts | 5 ++++- src/types/electron.d.ts | 1 + 4 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index 35dabaa..c40d5fd 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,4 @@ +import { WebviewTag } from 'electron'; import { TabGroup, Tab } from 'electron-tabs'; // 菜单项 @@ -225,3 +226,21 @@ async function initialize() { // 页面加载完成后初始化 document.addEventListener('DOMContentLoaded', initialize); + +window.electronAPI.onOpenTab((webContentId: number, url: string) => { + addTab(tabGroup, { + Url: url, + ShowName: '新标签页', + IconConfig: { _1x: { Default: '', Selected: '' }, _2x: { Default: '', Selected: '' } }, + Children: null + }).then(tab => { + // 在加载完成后,获取标题 + tab.once('webview-dom-ready', (detail: string) => { + console.log('webview-dom-ready detail:', detail); + const webview = tab.webview as WebviewTag; + const title = webview.getTitle(); + tab.setTitle(title); + console.log('webview-dom-ready title:', title); + }); + }); +}); \ No newline at end of file diff --git a/src/main.ts b/src/main.ts index 89be87b..1eae001 100644 --- a/src/main.ts +++ b/src/main.ts @@ -135,6 +135,14 @@ const createWindow = () => { // }); // }); + win.webContents.on('did-attach-webview', (event, webContents) => { + webContents.setWindowOpenHandler((details) => { + console.log('webview-new-window', webContents, details); + win.webContents.send('webview-new-window', webContents.id, details.url); + return { action: 'deny' }; + }); + }); + // Set session win.webContents.session.setPermissionRequestHandler((webContents, permission, callback) => { callback(true); diff --git a/src/preload.ts b/src/preload.ts index 02c487b..32f1daa 100644 --- a/src/preload.ts +++ b/src/preload.ts @@ -4,6 +4,9 @@ import { contextBridge, ipcRenderer } from 'electron'; contextBridge.exposeInMainWorld('electronAPI', { + onOpenTab: (callback: (webContentId: number, url: string) => void) => { + ipcRenderer.on('webview-new-window', (_event, webContentId, url) => callback(webContentId, url)); + }, // 检查URL是否可用 checkUrlAvailable: (url: string) => ipcRenderer.invoke('check-url-available', url), @@ -29,4 +32,4 @@ contextBridge.exposeInMainWorld('electronAPI', { clearSessionStorage: () => { window.sessionStorage.clear(); } -}); +}); \ No newline at end of file diff --git a/src/types/electron.d.ts b/src/types/electron.d.ts index cf46121..c9af3d7 100644 --- a/src/types/electron.d.ts +++ b/src/types/electron.d.ts @@ -1,4 +1,5 @@ export interface ElectronAPI { + onOpenTab: (callback: (webContentId: number, url: string) => void) => void; checkUrlAvailable: (url: string) => Promise<{ ok: boolean; status: number; error?: string }>; setWebviewCookie: (url: string, cookie: string) => Promise; setSessionStorage: (key: string, value: string) => void;