diff --git a/src/index.ts b/src/index.ts index c40d5fd..a956f6f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -167,6 +167,16 @@ async function addTab(tabGroup: TabGroup, menuItem: MenuItem): Promise<Tab> { 'webpreferences': 'contextIsolation=yes, nodeIntegration=no', 'autosize': 'on', 'allowpopups': true, + }, + ready: (tab: 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); + }); } }); @@ -233,14 +243,5 @@ window.electronAPI.onOpenTab((webContentId: number, url: string) => { 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 1eae001..5f5bc13 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,4 +1,4 @@ -import { app, BrowserWindow, globalShortcut, ipcMain, Menu, Tray, session } from 'electron'; +import { app, BrowserWindow, globalShortcut, ipcMain, Menu, Tray, session, screen } from 'electron'; import path from 'node:path'; import started from 'electron-squirrel-startup'; import * as http from 'http'; @@ -26,6 +26,10 @@ if (started) { app.exit(0); // 使用 exit 而不是 quit,确保立即退出 } +// Get primary display(需要注意:size 是逻辑像素尺寸(logical screen size),已被 DPI 缩放影响,需要除以 DPI 缩放比例才是物理像素尺寸(physical screen size)) +ipcMain.handle('get-primary-display', () => screen.getPrimaryDisplay()); + +// Check if the URL is available ipcMain.handle('check-url-available', async (event, rawUrl: string) => { try { const url = new URL(rawUrl); diff --git a/src/preload.ts b/src/preload.ts index 32f1daa..e41c1c6 100644 --- a/src/preload.ts +++ b/src/preload.ts @@ -4,9 +4,14 @@ 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)); }, + + // 获取当前屏幕的缩放比例和分辨率 + getPrimaryDisplay: () => ipcRenderer.invoke('get-primary-display'), + // 检查URL是否可用 checkUrlAvailable: (url: string) => ipcRenderer.invoke('check-url-available', url), diff --git a/src/types/electron.d.ts b/src/types/electron.d.ts index c9af3d7..3295f1a 100644 --- a/src/types/electron.d.ts +++ b/src/types/electron.d.ts @@ -1,5 +1,6 @@ export interface ElectronAPI { onOpenTab: (callback: (webContentId: number, url: string) => void) => void; + getPrimaryDisplay: () => Promise<Electron.Display>; checkUrlAvailable: (url: string) => Promise<{ ok: boolean; status: number; error?: string }>; setWebviewCookie: (url: string, cookie: string) => Promise<boolean>; setSessionStorage: (key: string, value: string) => void;