skyffire 1 жил өмнө
parent
commit
3776d8b16c
1 өөрчлөгдсөн 38 нэмэгдсэн , 28 устгасан
  1. 38 28
      example/electron.js

+ 38 - 28
example/electron.js

@@ -2,14 +2,12 @@ const { app, BrowserWindow, Menu, protocol } = require('electron');
 const path = require('path');
 const fs = require('fs');
 const crypto = require('crypto');
-const mime = require('mime');
 
 const algorithm = 'aes-256-ctr';
 const password = 'skyfffire-password';
 const key = crypto.createHash('sha256').update(password).digest();
 let memoryCache = {};
 
-
 // Function to decrypt a file and store its content in memory
 function decryptFile(filePath) {
   console.log(`Decrypting: ${filePath}`);
@@ -37,9 +35,12 @@ function decryptDirectory(directoryPath) {
   });
 }
 
-
-
 function createWindow() {
+  // 把index.html先放到内存里
+  const relativePath = path.join(process.resourcesPath, 'app.asar', 'index.html');
+  memoryCache[relativePath] = fs.readFileSync(relativePath);
+  console.log(`index: ${relativePath}, content: ${memoryCache[relativePath].slice(0, 100)}`)
+
   const directoryToDecrypt = path.join(process.resourcesPath, 'app.asar', 'static');
   console.log(`Decrypting directory: ${directoryToDecrypt}`);
   decryptDirectory(directoryToDecrypt);
@@ -49,7 +50,15 @@ function createWindow() {
     const url = request.url.substr(7); // Remove 'file://' prefix
     const relativePath = path.relative(process.resourcesPath, url);
     if (memoryCache[relativePath]) {
-      callback({ mimeType: 'text/javascript', data: 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';
+      }
+      callback({ mimeType: mimeType, data: memoryCache[relativePath] });
     } else {
       callback({ error: -6 }); // FILE_NOT_FOUND
     }
@@ -65,36 +74,37 @@ function createWindow() {
     icon: path.join(__dirname, 'favicon.ico'), // 设置窗口图标
     webPreferences: {
       nodeIntegration: false,
-      contextIsolation: true
+      contextIsolation: true,
     },
   });
 
-  win.loadFile('index.html');
+  // Load the index.html from disk
+  win.loadFile(path.join(process.resourcesPath, 'app.asar', 'index.html'));
 
   // 打开调试工具
   win.webContents.openDevTools();
 
-  // // 创建菜单模板,只包含一个刷新按钮
-  // const menuTemplate = [
-  //   {
-  //     label: 'View',
-  //     submenu: [
-  //       {
-  //         label: 'Reload',
-  //         accelerator: 'CmdOrCtrl+R',
-  //         click: () => {
-  //           mainWindow.reload();
-  //         },
-  //       },
-  //     ],
-  //   },
-  // ];
-  //
-  // // 创建菜单
-  // const menu = Menu.buildFromTemplate(menuTemplate);
-  //
-  // // 设置应用程序的菜单
-  // Menu.setApplicationMenu(menu);
+  // 创建菜单模板,只包含一个刷新按钮
+  const menuTemplate = [
+    {
+      label: 'View',
+      submenu: [
+        {
+          label: 'Reload',
+          accelerator: 'CmdOrCtrl+R',
+          click: () => {
+            win.reload();
+          },
+        },
+      ],
+    },
+  ];
+
+  // 创建菜单
+  const menu = Menu.buildFromTemplate(menuTemplate);
+
+  // 设置应用程序的菜单
+  Menu.setApplicationMenu(menu);
 }
 
 app.on('ready', createWindow);