Эх сурвалжийг харах

能成功触发引入逻辑了。

skyffire 1 жил өмнө
parent
commit
ffcf681c0c
1 өөрчлөгдсөн 30 нэмэгдсэн , 27 устгасан
  1. 30 27
      example/electron.js

+ 30 - 27
example/electron.js

@@ -1,4 +1,4 @@
-const { app, BrowserWindow, Menu, protocol } = require('electron');
+const { app, BrowserWindow, Menu, session } = require('electron');
 const path = require('path');
 const fs = require('fs');
 const crypto = require('crypto');
@@ -41,29 +41,6 @@ function createWindow() {
   const indexRelativePath = path.relative(process.resourcesPath, indexPath).replace(/\\/g, '/');
   memoryCache[indexRelativePath] = fs.readFileSync(indexPath);
 
-  // Register a custom protocol to serve content from memory
-  protocol.registerBufferProtocol('file', (request, callback) => {
-    const url = request.url.substr(7); // Remove 'file://' prefix
-    const relativePath = path.relative(process.resourcesPath, url).replace(/\\/g, '/');
-    console.log(`Intercepting request for: ${relativePath}`);
-
-    if (memoryCache[relativePath]) {
-      let mimeType = 'text/plain';
-      if (relativePath.endsWith('.js')) {
-        mimeType = 'application/javascript';
-      } else if (relativePath.endsWith('.css')) {
-        mimeType = 'text/css';
-      } else if (relativePath.endsWith('.html')) {
-        mimeType = 'text/html';
-      }
-      console.log(`Serving from memory: ${relativePath} as ${mimeType}`);
-      callback({ mimeType: mimeType, data: memoryCache[relativePath] });
-    } else {
-      console.error(`File not found in memory: ${relativePath}`);
-      callback({ error: -6 }); // FILE_NOT_FOUND
-    }
-  });
-
   const win = new BrowserWindow({
     width: 1600,
     height: 900,
@@ -75,8 +52,9 @@ function createWindow() {
   });
 
   // Load the index.html from memory using a data URL
-  const indexHtmlContent = memoryCache[indexRelativePath].toString();
-  win.loadURL('data:text/html;charset=utf-8,' + encodeURIComponent(indexHtmlContent));
+  // const indexHtmlContent = memoryCache[indexRelativePath].toString();
+  // win.loadURL('data:text/html;charset=utf-8,' + encodeURIComponent(indexHtmlContent));
+  win.loadFile('index.html');
 
   // 打开调试工具
   win.webContents.openDevTools();
@@ -104,7 +82,32 @@ function createWindow() {
   Menu.setApplicationMenu(menu);
 }
 
-app.on('ready', createWindow);
+app.on('ready', () => {
+  // Intercept file requests and serve from memory
+  session.defaultSession.webRequest.onBeforeRequest((details, callback) => {
+    const url = details.url.substr(7); // Remove 'file://' prefix
+    const relativePath = path.relative(process.resourcesPath, url).replace(/\\/g, '/');
+    console.log(`Intercepting request for: ${relativePath}`);
+
+    if (memoryCache[relativePath]) {
+      let mimeType = 'text/plain';
+      if (relativePath.endsWith('.js')) {
+        mimeType = 'application/javascript';
+      } else if (relativePath.endsWith('.css')) {
+        mimeType = 'text/css';
+      } else if (relativePath.endsWith('.html')) {
+        mimeType = 'text/html';
+      }
+      console.log(`Serving from memory: ${relativePath} as ${mimeType}`);
+      callback({ mimeType: mimeType, data: memoryCache[relativePath] });
+    } else {
+      console.error(`File not found in memory: ${relativePath}`);
+      callback({ cancel: false });
+    }
+  });
+
+  createWindow();
+});
 
 app.on('window-all-closed', () => {
   if (process.platform !== 'darwin') {