From 68944671c5ae1e92957f6651ff85729cd986d5c3 Mon Sep 17 00:00:00 2001 From: npmrun <1549469775@qq.com> Date: Thu, 6 Feb 2025 20:04:56 +0800 Subject: [PATCH] add --- src/main/App copy.ts | 12 +++++------ src/main/App.ts | 14 ++++++++++--- src/main/_iocClass.ts | 4 ++++ src/main/commands/TabsCommand.ts | 11 ++++++++-- src/main/modules/_ioc.ts | 2 ++ src/main/modules/tabs/Tab.ts | 34 ++++++++++++++++++++++++++---- src/main/modules/tabs/index.ts | 24 ++++++++++++++++++++- src/main/modules/zephyr/index.ts | 45 ++++++++++++++++++++++++++++++++++++++++ src/renderer/src/App.vue | 28 +++++++++++++++++++------ 9 files changed, 152 insertions(+), 22 deletions(-) create mode 100644 src/main/modules/zephyr/index.ts diff --git a/src/main/App copy.ts b/src/main/App copy.ts index bba0a02..9d0584c 100644 --- a/src/main/App copy.ts +++ b/src/main/App copy.ts @@ -5,7 +5,7 @@ import Api from "./modules/api" import WindowManager from "./modules/window-manager" import { app, nativeTheme, protocol, WebContentsView } from "electron" import { electronApp } from "@electron-toolkit/utils" -import Tabs from "./modules/tabs/Tabs" +// import Tabs from "./modules/tabs/Tabs" import { getFileUrl } from "./utils" import BaseClass from "./base/base" @@ -39,21 +39,21 @@ class App extends BaseClass { // private _db: DB private _Api: Api private _windowManager: WindowManager - private _tabs: Tabs + // private _tabs: Tabs constructor( // @inject(Setting) setting: Setting, // @inject(DB) db: DB, @inject(Api) Api: Api, @inject(WindowManager) windowManager: WindowManager, - @inject(Tabs) tabs: Tabs, + // @inject(Tabs) tabs: Tabs, ) { super() // this._setting = setting // this._db = db this._Api = Api this._windowManager = windowManager - this._tabs = tabs + // this._tabs = tabs } async init() { @@ -155,8 +155,8 @@ class App extends BaseClass { // listenResize() // mainWindow!.addListener("resize", listenResize) - this._tabs.add("https://baidu.com", true) - this._tabs.add("https://zhihu.com") + // this._tabs.add("https://baidu.com", true) + // this._tabs.add("https://zhihu.com") return mainWindow } } diff --git a/src/main/App.ts b/src/main/App.ts index 65a3d93..c61b521 100644 --- a/src/main/App.ts +++ b/src/main/App.ts @@ -5,11 +5,11 @@ import Api from "./modules/api" import WindowManager from "./modules/window-manager" import { app, nativeTheme, protocol } from "electron" import { electronApp } from "@electron-toolkit/utils" -import Tabs from "./modules/tabs" import Command from "./modules/commands" import BaseClass from "./base/base" import IOC from "./_ioc" import DB from "./modules/db" +import Zephyr from "./modules/zephyr" protocol.registerSchemesAsPrivileged([ // { @@ -29,6 +29,14 @@ protocol.registerSchemesAsPrivileged([ supportFetchAPI: true, }, }, + { + scheme: "zephyr", + privileges: { + standard: true, + secure: true, + supportFetchAPI: true, + }, + }, ]) @injectable() @@ -45,7 +53,7 @@ class App extends BaseClass { @inject(Command) private _Command: Command, @inject(DB) private _DB: DB, @inject(WindowManager) private _WindowManager: WindowManager, - @inject(Tabs) private _Tabs: Tabs, + @inject(Zephyr) private _Zephyr: Zephyr, ) { super() } @@ -56,10 +64,10 @@ class App extends BaseClass { this._WindowManager.init() app.whenReady().then(() => { this._Api.init() + this._Zephyr.init() electronApp.setAppUserModelId("top.xieyaxin") this._WindowManager.showMainWindow() const mainWindow = this._WindowManager.getMainWindow() - this._Tabs.init(mainWindow) if (mainWindow) { nativeTheme.themeSource = "light" mainWindow.setTitleBarOverlay({ diff --git a/src/main/_iocClass.ts b/src/main/_iocClass.ts index ecd6e0a..74d1bd9 100644 --- a/src/main/_iocClass.ts +++ b/src/main/_iocClass.ts @@ -3,6 +3,10 @@ import BaseClass from "./base/base" import { destroyAll, _ioc } from "./_ioc" class IOC extends BaseClass { + init() { + // TODO + } + destroy() { destroyAll() } diff --git a/src/main/commands/TabsCommand.ts b/src/main/commands/TabsCommand.ts index 674277b..f75647b 100644 --- a/src/main/commands/TabsCommand.ts +++ b/src/main/commands/TabsCommand.ts @@ -14,6 +14,15 @@ class TabsCommand { this._Tabs.events.addListener("update", this.listenerTabActive) } + init() { + const mainWindow = this._WindowManager.getMainWindow() + this._Tabs.init(mainWindow) + } + + bindElement(rect) { + this._Tabs.updateRect(rect) + } + reload() { this._WindowManager.getMainWindow()?.reload() } @@ -31,8 +40,6 @@ class TabsCommand { } nagivate(index: number, url: string) { - console.log(`跳转${index}:${url}`) - this._Tabs.navigate(+index, url) } diff --git a/src/main/modules/_ioc.ts b/src/main/modules/_ioc.ts index 1c6cbc5..830a4d7 100644 --- a/src/main/modules/_ioc.ts +++ b/src/main/modules/_ioc.ts @@ -5,9 +5,11 @@ import { Api } from "./api" import { WindowManager } from "./window-manager" import { Tabs } from "./tabs" import Commands from "./commands" +import Zephyr from "./zephyr" const modules = new ContainerModule(bind => { bind(Setting).toConstantValue(new Setting()) + bind(Zephyr).toSelf().inSingletonScope() bind(Api).toSelf().inSingletonScope() bind(WindowManager).toSelf().inSingletonScope() bind(Commands).toSelf().inSingletonScope() diff --git a/src/main/modules/tabs/Tab.ts b/src/main/modules/tabs/Tab.ts index cf4bb06..4f21181 100644 --- a/src/main/modules/tabs/Tab.ts +++ b/src/main/modules/tabs/Tab.ts @@ -2,7 +2,7 @@ import { BrowserWindow, WebContentsView, WebPreferences } from "electron" import { join } from "node:path" import BaseClass from "vc/base/base" import _debug from "debug" -import { Layout } from "./Constant" +// import { Layout } from "./Constant" import FuckHTML from "res/fuck.html?asset" import { fileURLToPath } from "node:url" @@ -13,6 +13,13 @@ interface IOption { active: boolean } +interface IRect { + x: number + y: number + width: number + height: number +} + class Tab extends BaseClass { init() { // TODO @@ -28,6 +35,12 @@ class Tab extends BaseClass { public visible: boolean = false private webContentsView: WebContentsView | null = null private curWindow: BrowserWindow | null = null + private curRect: { + x: number + y: number + width: number + height: number + } | null = null private defaultOptions: IOption = { url: "", @@ -44,7 +57,7 @@ class Tab extends BaseClass { return this._events } - constructor(options = {}, window: BrowserWindow) { + constructor(options = {}, window: BrowserWindow, curRect: IRect) { super() this.listenResize = this.listenResize.bind(this) this.options = { @@ -54,6 +67,7 @@ class Tab extends BaseClass { this.url = this.getUrl(this.options.url) this.showUrl = this.options.url this.curWindow = window + this.curRect = curRect this.setActive(this.options.active) } destroyTimer: NodeJS.Timeout | null = null @@ -225,8 +239,20 @@ class Tab extends BaseClass { if (!this.webContentsView) { return } - const size = this.curWindow.getContentSize() - this.webContentsView.setBounds(Layout(size[0], size[1])) + if (!this.curRect) { + return + } + this.webContentsView.setBounds(this.curRect) + // const size = this.curWindow.getContentSize() + // this.webContentsView.setBounds(Layout(size[0], size[1])) + } + + updateRect(curRect: IRect) { + this.curRect = curRect + if (!this.webContentsView) { + return + } + this.webContentsView.setBounds(this.curRect) } navigate(url: string) { diff --git a/src/main/modules/tabs/index.ts b/src/main/modules/tabs/index.ts index bc77448..924548d 100644 --- a/src/main/modules/tabs/index.ts +++ b/src/main/modules/tabs/index.ts @@ -4,6 +4,13 @@ import _debug from "debug" import { BrowserWindow } from "electron" import EventEmitter from "events" +interface IRect { + x: number + y: number + width: number + height: number +} + const debug = _debug("app:tabs") class Tabs extends BaseClass { @@ -14,6 +21,13 @@ class Tabs extends BaseClass { public events = new EventEmitter() + private curRect: { + x: number + y: number + width: number + height: number + } | null = null + constructor() { super() } @@ -24,8 +38,16 @@ class Tabs extends BaseClass { this.add("about:blank", true, mainWindow) } + updateRect(curRect: IRect) { + this.curRect = curRect + this._tabs.forEach(tab => { + tab.updateRect(curRect) + }) + } + add(url: string, active: boolean, win: BrowserWindow) { - const tab = new Tab({ url }, win) + if (!this.curRect) throw new Error("请绑定区域显示") + const tab = new Tab({ url }, win, this.curRect) tab.events.on("window-open", ev => { debug(ev) this.add(ev.url, true, win) diff --git a/src/main/modules/zephyr/index.ts b/src/main/modules/zephyr/index.ts new file mode 100644 index 0000000..96264c8 --- /dev/null +++ b/src/main/modules/zephyr/index.ts @@ -0,0 +1,45 @@ +import { session, net } from "electron" +import { inject, injectable } from "inversify" +import IOC from "vc/_ioc" +import BaseClass from "vc/base/base" + +@injectable() +class Zephyr extends BaseClass { + constructor(@inject(IOC) private _IOC: IOC) { + super() + this.interceptHandlerZephyr = this.interceptHandlerZephyr.bind(this) + } + + destroy() { + // TODO + } + init(partition?: string) { + const ses = partition ? session.fromPartition(partition) : session.defaultSession + ses.protocol.handle("zephyr", this.interceptHandlerZephyr) + console.log(32423) + } + async interceptHandlerZephyr(request: Request) { + if (request.url.startsWith("zephyr://")) { + let curPath = request.url.replace(/^zephyr:\/\//, "") + let isPathRead = false + if (curPath.startsWith("$path/")) { + isPathRead = true + curPath = curPath.replace(/^\$path\//, "") + } + if (isPathRead) { + console.log("安全读取本地目录") + // 检查文件的安全性 + const headers: HeadersInit = {} + headers["content-type"] = "text/txt" + return new Response(curPath, { + status: 200, + headers: Object.keys(headers).length ? headers : undefined, + }) + } + } + return net.fetch(request.url, request) + } +} + +export default Zephyr +export { Zephyr } diff --git a/src/renderer/src/App.vue b/src/renderer/src/App.vue index 354e5e5..b2c9e01 100644 --- a/src/renderer/src/App.vue +++ b/src/renderer/src/App.vue @@ -1,16 +1,29 @@