奇怪的用法增加了:通过渲染进程在思源中拉起子应用

本贴最后更新于 964 天前,其中的信息可能已经时移世异

image.png

这下面这一排就是在渲染进程中拉起的子应用窗口

image.png

打开的效果类似这样,这是 kityminder

image.png

这是以源码方式直接伺服的魔改思源界面,注意右边,他有一些 api。

还有嵌入到思源中运行的 esm.sh(一个 es module 的 cdn 吧算是),vite 等等

主要的代码实现在下面:

import Button from "./UI/button.js" const { EventEmitter } = require("stream") const { app, BrowserWindow, shell, Menu, screen, nativeTheme, ipcMain, globalShortcut, Tray, } = require("@electron/remote") const path = require("path") const fs = require("fs") const appDir = path.dirname(app.getAppPath()) let createServiceHost = (icon) => { let serverHost = new BrowserWindow( { width: screen.getPrimaryDisplay().size.width / 2, height: screen.getPrimaryDisplay().workAreaSize.height / 2, frame: true, icon: icon||path.join(appDir, 'stage', 'icon-large.png'), show: false, webPreferences: { preload: 'D:\\newSiyuan\\conf\\appearance\\themes\\naive\\script\\serviceHandler\\UI\\preload.js', nativeWindowOpen: true, nodeIntegration: true, webviewTag: true, webSecurity: false, contextIsolation: false, }, } ) window.addEventListener('beforeunload', () => { serverHost.close() serverHost.webContents.destroy serverHost.webContents.send("重新加载", {}) }) serverHost.webContents.send("setGlobal", { key: "appDir", value: appDir }) require("@electron/remote").require("@electron/remote/main").enable(serverHost.webContents) return serverHost } export default class naiveService extends EventEmitter { constructor(_path, option) { super() this.option = option if (_path.endsWith('index.html')) { _path = path.dirname(_path) } this.hosts = [] this.id = _path if(fs.existsSync(path.join(_path, "favicon.png"))){ this.icon = path.join(_path, "favicon.png") }else if(fs.existsSync(path.join(_path, "favicon.ico"))){ this.icon = path.join(_path, "favicon.ico") }else if(fs.existsSync(path.join(_path, "favicon.svg"))){ this.icon = path.join(_path, "favicon.svg") }else{ this.icon = path.join(appDir, 'stage', 'icon-large.png') } let serverHost = createServiceHost(this.icon) this.serverHost = serverHost this.button = new Button(this.id, this.icon, this) serverHost.webContents.on('did-finish-load', () => { if (!serverHost) { return } }) ipcMain.on('服务启动完成', (event, msg) => { try { console.log(msg) this.emit("服务启动完成",msg) } catch (e) { console.log(e) } }) this.自杀计数 = 0 ipcMain.on(this.id, (event, msg) => { this.自杀计数 = 0 }) this.path = _path if (option.url) { this.url = option.url } else { this.url = this.path } this.加载服务界面() this.加载脚本() this.开始自杀计数() if (option && option.show) { this.是否显示界面 = true this.显示界面() } this.serverHost.webContents.on("close", () => { this.destoyed = true this.button.remove() if (option && option.stayAlive) { this.重新初始化() } }) this.serverHost.webContents.on("destoyed", () => { this.destoyed = true this.button.remove() if (option && option.stayAlive) { this.重新初始化() } }) let options = { persistent: true, recursive: true, }; fs.watch( this.path,options,(type,fileName)=>{ this.文件被修改=true this.自杀计数 = 10 } ) } //这里是让子应用每秒钟向主窗口发送消息证明自己还没有卡死,如果卡死并且设置了stayAlive的话, 子应用会自动被重新拉起 自杀计数器 = () => { this.自杀计数 += 1 if (this.destoyed) { this.自杀计数 = -1 if (this.option && this.option.stayAlive) { this.重新初始化() } return } if (this.自杀计数 >= 5) { if(this.文件被修改){ console.log(`服务${this.path}源代码改动,重新启动`) this.文件被修改=false }else{ console.log(`服务${this.path}失联,重新启动`) } if (this.serverHost && !this.destoyed) { this.serverHost.close() this.serverHost = undefined } this.自杀计数 = -1 clearInterval(this.自杀计数器) this.重新初始化() } } 开始自杀计数() { let that = this clearInterval(that.自杀计数器) setInterval( that.自杀计数器, 1000 ) } 重新初始化() { if (!this.destoyed) { return } let serverHost = createServiceHost() this.serverHost = serverHost serverHost.webContents.on('did-finish-load', () => { if (!serverHost) { return } }) ipcMain.on('服务启动完成', (event, msg) => { try { this.emit("服务启动完成",msg) console.log(msg) } catch (e) { console.log(e) } }) this.自杀计数 = 0 this.加载服务界面() if (this.是否显示界面) { this.显示界面() } this.button.setColor('success') this.destoyed = false this.serverHost.webContents.on("close", () => { this.destoyed = true this.button.remove() }) this.serverHost.webContents.on("destoyed", () => { this.destoyed = true this.button.remove() }) } 改变可见性() { if (this.destoyed) { return } else { if (this.serverHost.isVisible()) { this.serverHost.hide() this.button.setColor('info') } else { this.serverHost.show() this.button.setColor('success') } } } 加载服务界面() { if (fs.existsSync(this.path+'/index.html')) { console.log(this.option) try { if(this.option&&!this.option.widget){ this.serverHost.loadURL(this.path+'/index.html') }else{ console.log("http://"+"127.0.0.1:"+window.location.port+`/widgets/${this.path.split('\\').pop()}/index.html`) let url =new URL("http://"+"127.0.0.1:"+window.location.port+`/widgets/${this.path.split('\\').pop()}/index.html`) this.serverHost.loadURL(url.href) } this.serverHost.send("id", this.id) } catch (e) { console.error(e) } } else { throw (`${this.path}下不存在index或者index.html,请检查`) } } 加载脚本(filePath) { if (fs.existsSync(filePath)) { this.serverHost.webContents.send('加载脚本', filePath) } } 显示界面() { if (!this.serverHost.isVisible()) { this.serverHost.show() } } 隐藏界面() { if (this.serverHost.isVisible()) { this.serverHost.hide() } } }

这玩意的主要作用就是将一些可能造成主界面卡顿的大型代码丢到另一个渲染进程去执行,或者给挂件更独立的运行环境,拉起 vite、esm.sh 之类纯属我自己犯懒......

奥,那个圆形的按钮自己实现一下就可以了奥

  • 思源笔记

    思源笔记是一款隐私优先的个人知识管理系统,支持完全离线使用,同时也支持端到端加密同步。

    融合块、大纲和双向链接,重构你的思维。

    26072 引用 • 108232 回帖 • 1 关注

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...

推荐标签 标签

  • 游戏

    沉迷游戏伤身,强撸灰飞烟灭。

    187 引用 • 831 回帖
  • Ngui

    Ngui 是一个 GUI 的排版显示引擎和跨平台的 GUI 应用程序开发框架,基于
    Node.js / OpenGL。目标是在此基础上开发 GUI 应用程序可拥有开发 WEB 应用般简单与速度同时兼顾 Native 应用程序的性能与体验。

    7 引用 • 9 回帖 • 402 关注
  • V2Ray
    1 引用 • 15 回帖 • 2 关注
  • danl
    174 关注
  • OpenStack

    OpenStack 是一个云操作系统,通过数据中心可控制大型的计算、存储、网络等资源池。所有的管理通过前端界面管理员就可以完成,同样也可以通过 Web 接口让最终用户部署资源。

    10 引用 • 2 关注
  • 支付宝

    支付宝是全球领先的独立第三方支付平台,致力于为广大用户提供安全快速的电子支付/网上支付/安全支付/手机支付体验,及转账收款/水电煤缴费/信用卡还款/AA 收款等生活服务应用。

    29 引用 • 347 回帖
  • Caddy

    Caddy 是一款默认自动启用 HTTPS 的 HTTP/2 Web 服务器。

    10 引用 • 54 回帖 • 180 关注
  • Ruby

    Ruby 是一种开源的面向对象程序设计的服务器端脚本语言,在 20 世纪 90 年代中期由日本的松本行弘(まつもとゆきひろ/Yukihiro Matsumoto)设计并开发。在 Ruby 社区,松本也被称为马茨(Matz)。

    7 引用 • 31 回帖 • 260 关注
  • 互联网

    互联网(Internet),又称网际网络,或音译因特网、英特网。互联网始于 1969 年美国的阿帕网,是网络与网络之间所串连成的庞大网络,这些网络以一组通用的协议相连,形成逻辑上的单一巨大国际网络。

    98 引用 • 367 回帖
  • 脑图

    脑图又叫思维导图,是表达发散性思维的有效图形思维工具 ,它简单却又很有效,是一种实用性的思维工具。

    32 引用 • 99 回帖
  • Android

    Android 是一种以 Linux 为基础的开放源码操作系统,主要使用于便携设备。2005 年由 Google 收购注资,并拉拢多家制造商组成开放手机联盟开发改良,逐渐扩展到到平板电脑及其他领域上。

    336 引用 • 324 回帖 • 1 关注
  • Postman

    Postman 是一款简单好用的 HTTP API 调试工具。

    4 引用 • 3 回帖 • 2 关注
  • ActiveMQ

    ActiveMQ 是 Apache 旗下的一款开源消息总线系统,它完整实现了 JMS 规范,是一个企业级的消息中间件。

    19 引用 • 13 回帖 • 675 关注
  • Sphinx

    Sphinx 是一个基于 SQL 的全文检索引擎,可以结合 MySQL、PostgreSQL 做全文搜索,它可以提供比数据库本身更专业的搜索功能,使得应用程序更容易实现专业化的全文检索。

    1 引用 • 228 关注
  • Vim

    Vim 是类 UNIX 系统文本编辑器 Vi 的加强版本,加入了更多特性来帮助编辑源代码。Vim 的部分增强功能包括文件比较(vimdiff)、语法高亮、全面的帮助系统、本地脚本(Vimscript)和便于选择的可视化模式。

    29 引用 • 66 回帖 • 1 关注
  • 外包

    有空闲时间是接外包好呢还是学习好呢?

    26 引用 • 233 回帖
  • Webswing

    Webswing 是一个能将任何 Swing 应用通过纯 HTML5 运行在浏览器中的 Web 服务器,详细介绍请看 将 Java Swing 应用变成 Web 应用

    1 引用 • 15 回帖 • 644 关注
  • 导航

    各种网址链接、内容导航。

    45 引用 • 177 回帖 • 2 关注
  • 浅吟主题

    Jeffrey Chen 制作的思源笔记主题,项目仓库:https://github.com/TCOTC/Whisper

    1 引用 • 28 回帖 • 1 关注
  • Ant-Design

    Ant Design 是服务于企业级产品的设计体系,基于确定和自然的设计价值观上的模块化解决方案,让设计者和开发者专注于更好的用户体验。

    17 引用 • 23 回帖 • 3 关注
  • 禅道

    禅道是一款国产的开源项目管理软件,她的核心管理思想基于敏捷方法 scrum,内置了产品管理和项目管理,同时又根据国内研发现状补充了测试管理、计划管理、发布管理、文档管理、事务管理等功能,在一个软件中就可以将软件研发中的需求、任务、bug、用例、计划、发布等要素有序的跟踪管理起来,完整地覆盖了项目管理的核心流程。

    10 引用 • 15 回帖 • 5 关注
  • Kotlin

    Kotlin 是一种在 Java 虚拟机上运行的静态类型编程语言,由 JetBrains 设计开发并开源。Kotlin 可以编译成 Java 字节码,也可以编译成 JavaScript,方便在没有 JVM 的设备上运行。在 Google I/O 2017 中,Google 宣布 Kotlin 成为 Android 官方开发语言。

    19 引用 • 33 回帖 • 78 关注
  • 百度

    百度(Nasdaq:BIDU)是全球最大的中文搜索引擎、最大的中文网站。2000 年 1 月由李彦宏创立于北京中关村,致力于向人们提供“简单,可依赖”的信息获取方式。“百度”二字源于中国宋朝词人辛弃疾的《青玉案·元夕》词句“众里寻他千百度”,象征着百度对中文信息检索技术的执著追求。

    63 引用 • 785 回帖 • 74 关注
  • Thymeleaf

    Thymeleaf 是一款用于渲染 XML/XHTML/HTML5 内容的模板引擎。类似 Velocity、 FreeMarker 等,它也可以轻易的与 Spring 等 Web 框架进行集成作为 Web 应用的模板引擎。与其它模板引擎相比,Thymeleaf 最大的特点是能够直接在浏览器中打开并正确显示模板页面,而不需要启动整个 Web 应用。

    11 引用 • 19 回帖 • 394 关注
  • 招聘

    哪里都缺人,哪里都不缺人。

    188 引用 • 1057 回帖 • 2 关注
  • 友情链接

    确认过眼神后的灵魂连接,站在链在!

    24 引用 • 373 回帖
  • Spring

    Spring 是一个开源框架,是于 2003 年兴起的一个轻量级的 Java 开发框架,由 Rod Johnson 在其著作《Expert One-On-One J2EE Development and Design》中阐述的部分理念和原型衍生而来。它是为了解决企业应用开发的复杂性而创建的。框架的主要优势之一就是其分层架构,分层架构允许使用者选择使用哪一个组件,同时为 JavaEE 应用程序开发提供集成的框架。

    947 引用 • 1460 回帖 • 1 关注