新增获取当前显示屏幕信息接口

This commit is contained in:
Allen 2025-05-12 23:41:36 +08:00
parent cfb0f05ff1
commit 3cec21a7f3
4 changed files with 21 additions and 10 deletions

View File

@ -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);
});
});
});

View File

@ -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);

View File

@ -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),

View File

@ -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;