为 Chromium 浏览器加入书签和持久历史 #10
No files matched your search
@@ -9,7 +9,7 @@
|
||||
</head>
|
||||
<body>
|
||||
<header>
|
||||
<div class="tabs"><div id="tabs" aria-label="标签页"></div><button data-action="new" aria-label="新建标签页">+</button><span id="mode"></span></div>
|
||||
<div class="tabs"><div id="tabs" aria-label="标签页"></div><button data-action="new" aria-label="新建标签页">+</button><span id="mode"></span><button data-action="bookmark">添加书签</button><button data-action="library">书签与历史</button></div>
|
||||
<form id="address-form">
|
||||
<button type="button" data-action="back" aria-label="后退">←</button>
|
||||
<button type="button" data-action="forward" aria-label="前进">→</button>
|
||||
@@ -26,5 +26,9 @@
|
||||
</form>
|
||||
<div id="status" role="status"></div>
|
||||
</header>
|
||||
<main id="library" hidden>
|
||||
<section><h1>书签</h1><ul id="bookmarks"></ul></section>
|
||||
<section><h1>浏览历史</h1><ul id="history"></ul></section>
|
||||
</main>
|
||||
</body>
|
||||
</html>
|
||||
@@ -16,4 +16,11 @@ form { padding: 6px 8px 2px; }
|
||||
input { flex: 1; min-width: 0; }
|
||||
#status { padding: 0 10px; color: light-dark(#943f42,#ffb3b9); overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
|
||||
#mode { margin-left: auto; white-space: nowrap; }
|
||||
#library { display: grid; grid-template-columns: repeat(2,minmax(0,1fr)); gap: 18px; padding: 18px; }
|
||||
#library[hidden] { display: none; }
|
||||
#library h1 { font-size: 18px; margin: 0 0 10px; }
|
||||
#library ul { list-style: none; margin: 0; padding: 0; }
|
||||
#library li { display: flex; gap: 6px; align-items: start; margin-bottom: 6px; }
|
||||
#library li button:first-child { display: grid; flex: 1; text-align: left; white-space: normal; overflow-wrap: anywhere; }
|
||||
#library li span { font-size: 11px; opacity: .78; }
|
||||
@media (max-width: 750px) { form [data-action="zoomIn"], form [data-action="zoomOut"] { display: none; } }
|
||||
@@ -5,6 +5,34 @@ const status = document.querySelector('#status')
|
||||
const button = (action) => document.querySelector(`[data-action="${action}"]`)
|
||||
let current
|
||||
|
||||
function renderEntries(selector, entries, removable) {
|
||||
const container = document.querySelector(selector)
|
||||
const rows = entries.map((entry) => {
|
||||
const row = document.createElement('li')
|
||||
const open = document.createElement('button')
|
||||
const title = document.createElement('strong')
|
||||
const url = document.createElement('span')
|
||||
title.textContent = entry.title || entry.url
|
||||
url.textContent = entry.url
|
||||
open.append(title, url)
|
||||
open.onclick = () => void command('openEntry', entry.id)
|
||||
row.append(open)
|
||||
if (removable) {
|
||||
const remove = document.createElement('button')
|
||||
remove.textContent = '移除'
|
||||
remove.onclick = () => void command('removeBookmark', entry.id)
|
||||
row.append(remove)
|
||||
}
|
||||
return row
|
||||
})
|
||||
if (!rows.length) {
|
||||
const empty = document.createElement('li')
|
||||
empty.textContent = removable ? '暂无书签' : '暂无历史记录'
|
||||
rows.push(empty)
|
||||
}
|
||||
container.replaceChildren(...rows)
|
||||
}
|
||||
|
||||
function render(state) {
|
||||
current = state
|
||||
tabs.replaceChildren(...state.tabs.map((tab) => {
|
||||
@@ -28,6 +56,16 @@ function render(state) {
|
||||
button('reload').setAttribute('aria-label', state.loading ? '停止加载' : '刷新')
|
||||
for (const action of ['copy', 'external', 'zoomIn', 'zoomOut']) button(action).disabled = !state.url
|
||||
document.querySelector('#mode').textContent = state.privateMode ? '隐私窗口' : ''
|
||||
button('bookmark').hidden = state.privateMode
|
||||
button('bookmark').disabled = !state.url
|
||||
button('bookmark').textContent = state.bookmarked ? '取消书签' : '添加书签'
|
||||
button('library').hidden = state.privateMode
|
||||
button('library').setAttribute('aria-pressed', String(state.libraryMode))
|
||||
document.querySelector('#library').hidden = !state.libraryMode
|
||||
if (state.libraryMode) {
|
||||
renderEntries('#bookmarks', state.bookmarks, true)
|
||||
renderEntries('#history', state.history, false)
|
||||
}
|
||||
status.textContent = state.error
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
import { app } from 'electron'
|
||||
import { randomUUID } from 'node:crypto'
|
||||
import { readFileSync, renameSync, writeFileSync } from 'node:fs'
|
||||
import { join } from 'node:path'
|
||||
|
||||
export type Entry = { id: string; url: string; title: string; time: number }
|
||||
type Library = { bookmarks: Entry[]; history: Entry[] }
|
||||
|
||||
let cache: Library | undefined
|
||||
const file = () => join(app.getPath('userData'), 'browser-library.json')
|
||||
|
||||
export function library(): Library {
|
||||
if (cache) return cache
|
||||
try {
|
||||
const saved = JSON.parse(readFileSync(file(), 'utf8')) as Partial<Library>
|
||||
cache = { bookmarks: parseEntries(saved.bookmarks), history: parseEntries(saved.history) }
|
||||
} catch {
|
||||
cache = { bookmarks: [], history: [] }
|
||||
}
|
||||
return cache
|
||||
}
|
||||
|
||||
function parseEntries(value: unknown): Entry[] {
|
||||
if (!Array.isArray(value)) return []
|
||||
return value.slice(0, 250).filter((item): item is Entry =>
|
||||
typeof item?.id === 'string' && typeof item.url === 'string' &&
|
||||
typeof item.title === 'string' && typeof item.time === 'number')
|
||||
}
|
||||
|
||||
function persist(next: Library) {
|
||||
const target = file()
|
||||
const temporary = target + '.tmp'
|
||||
writeFileSync(temporary, JSON.stringify(next), { mode: 0o600 })
|
||||
renameSync(temporary, target)
|
||||
cache = next
|
||||
}
|
||||
|
||||
function entry(url: string, title: string): Entry {
|
||||
return { id: randomUUID(), url, title: title.trim().slice(0, 200), time: Date.now() }
|
||||
}
|
||||
|
||||
export function visit(url: string, title: string) {
|
||||
const previous = library()
|
||||
const first = previous.history[0]
|
||||
if (first?.url === url && first.title === title) return
|
||||
const history = [entry(url, title), ...previous.history.filter((item) => item.url !== url)].slice(0, 250)
|
||||
persist({ ...previous, history })
|
||||
}
|
||||
|
||||
export function toggleBookmark(url: string, title: string) {
|
||||
const previous = library()
|
||||
const present = previous.bookmarks.some((item) => item.url === url)
|
||||
if (!present && previous.bookmarks.length >= 250) throw new Error('书签数量已达上限')
|
||||
const bookmarks = present ? previous.bookmarks.filter((item) => item.url !== url) :
|
||||
[entry(url, title), ...previous.bookmarks]
|
||||
persist({ ...previous, bookmarks })
|
||||
}
|
||||
|
||||
export function removeBookmark(id: string) {
|
||||
const previous = library()
|
||||
persist({ ...previous, bookmarks: previous.bookmarks.filter((item) => item.id !== id) })
|
||||
}
|
||||
|
||||
export function clearHistory() {
|
||||
persist({ ...library(), history: [] })
|
||||
}
|
||||
+58
-3
@@ -2,6 +2,7 @@ import { app, BrowserWindow, clipboard, dialog, ipcMain, Menu, session, shell, W
|
||||
import type { Session } from 'electron'
|
||||
import { randomUUID } from 'node:crypto'
|
||||
import { join } from 'node:path'
|
||||
import { clearHistory, library, removeBookmark, toggleBookmark, visit } from './browser-library'
|
||||
|
||||
const browserURL = 'xhchat://app/browser'
|
||||
const height = 102
|
||||
@@ -14,6 +15,7 @@ type BrowserWindowState = {
|
||||
privateMode: boolean
|
||||
tabs: Tab[]
|
||||
active?: Tab
|
||||
libraryMode: boolean
|
||||
}
|
||||
|
||||
const windows = new Map<number, BrowserWindowState>()
|
||||
@@ -39,6 +41,7 @@ function address(value: string): URL | undefined {
|
||||
|
||||
function state(browser: BrowserWindowState) {
|
||||
const contents = browser.active?.view.webContents
|
||||
const collection = browser.privateMode ? { bookmarks: [], history: [] } : library()
|
||||
return {
|
||||
privateMode: browser.privateMode,
|
||||
active: browser.active?.id,
|
||||
@@ -48,6 +51,9 @@ function state(browser: BrowserWindowState) {
|
||||
forward: contents?.navigationHistory.canGoForward() ?? false,
|
||||
loading: contents?.isLoading() ?? false,
|
||||
error: browser.active?.error ?? '',
|
||||
libraryMode: browser.libraryMode,
|
||||
bookmarked: collection.bookmarks.some((item) => item.url === contents?.getURL()),
|
||||
...collection,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -59,6 +65,7 @@ function resize(browser: BrowserWindowState) {
|
||||
const [width, total] = browser.window.getContentSize()
|
||||
const top = browser.window.isFullScreen() ? 0 : height
|
||||
browser.active?.view.setBounds({ x: 0, y: top, width, height: Math.max(0, total - top) })
|
||||
browser.active?.view.setVisible(!browser.libraryMode)
|
||||
}
|
||||
|
||||
function activate(browser: BrowserWindowState, tab?: Tab) {
|
||||
@@ -71,6 +78,10 @@ function activate(browser: BrowserWindowState, tab?: Tab) {
|
||||
update(browser)
|
||||
}
|
||||
|
||||
function updateNormalWindows() {
|
||||
for (const browser of windows.values()) if (!browser.privateMode) update(browser)
|
||||
}
|
||||
|
||||
async function navigate(tab: Tab, url: URL) {
|
||||
tab.error = ''
|
||||
try {
|
||||
@@ -106,11 +117,23 @@ function addTab(browser: BrowserWindowState, url?: URL) {
|
||||
if (mainFrame && code !== -3) tab.error = '网页加载失败,请检查地址或网络。'
|
||||
update(browser)
|
||||
})
|
||||
function remember() {
|
||||
const current = website(contents.getURL())
|
||||
if (!browser.privateMode && current && current.href.length <= 4096) {
|
||||
try {
|
||||
visit(current.href, contents.getTitle())
|
||||
updateNormalWindows()
|
||||
} catch {
|
||||
tab.error = '无法保存浏览历史'
|
||||
}
|
||||
}
|
||||
update(browser)
|
||||
}
|
||||
contents.on('did-navigate', () => { tab.error = ''; update(browser) })
|
||||
contents.on('did-navigate-in-page', () => update(browser))
|
||||
contents.on('did-navigate-in-page', (_event, _url, mainFrame) => { if (mainFrame) remember() })
|
||||
contents.on('page-title-updated', () => update(browser))
|
||||
contents.on('did-start-loading', () => update(browser))
|
||||
contents.on('did-stop-loading', () => update(browser))
|
||||
contents.on('did-stop-loading', remember)
|
||||
contents.on('enter-html-full-screen', () => resize(browser))
|
||||
contents.on('leave-html-full-screen', () => resize(browser))
|
||||
contents.on('before-input-event', (event, input) => {
|
||||
@@ -179,7 +202,7 @@ export function openBrowser(trusted: Session, privateMode = false) {
|
||||
sandbox: true, contextIsolation: true, nodeIntegration: false, webviewTag: false,
|
||||
},
|
||||
})
|
||||
const browser: BrowserWindowState = { window, sites, privateMode, tabs: [] }
|
||||
const browser: BrowserWindowState = { window, sites, privateMode, tabs: [], libraryMode: false }
|
||||
const id = window.webContents.id
|
||||
windows.set(id, browser)
|
||||
window.setMenuBarVisibility(false)
|
||||
@@ -225,6 +248,8 @@ export function registerBrowserHandlers(trusted: Session) {
|
||||
case 'go': {
|
||||
const target = typeof value === 'string' ? address(value) : undefined
|
||||
if (!target) throw new Error('请输入 HTTPS 地址或搜索关键词。')
|
||||
browser.libraryMode = false
|
||||
resize(browser)
|
||||
if (tab) void navigate(tab, target)
|
||||
break
|
||||
}
|
||||
@@ -237,6 +262,35 @@ export function registerBrowserHandlers(trusted: Session) {
|
||||
case 'zoomIn': if (contents) contents.setZoomLevel(Math.min(4, contents.getZoomLevel() + 1)); break
|
||||
case 'zoomOut': if (contents) contents.setZoomLevel(Math.max(-3, contents.getZoomLevel() - 1)); break
|
||||
case 'fullscreen': browser.window.setFullScreen(!browser.window.isFullScreen()); break
|
||||
case 'library': {
|
||||
if (browser.privateMode) throw new Error('隐私窗口不记录浏览历史')
|
||||
browser.libraryMode = !browser.libraryMode
|
||||
resize(browser)
|
||||
break
|
||||
}
|
||||
case 'bookmark': {
|
||||
if (browser.privateMode || !current || current.href.length > 4096) throw new Error('此页面无法添加书签')
|
||||
toggleBookmark(current.href, contents?.getTitle() ?? '')
|
||||
updateNormalWindows()
|
||||
break
|
||||
}
|
||||
case 'removeBookmark': {
|
||||
if (browser.privateMode || typeof value !== 'string') break
|
||||
removeBookmark(value)
|
||||
updateNormalWindows()
|
||||
break
|
||||
}
|
||||
case 'openEntry': {
|
||||
if (browser.privateMode || typeof value !== 'string') break
|
||||
const { bookmarks, history } = library()
|
||||
const entry = [...bookmarks, ...history].find((item) => item.id === value)
|
||||
const target = entry ? website(entry.url) : undefined
|
||||
if (!target) throw new Error('该网址不可用')
|
||||
browser.libraryMode = false
|
||||
resize(browser)
|
||||
if (tab) void navigate(tab, target)
|
||||
break
|
||||
}
|
||||
case 'clear': {
|
||||
const answer = await dialog.showMessageBox(browser.window, {
|
||||
type: 'question', message: '清除浏览器 Cookie、缓存和网站数据?',
|
||||
@@ -244,6 +298,7 @@ export function registerBrowserHandlers(trusted: Session) {
|
||||
})
|
||||
if (answer.response === 1) {
|
||||
await browser.sites.clearData()
|
||||
if (!browser.privateMode) clearHistory()
|
||||
for (const candidate of windows.values()) {
|
||||
if (candidate.sites !== browser.sites) continue
|
||||
for (const tab of candidate.tabs) tab.view.webContents.navigationHistory.clear()
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
| 加密文字聊天 | 在两个已核验设备之间发送 MLS 加密消息,支持离线接收。 |
|
||||
| 加密附件 | 两端可发送和下载最多 8 MiB 的文件,下载时验证完整性。 |
|
||||
| 消息管理 | 支持引用回复、搜索本机历史、收藏和本机删除。 |
|
||||
| Chromium 浏览器 | Windows 端支持独立窗口、多标签、导航、搜索和隐私窗口,网页运行在独立会话中。 |
|
||||
| Chromium 浏览器 | Windows 端支持独立窗口、多标签、导航、搜索、书签、历史和隐私窗口,网页运行在独立会话中。 |
|
||||
|
||||
当前为开发预览。大文件分片、群聊、音视频和多设备历史同步仍在开发,Windows 安装包尚未发布。 [开发进度与验收记录](docs/progress.md)
|
||||
|
||||
|
||||
@@ -4,6 +4,8 @@
|
||||
|
||||
内置浏览器将以 Chromium 的真实 UA 追加 XHChat/版本,保留 UA-CH 默认协商并用测试站检查一致性。导航、重定向和新窗口只接受允许的网络协议。摄像头、麦克风、剪贴板、通知与位置默认拒绝,后续按可信 UI 中的顶级网站授权。下载由可信 UI 确认路径及来源。恶意站点警告机制需要评估信誉服务、误报与隐私影响。
|
||||
|
||||
普通窗口的书签和历史保存在 Electron 用户目录,独立于通讯存储。可信工具栏展示标题和地址时使用纯文本。打开收藏或历史项会在主进程重新校验 URL。隐私窗口使用临时分区,关闭后清理网站数据,不写入浏览记录。
|
||||
|
||||
聊天链接卡片由发送者主动请求预览。若采用代理,部署独立网络出口并在每次重定向及实际建连时校验公网目的地址,限制响应类型、体积、超时与跳转次数。代理只接收明确提交的 URL,卡片由客户端随 E2EE 消息传输。安全验收包含远程网页的权限拒绝、无特权对象、无通讯存储可见性和自定义协议隔离。
|
||||
|
||||
## 威胁模型
|
||||
|
||||
+5
-1
@@ -21,7 +21,7 @@ Web 和 Electron 共用 Vue 客户端,独立接入同一 Go API。真实 Postg
|
||||
- 阶段一还需邮箱和手机号注册、验证码服务、找回密码和双重验证。真实联调需要 SMTP 与短信供应商配置。
|
||||
- 阶段二与三还需分片附件、消息操作、群聊、多设备可信授权、加密备份与跨设备历史同步。两设备文字和小附件私聊已进入开发预览。
|
||||
- 阶段四还需 WebRTC 音视频、TURN、SFU、屏幕共享及媒体 E2EE。
|
||||
- 阶段五还需持久书签和历史、下载记录、逐站授权、反钓鱼及聊天联动。
|
||||
- 阶段五还需下载记录、逐站授权、反钓鱼及聊天联动。
|
||||
- 阶段六到八还需社交、扩展生态、独立管理后台、推送、生产部署与备份演练、压力测试、安全评估、签名安装包和发布流程。
|
||||
|
||||
## 联系人和 Chromium 浏览器
|
||||
@@ -58,3 +58,7 @@ Windows 与 Web 支持发送和下载最多 8 MiB 的附件。文件名、长度
|
||||
## 消息管理
|
||||
|
||||
Windows 与 Web 可发送带引用摘要的 MLS 文字消息。两端支持按正文、引用和文件名搜索本机历史,以及收藏和本机删除。收藏标记与历史保存在加密本地记录中。集成测试覆盖引用跨端显示、收藏筛选、搜索及删除后另一端仍可读取原消息。远程撤回、编辑、转发与跨设备收藏同步待后续交付。
|
||||
|
||||
## 浏览器书签和历史
|
||||
|
||||
Windows 浏览器新增可跨窗口保留的本地书签和历史,每类最多 250 条。书签支持添加、打开和移除。清除站点数据会同时清除历史。隐私窗口不读写书签或历史,隔离测试验证其访问不会出现在普通窗口。
|
||||
@@ -90,6 +90,24 @@ test('双端联系人实时同步,桌面浏览器独立分区与 UA', { timeou
|
||||
const userAgent = requests.find((entry) => entry.path === '/first').userAgent
|
||||
assert.ok(userAgent.includes('Chrome/' + site.version))
|
||||
assert.match(userAgent, /XHChat\/0\.1\.0/)
|
||||
await toolbar.getByRole('button', { name: '添加书签', exact: true }).click()
|
||||
await toolbar.getByRole('button', { name: '取消书签', exact: true }).waitFor()
|
||||
await toolbar.getByRole('button', { name: '书签与历史', exact: true }).click()
|
||||
await toolbar.locator('#bookmarks').getByText(url + '/first', { exact: true }).waitFor()
|
||||
await toolbar.locator('#history').getByText(url + '/first', { exact: true }).waitFor()
|
||||
const openedPrivate = app.waitForEvent('window')
|
||||
await toolbar.getByRole('button', { name: '隐私窗口', exact: true }).click()
|
||||
const privateToolbar = await openedPrivate
|
||||
const privateAddress = privateToolbar.getByRole('textbox', { name: '网址或搜索关键词' })
|
||||
await privateAddress.fill(url + '/private')
|
||||
await privateAddress.press('Enter')
|
||||
await privateToolbar.getByRole('button', { name: '隔离测试站', exact: true }).waitFor()
|
||||
assert.equal(await privateToolbar.getByRole('button', { name: '书签与历史' }).count(), 0)
|
||||
assert.equal(await toolbar.locator('#history').getByText(url + '/private', { exact: true }).count(), 0)
|
||||
await privateToolbar.close()
|
||||
await toolbar.locator('#bookmarks').getByRole('button', { name: '移除' }).click()
|
||||
await toolbar.getByText('暂无书签').waitFor()
|
||||
await toolbar.getByRole('button', { name: '书签与历史', exact: true }).click()
|
||||
await toolbar.getByRole('button', { name: '新建标签页' }).click()
|
||||
await toolbar.getByRole('button', { name: '新标签页', exact: true }).waitFor()
|
||||
await address.fill('file:///C:/Windows/win.ini')
|
||||
|
||||
Reference in new issue
Block a user