Sfoglia il codice sorgente

拦截成功的版本。但是无法触发加载。

skyffire 1 anno fa
parent
commit
c7d32f3614
1 ha cambiato i file con 5 aggiunte e 5 eliminazioni
  1. 5 5
      example/electron.js

+ 5 - 5
example/electron.js

@@ -51,9 +51,7 @@ 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));
+  // Load the index.html from disk
   win.loadFile('index.html');
 
   // 打开调试工具
@@ -80,13 +78,15 @@ function createWindow() {
 
   // 设置应用程序的菜单
   Menu.setApplicationMenu(menu);
+  console.log(Object.keys(memoryCache))
 }
 
 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, '/');
+    const url = new URL(details.url);
+    const filePath = path.normalize(decodeURIComponent(url.pathname));
+    const relativePath = filePath.replace(path.normalize(process.resourcesPath), '').replace(/\\/g, '/').replace('//', '');
     console.log(`Intercepting request for: ${relativePath}`);
 
     if (memoryCache[relativePath]) {