main.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Modules to control application life and create native browser window
  2. const {app, BrowserWindow} = require('electron')
  3. const path = require('path')
  4. const fs = require('fs')
  5. // 读取配置文件
  6. const config = JSON.parse(fs.readFileSync('./resources/config.json', 'utf-8'))
  7. function createWindow () {
  8. // Create the browser window.
  9. let webConfig = {
  10. width: config.width,
  11. height: config.height,
  12. webPreferences: {
  13. webSecurity: false,
  14. },
  15. // 无边框
  16. // frame: false,
  17. // 全屏
  18. // fullscreen: true
  19. }
  20. // if (config.preload) {
  21. // console.log(config.preload)
  22. // webConfig.webPreferences.preload = config.preload
  23. // }
  24. const mainWindow = new BrowserWindow(webConfig)
  25. // and load the index.html of the app.
  26. // mainWindow.loadFile('./resources/01.html')
  27. mainWindow.webContents.userAgent = 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_5_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1.1 Mobile/15E148 Safari/604.1'
  28. // mainWindow.webContents.openDevTools()
  29. mainWindow.loadURL(config.url)
  30. mainWindow.webContents.on("dom-ready", function() {
  31. mainWindow.webContents.executeJavaScript(`
  32. function loadScript(url, callback) {
  33. var script = document.createElement("script")
  34. script.type = "text/javascript";
  35. if (script.readyState) { //IE
  36. script.onreadystatechange = function () {
  37. if (script.readyState == "loaded" || script.readyState == "complete") {
  38. script.onreadystatechange = null;
  39. if (callback) callback();
  40. }
  41. };
  42. } else { //Others
  43. script.onload = function () {
  44. if (callback) callback();
  45. };
  46. }
  47. script.src = url;
  48. var head = document.head || document.getElementsByTagName('head')[0];
  49. head.appendChild(script);
  50. }
  51. loadScript('${config.preload}')
  52. `);
  53. });
  54. // Open the DevTools.
  55. }
  56. // This method will be called when Electron has finished
  57. // initialization and is ready to create browser windows.
  58. // Some APIs can only be used after this event occurs.
  59. app.whenReady().then(() => {
  60. createWindow()
  61. app.on('activate', function () {
  62. // On macOS it's common to re-create a window in the app when the
  63. // dock icon is clicked and there are no other windows open.
  64. if (BrowserWindow.getAllWindows().length === 0) createWindow()
  65. })
  66. })
  67. app.on('certificate-error', (event, webContents, url, error, certificate, callback) => {
  68. //允许私有证书
  69. event.preventDefault()
  70. callback(true)
  71. });
  72. // Quit when all windows are closed, except on macOS. There, it's common
  73. // for applications and their menu bar to stay active until the user quits
  74. // explicitly with Cmd + Q.
  75. app.on('window-all-closed', function () {
  76. if (process.platform !== 'darwin') app.quit()
  77. })
  78. // In this file you can include the rest of your app's specific main process
  79. // code. You can also put them in separate files and require them here.