为 Chromium 浏览器加入书签和持久历史
This commit is contained in:
1 parent
c27ad52dab
commit
ae04cc9494
9 files changed
+200
-6
No files matched your search
+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()
|
||||
|
||||
Reference in new issue
Block a user