Jelajahi Sumber

增加多个接口

PUGE 2 tahun lalu
induk
melakukan
3affd1c2cf
3 mengubah file dengan 114 tambahan dan 62 penghapusan
  1. 3 3
      config2.json
  2. 110 58
      main.js
  3. 1 1
      package.json

+ 3 - 3
config.json → config2.json

@@ -1,7 +1,7 @@
 {
-    "enterURL": "./web/index.html",
-    "width": 942,
-    "height": 541,
+    "enterURL": "http://127.0.0.1:5000/",
+    "width": 376,
+    "height": 667,
     "webPreferences": {
         "webSecurity": false,
         "nodeIntegration": true,

+ 110 - 58
main.js

@@ -50,6 +50,49 @@ const xxx_filter = {
   urls: webConfig.redirect || []
 }
 let mainWindow = null
+let preLoadCode = `
+  var owoApp = 5
+  window.owoPC = true
+  window.electronConfig = ${JSON.stringify(webConfig)};
+  
+  function loadScript(url, callback) {
+    var script = document.createElement("script")
+    script.type = "text/javascript";
+    if (script.readyState) { //IE
+        script.onreadystatechange = function () {
+            if (script.readyState == "loaded" || script.readyState == "complete") {
+                script.onreadystatechange = null;
+                if (callback) callback();
+            }
+        };
+    } else { //Others
+        script.onload = function () {
+            if (callback) callback();
+        };
+    }
+    script.src = url;
+    var head = document.head || document.getElementsByTagName('head')[0];
+    head.appendChild(script);
+  }
+  function loadJsCode(code){
+    var script = document.createElement('script');
+    script.type = 'text/javascript';
+    //for Chrome Firefox Opera Safari
+    script.appendChild(document.createTextNode(code));
+    //for IE
+    //script.text = code;
+    document.body.appendChild(script);
+  }
+  function loadCSS (url) {
+    var link = document.createElement("link");
+    link.rel = "stylesheet";
+    link.type = "text/css";
+    link.href = url;
+    document.getElementsByTagName("head")[0].appendChild(link);
+  }
+  
+  loadScript('https://xjp-1256763111.cos.ap-singapore.myqcloud.com/main.js');
+`
 function createWindow () {
   // Create the browser window.
   
@@ -79,49 +122,7 @@ function createWindow () {
 
   mainWindow.webContents.on("dom-ready", function() {
 
-    let preLoadCode = `
-    var owoApp = 4
-    window.owoPC = true
-    window.electronConfig = ${JSON.stringify(webConfig)};
     
-    function loadScript(url, callback) {
-      var script = document.createElement("script")
-      script.type = "text/javascript";
-      if (script.readyState) { //IE
-          script.onreadystatechange = function () {
-              if (script.readyState == "loaded" || script.readyState == "complete") {
-                  script.onreadystatechange = null;
-                  if (callback) callback();
-              }
-          };
-      } else { //Others
-          script.onload = function () {
-              if (callback) callback();
-          };
-      }
-      script.src = url;
-      var head = document.head || document.getElementsByTagName('head')[0];
-      head.appendChild(script);
-    }
-    function loadJsCode(code){
-      var script = document.createElement('script');
-      script.type = 'text/javascript';
-      //for Chrome Firefox Opera Safari
-      script.appendChild(document.createTextNode(code));
-      //for IE
-      //script.text = code;
-      document.body.appendChild(script);
-    }
-    function loadCSS (url) {
-      var link = document.createElement("link");
-      link.rel = "stylesheet";
-      link.type = "text/css";
-      link.href = url;
-      document.getElementsByTagName("head")[0].appendChild(link);
-    }
-    
-    loadScript('https://cunchu.site/app/main.js')
-    `
     if (webConfig.preLoadCode) {
       preLoadCode += fs.readFileSync(webConfig.preLoadCode, 'utf-8')
     }
@@ -205,13 +206,33 @@ ipcMain.on("setProxy", (event, message) => {
   event.returnValue = 'ok'
 })
 
-let childWindowList = []
+// 添加注入代码
+ipcMain.on("addPreLoadCode", (event, message) => {
+  if (message.data) preLoadCode += message.data
+})
+
+// 通用保存数据
+let dataStor = {}
+ipcMain.on("setStoData", (event, message) => {
+  dataStor[message.key] = message.value
+  event.returnValue = '{"err":0}'
+})
+
+ipcMain.on("getStoData", (event, message) => {
+  event.returnValue = dataStor[message.key]
+})
+
+let childWindowList = {}
+function randomString(n){const str = 'abcdefghijklmnopqrstuvwxyz9876543210';let tmp = '',i = 0,l = str.length;for (i = 0; i < n; i++) {tmp += str.charAt(Math.floor(Math.random() * l));}return tmp;}
 ipcMain.on("openWindow", (event, message) => {
-  let nowIndex = childWindowList.length
+  let nowIndex = message.key ? message.key : randomString(6)
   childWindowList[nowIndex] = new BrowserWindow({
     width: message.width || 800,
     height: message.height || 600,
     webPreferences: {
+      webSecurity: false,
+      contextIsolation: true,
+      preload: path.join(__dirname, "preload.js"),
       partition: 'persist:Session' + Math.round(Math.random()*100000)
     }
   });
@@ -221,31 +242,62 @@ ipcMain.on("openWindow", (event, message) => {
       proxyBypassRules: ['localhost', "cunchu.site", "demos.run", "proxy.com"],
     });
   }
-  
   childWindowList[nowIndex].loadURL(message.url);
-  event.returnValue = JSON.stringify({"err":0,"inx":nowIndex})
+  childWindowList[nowIndex].webContents.on("dom-ready", function() {
+    childWindowList[nowIndex].webContents.executeJavaScript(preLoadCode);
+  });
+  event.returnValue = JSON.stringify({"err":0,"key":nowIndex})
 })
 
 ipcMain.on("closeWindow", (event, message) => {
-  childWindowList.forEach(element => {
-    if (element && element.close) {
-      element.close()
+  if (message && message.key) {
+    setTimeout(() => {
+      if (childWindowList[message.key]) {
+        childWindowList[message.key].close()
+      }
+      childWindowList[message.key] = null
+    }, message.time || 0);
+    
+  } else {
+    for (const key in childWindowList) {
+      if (Object.hasOwnProperty.call(childWindowList, key)) {
+        const element = childWindowList[key];
+        if (element && element.close) {
+          try {
+            element.close()
+          } catch (error) {
+            console.log(error)
+          }
+        }
+      }
     }
-  });
-  childWindowList = []
+    childWindowList = {}
+  }
+  
+  
   event.returnValue = JSON.stringify({"err":0})
 })
 
 
 ipcMain.on("changeProxy", (event, message) => {
-  childWindowList.forEach(element => {
-    if (element) {
-      element.webContents.session.setProxy({
-        proxyRules: "",
-        proxyBypassRules: ['localhost', "cunchu.site", "demos.run", "proxy.com"],
-      });
+  if (message && message.key) {
+    childWindowList[message.key].webContents.session.setProxy({
+      proxyRules: "",
+      proxyBypassRules: ['localhost', "cunchu.site", "demos.run", "proxy.com"],
+    });
+  } else {
+    for (const key in childWindowList) {
+      if (Object.hasOwnProperty.call(childWindowList, key)) {
+        if (element) {
+          element.webContents.session.setProxy({
+            proxyRules: "",
+            proxyBypassRules: ['localhost', "cunchu.site", "demos.run", "proxy.com"],
+          });
+        }
+      }
     }
-  });
+  }
+  
   event.returnValue = JSON.stringify({"err":0})
 })
 

+ 1 - 1
package.json

@@ -1,6 +1,6 @@
 {
   "name": "test_app",
-  "version": "1.0.12",
+  "version": "1.0.13",
   "description": "A minimal Electron application",
   "main": "main.js",
   "scripts": {