增加 Chromium 浏览器逐站权限管理
This commit is contained in:
1 parent
0b89634af2
commit
f4ea1f8e03
8 files changed
+165
-6
No files matched your search
+29
-3
@@ -3,6 +3,7 @@ import type { DownloadItem, Session } from 'electron'
|
||||
import { randomUUID } from 'node:crypto'
|
||||
import { join } from 'node:path'
|
||||
import { clearDownloads, clearHistory, library, recordDownload, removeBookmark, toggleBookmark, visit } from './browser-library'
|
||||
import { clearGrants, configurePermissions, grants, setGrant } from './browser-permissions'
|
||||
|
||||
const browserURL = 'xhchat://app/browser'
|
||||
const height = 102
|
||||
@@ -43,6 +44,7 @@ function address(value: string): URL | undefined {
|
||||
function state(browser: BrowserWindowState) {
|
||||
const contents = browser.active?.view.webContents
|
||||
const collection = browser.privateMode ? { bookmarks: [], history: [], downloads: [] } : library()
|
||||
const origin = website(contents?.getURL() ?? '')?.origin ?? ''
|
||||
return {
|
||||
privateMode: browser.privateMode,
|
||||
active: browser.active?.id,
|
||||
@@ -53,6 +55,8 @@ function state(browser: BrowserWindowState) {
|
||||
loading: contents?.isLoading() ?? false,
|
||||
error: browser.active?.error ?? '',
|
||||
libraryMode: browser.libraryMode,
|
||||
origin,
|
||||
grants: browser.privateMode || !origin ? {} : grants(origin),
|
||||
bookmarked: collection.bookmarks.some((item) => item.url === contents?.getURL()),
|
||||
...collection,
|
||||
downloads: browser.privateMode ? [] : [
|
||||
@@ -179,8 +183,8 @@ const configured = new WeakSet<Session>()
|
||||
function configureSites(sites: Session, privateMode: boolean) {
|
||||
if (configured.has(sites)) return
|
||||
configured.add(sites)
|
||||
sites.setPermissionRequestHandler((_contents, _permission, callback) => callback(false))
|
||||
sites.setPermissionCheckHandler(() => false)
|
||||
configurePermissions(sites, privateMode, (contents) => [...windows.values()].some((browser) =>
|
||||
!browser.libraryMode && browser.active?.view.webContents === contents && browser.window.isVisible()))
|
||||
const defaultUA = sites.getUserAgent()
|
||||
.replace(/\sElectron\/\S+/g, '')
|
||||
.replace(/\s(?:XHChat|xhchat)\/\S+/g, '')
|
||||
@@ -320,6 +324,25 @@ export function registerBrowserHandlers(trusted: Session) {
|
||||
if (item?.path) shell.showItemInFolder(item.path)
|
||||
break
|
||||
}
|
||||
case 'siteGrant': {
|
||||
if (browser.privateMode || !browser.libraryMode || !current || typeof value !== 'string') break
|
||||
const [origin, permission, desired, ...rest] = value.split('\n')
|
||||
if (origin !== current.origin || rest.length || !['allow', 'block'].includes(desired)) {
|
||||
throw new Error('网页已切换,请重新检查网站权限')
|
||||
}
|
||||
setGrant(origin, permission, desired === 'allow')
|
||||
if (desired === 'block') {
|
||||
for (const candidate of windows.values()) {
|
||||
if (candidate.sites !== browser.sites) continue
|
||||
for (const tab of candidate.tabs) {
|
||||
const page = tab.view.webContents
|
||||
if (website(page.getURL())?.origin === origin) page.reload()
|
||||
}
|
||||
}
|
||||
}
|
||||
updateNormalWindows()
|
||||
break
|
||||
}
|
||||
case 'clearDownloads': {
|
||||
if (browser.privateMode) break
|
||||
clearDownloads()
|
||||
@@ -344,7 +367,10 @@ export function registerBrowserHandlers(trusted: Session) {
|
||||
})
|
||||
if (answer.response === 1) {
|
||||
await browser.sites.clearData()
|
||||
if (!browser.privateMode) clearHistory()
|
||||
if (!browser.privateMode) {
|
||||
clearHistory()
|
||||
clearGrants()
|
||||
}
|
||||
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