From 6786158a3e8b9ac4ee49f0fedb4ebbc5536149be Mon Sep 17 00:00:00 2001 From: xhgit Date: Fri, 13 Nov 2026 23:27:56 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E4=BB=8E=E5=8A=A0=E5=AF=86?= =?UTF-8?q?=E8=81=8A=E5=A4=A9=E6=89=93=E5=BC=80=E9=9A=94=E7=A6=BB=E6=B5=8F?= =?UTF-8?q?=E8=A7=88=E5=99=A8=E9=93=BE=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Desktop/src/browser.ts | 4 ++-- Desktop/src/main.ts | 8 +++++--- Desktop/src/preload.ts | 2 +- docs/progress.md | 4 ++++ tests/encrypted-chat.test.mjs | 11 +++++++++++ web/src/identity.ts | 2 +- web/src/style.css | 1 + web/src/views/ChatView.vue | 30 +++++++++++++++++++++++++++++- 8 files changed, 54 insertions(+), 8 deletions(-) diff --git a/Desktop/src/browser.ts b/Desktop/src/browser.ts index 9fe8a22..1aabf12 100644 --- a/Desktop/src/browser.ts +++ b/Desktop/src/browser.ts @@ -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() }) diff --git a/Desktop/src/main.ts b/Desktop/src/main.ts index d9c5383..898e669 100644 --- a/Desktop/src/main.ts +++ b/Desktop/src/main.ts @@ -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) } diff --git a/Desktop/src/preload.ts b/Desktop/src/preload.ts index f28ccd6..9855ddf 100644 --- a/Desktop/src/preload.ts +++ b/Desktop/src/preload.ts @@ -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), diff --git a/docs/progress.md b/docs/progress.md index 52156f6..11f70b2 100644 --- a/docs/progress.md +++ b/docs/progress.md @@ -72,3 +72,7 @@ Windows 浏览器新增可跨窗口保留的本地书签和历史,每类最多 ## 浏览器逐站权限 普通窗口可按当前网站的协议、主机与端口单独授权或撤销摄像头、麦克风、位置、通知和剪贴板权限。默认拒绝,授权只用于可见的顶级网页。隐私窗口不继承授权。Electron 集成测试通过 Chromium 权限查询验证通知权限默认拒绝、显式授权和撤销。 + +## 聊天中的链接 + +加密文字消息中的 HTTPS 链接在端侧识别。Windows 从链接启动隔离 Chromium,Web 在独立标签页打开并隔离 opener。开发环境支持本机 HTTP。真实双端测试验证消息链接点击与 Chromium 地址显示。网页卡片与从浏览器分享给联系人仍待实现。 diff --git a/tests/encrypted-chat.test.mjs b/tests/encrypted-chat.test.mjs index f3e813b..164318d 100644 --- a/tests/encrypted-chat.test.mjs +++ b/tests/encrypted-chat.test.mjs @@ -193,6 +193,17 @@ test('Web 与 Windows 安全码校验、加密互发、离线接收和状态恢 } finally { db.close() } }) assert.deepEqual(stored, [{ wrapped: true, encrypted: true, fields: ['iv', 'key', 'sealed'] }]) + const link = base + '/healthz' + await send(web, link) + const webLink = web.getByRole('link', { name: link, exact: true }) + assert.equal(await webLink.getAttribute('href'), link) + assert.equal(await webLink.getAttribute('rel'), 'noopener noreferrer') + const windowOpened = desktop.waitForEvent('window') + await windows.getByRole('button', { name: link, exact: true }).click() + const toolbar = await windowOpened + const address = toolbar.getByRole('textbox', { name: '网址或搜索关键词' }) + await toolbar.waitForFunction((expected) => document.querySelector('#address')?.value === expected, link) + assert.equal(await address.inputValue(), link) } finally { await desktop?.close() await browser.close() diff --git a/web/src/identity.ts b/web/src/identity.ts index 1288ecd..6f87416 100644 --- a/web/src/identity.ts +++ b/web/src/identity.ts @@ -6,7 +6,7 @@ interface DeviceProof { interface DesktopBridge { apiBase: string signChallenge: (username: string, challenge: string) => Promise - openBrowser?: () => Promise + openBrowser?: (url?: string) => Promise signMls: (username: string, kind: 'credential' | 'KeyPackage', user: string, device: string, value: Uint8Array) => Promise> protectMls: (key: string) => Promise unprotectMls: (key: string) => Promise diff --git a/web/src/style.css b/web/src/style.css index 140d2df..a50040b 100644 --- a/web/src/style.css +++ b/web/src/style.css @@ -92,6 +92,7 @@ input { width: 100%; color: var(--text); background: var(--base); border: 1px so .chat-lines blockquote { border-left: 2px solid var(--line); margin: 0 0 4px; padding: 0 6px; color: var(--muted); font-size: .9em; } .message-actions { margin-top: 5px; } .message-actions .plain { font-size: .78em; padding: 2px 6px; } +.chat-link { border: 0; background: transparent; color: var(--green); text-decoration: underline; padding: 0; overflow-wrap: anywhere; font: inherit; } .account-footer { display: grid; gap: 12px; margin-top: auto; } .account-footer p { color: var(--muted); font-size: 13px; } .dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: var(--muted); margin: 0 8px 1px 4px; } diff --git a/web/src/views/ChatView.vue b/web/src/views/ChatView.vue index 1c5c102..37679a4 100644 --- a/web/src/views/ChatView.vue +++ b/web/src/views/ChatView.vue @@ -38,6 +38,31 @@ const lines = computed(() => { (!favoritesOnly.value || line.favorite) && (!query || [line.text, line.attachment?.name, line.quote].some((part) => part?.toLowerCase().includes(query)))) }) +const internalBrowser = window.xhchat?.openBrowser + +function messageParts(text: string) { + const parts: { text: string; url?: string }[] = [] + let offset = 0 + for (const match of text.matchAll(/https?:\/\/[^\s<>"']+/giu)) { + const label = match[0].replace(/[.,!?;:,。!?;:、))\]】]+$/u, '') + try { + const url = new URL(label) + if (url.username || url.password || label.length > 4096 || + (url.protocol !== 'https:' && !['127.0.0.1', 'localhost', '[::1]'].includes(url.hostname))) continue + if (match.index > offset) parts.push({ text: text.slice(offset, match.index) }) + parts.push({ text: label, url: url.href }) + offset = match.index + label.length + } catch { + continue + } + } + if (offset < text.length) parts.push({ text: text.slice(offset) }) + return parts +} + +function openLink(url: string) { + if (internalBrowser) void execute(() => internalBrowser(url)) +} let timer: ReturnType async function execute(action: () => Promise) { @@ -235,7 +260,10 @@ onBeforeUnmount(() => clearInterval(timer)) 消息已撤回 - {{ line.text }}待发送 + + 待发送 已编辑
-- 2.54.0