支持从加密聊天打开隔离浏览器链接

This commit is contained in:
xhgit committed 2026-11-13 23:27:56 +08:00
1 parent f4ea1f8e03
commit 6786158a3e
8 files changed
+54 -8

No files matched your search

+2 -2
View File
@@ -218,7 +218,7 @@ function configureSites(sites: Session, privateMode: boolean) {
})
}
export function openBrowser(trusted: Session, privateMode = false) {
export function openBrowser(trusted: Session, privateMode = false, initial?: URL) {
const sites = session.fromPartition(privateMode ? 'xhchat-private-' + randomUUID() : 'persist:xhchat-sites')
configureSites(sites, privateMode)
const window = new BrowserWindow({
@@ -244,7 +244,7 @@ export function openBrowser(trusted: Session, privateMode = false) {
for (const tab of browser.tabs) tab.view.webContents.close()
if (privateMode) void sites.clearData().catch(() => {})
})
window.webContents.on('did-finish-load', () => addTab(browser))
window.webContents.once('did-finish-load', () => addTab(browser, initial))
void window.loadURL(browserURL).catch(() => {
if (!window.isDestroyed()) window.close()
})
+5 -3
View File
@@ -2,7 +2,7 @@ import { app, BrowserWindow, ipcMain, protocol, safeStorage, session } from 'ele
import { readFile } from 'node:fs/promises'
import { join } from 'node:path'
import { signChallenge, signMls } from './identity'
import { openBrowser, registerBrowserHandlers } from './browser'
import { openBrowser, registerBrowserHandlers, website } from './browser'
const appURL = 'xhchat://app/'
app.enableSandbox()
@@ -102,9 +102,11 @@ async function start() {
return key
})
registerBrowserHandlers(trusted)
ipcMain.handle('browser:open', (event) => {
ipcMain.handle('browser:open', (event, url: unknown) => {
if (!trustedSender(event)) throw new Error('浏览器入口来源无效')
openBrowser(trusted)
const initial = typeof url === 'string' && url.length <= 4096 ? website(url) : undefined
if (url !== undefined && !initial) throw new Error('网页地址无效')
openBrowser(trusted, false, initial)
})
await win.loadURL(appURL)
}
+1 -1
View File
@@ -5,7 +5,7 @@ const apiArgument = process.argv.find((value) => value.startsWith('--xhchat-api=
contextBridge.exposeInMainWorld('xhchat', {
apiBase: apiArgument?.slice('--xhchat-api='.length),
signChallenge: (username: string, challenge: string) => ipcRenderer.invoke('device:sign', username, challenge),
openBrowser: () => ipcRenderer.invoke('browser:open'),
openBrowser: (url?: string) => ipcRenderer.invoke('browser:open', url),
signMls: (username: string, kind: string, user: string, device: string, value: Uint8Array) =>
ipcRenderer.invoke('device:sign-mls', username, kind, user, device, value),
protectMls: (key: string) => ipcRenderer.invoke('device:protect-mls', key),