MCPcopy
hub / github.com/ZToolsCenter/ZTools / preloadPlugin

Method preloadPlugin

src/main/managers/pluginManager.ts:937–1003  ·  view source on GitHub ↗

* 后台预加载插件(不显示在主窗口中,仅创建 WebContentsView 并缓存) * 用于"跟随主程序同时启动运行"功能

(pluginPath: string)

Source from the content-addressed store, hash-verified

935 * 用于"跟随主程序同时启动运行"功能
936 */
937 public async preloadPlugin(pluginPath: string): Promise<void> {
938 if (!this.mainWindow) return
939
940 // 如果已经在缓存中,跳过
941 const existing = this.pluginViews.find((v) => v.path === pluginPath)
942 if (existing) {
943 console.log('[Plugin] 插件已在运行中,跳过预加载:', pluginPath)
944 return
945 }
946
947 try {
948 console.log('[Plugin] 开始后台预加载插件:', { pluginPath })
949 const pluginInfoFromDB = this.fetchPluginInfoFromDB(pluginPath)
950 const pluginConfig = this.readPluginConfig(pluginPath)
951 const isDevelopment = !!pluginInfoFromDB?.isDevelopment
952 const effectiveName = pluginInfoFromDB?.name || pluginConfig.name
953 const { pluginUrl } = this.resolvePluginUrl(pluginPath, pluginConfig, isDevelopment)
954
955 const preloadPath = pluginConfig.preload
956 ? path.join(pluginPath, pluginConfig.preload)
957 : undefined
958
959 const sess = await this.setupPluginSession(effectiveName, pluginPath)
960 const view = this.createPluginWebContentsView(sess, preloadPath)
961
962 // 注册事件监听
963 this.registerMainWindowPluginEvents(view, pluginPath)
964
965 // 缓存视图(但不添加到主窗口、不设为当前插件)
966 const logoUrl = this.buildPluginLogoUrl(pluginPath, pluginConfig.logo)
967 const pluginInfo: PluginViewInfo = {
968 path: pluginPath,
969 name: effectiveName,
970 view,
971 subInputPlaceholder: '搜索',
972 subInputVisible: false,
973 logo: logoUrl,
974 isDevelopment,
975 backgroundRunning: !!pluginConfig.pluginSetting?.backgroundRunning,
976 single: pluginConfig.pluginSetting?.single
977 }
978 this.pluginViews.push(pluginInfo)
979
980 // 预加载后视图处于后台态,按策略设置节流(默认启用,backgroundRunning=true 例外)
981 this.applyBackgroundThrottlingByPolicy(view, pluginPath, true)
982
983 // 加载插件 URL
984 view.webContents.loadURL(pluginUrl)
985
986 view.webContents.once('dom-ready', () => {
987 this.assemblyCoordinator.markDomReady(view.webContents.id)
988 view.webContents.insertCSS(GLOBAL_SCROLLBAR_CSS)
989 console.log('[Plugin] 后台预加载插件完成:', {
990 pluginName: effectiveName,
991 pluginPath,
992 webContentsId: view.webContents.id
993 })
994 })

Callers 3

queryMainPushMethod · 0.95
index.tsFile · 0.80
ensurePluginToolReadyMethod · 0.80

Calls 9

fetchPluginInfoFromDBMethod · 0.95
readPluginConfigMethod · 0.95
resolvePluginUrlMethod · 0.95
setupPluginSessionMethod · 0.95
buildPluginLogoUrlMethod · 0.95
markDomReadyMethod · 0.80

Tested by

no test coverage detected