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

能读取到内存里的index.html了

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

+ 9 - 7
example/electron.js

@@ -36,15 +36,16 @@ 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);
 
+  // Load index.html into memory
+  const indexPath = path.join(process.resourcesPath, 'app.asar', 'index.html');
+  const indexRelativePath = path.relative(process.resourcesPath, indexPath);
+  memoryCache[indexRelativePath] = fs.readFileSync(indexPath);
+  console.log(`index: ${indexRelativePath}, content: ${memoryCache[indexRelativePath].slice(0, 100)}`);
+
   // Register a custom protocol to serve content from memory
   protocol.interceptBufferProtocol('file', (request, callback) => {
     const url = request.url.substr(7); // Remove 'file://' prefix
@@ -78,8 +79,9 @@ function createWindow() {
     },
   });
 
-  // Load the index.html from disk
-  win.loadFile(path.join(process.resourcesPath, 'app.asar', 'index.html'));
+  // 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));
 
   // 打开调试工具
   win.webContents.openDevTools();