main.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 enterURL = 'http://cunchu.site/work/debug/index.html'
  10. let webConfig = {
  11. width: 376,
  12. height: 667,
  13. webPreferences: {
  14. webSecurity: false,
  15. nodeIntegration: true,
  16. nativeWindowOpen: false
  17. },
  18. autoHideMenuBar: true
  19. // 无边框
  20. // frame: false,
  21. // 全屏
  22. // fullscreen: true
  23. }
  24. // 判断是否有特殊配置文件
  25. console.log(__dirname + "\\config.json")
  26. if (fs.existsSync(__dirname + "\\config.json")) {
  27. webConfig = JSON.parse(fs.readFileSync(__dirname + '/config.json', 'utf-8'))
  28. enterURL = webConfig.enterURL
  29. }
  30. // if (config.preload) {
  31. // console.log(config.preload)
  32. // webConfig.webPreferences.preload = config.preload
  33. // }
  34. const mainWindow = new BrowserWindow(webConfig)
  35. // and load the index.html of the app.
  36. // mainWindow.loadFile('./resources/01.html')
  37. // 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 electron'
  38. // mainWindow.webContents.openDevTools()
  39. mainWindow.loadURL(enterURL)
  40. mainWindow.webContents.on("dom-ready", function() {
  41. mainWindow.webContents.executeJavaScript(`
  42. var owoApp = 3
  43. window.owoPC = true
  44. function loadScript(url, callback) {
  45. var script = document.createElement("script")
  46. script.type = "text/javascript";
  47. if (script.readyState) { //IE
  48. script.onreadystatechange = function () {
  49. if (script.readyState == "loaded" || script.readyState == "complete") {
  50. script.onreadystatechange = null;
  51. if (callback) callback();
  52. }
  53. };
  54. } else { //Others
  55. script.onload = function () {
  56. if (callback) callback();
  57. };
  58. }
  59. script.src = url;
  60. var head = document.head || document.getElementsByTagName('head')[0];
  61. head.appendChild(script);
  62. }
  63. function loadCSS (url) {
  64. var link = document.createElement("link");
  65. link.rel = "stylesheet";
  66. link.type = "text/css";
  67. link.href = url;
  68. document.getElementsByTagName("head")[0].appendChild(link);
  69. }
  70. // 拦截请求
  71. loadScript('//cunchu.site/work/assist/ajaxhook.min.js')
  72. let needLoad = localStorage.getItem('needLoad')
  73. needLoad = null
  74. function owoReload (data) {
  75. data.forEach(element => {
  76. if (element.type == 'app') {
  77. let scriptList = element.script
  78. if (typeof scriptList == 'string') {
  79. scriptList = JSON.parse(scriptList)
  80. }
  81. if (typeof element.style == 'string') {
  82. element.style = JSON.parse(element.style)
  83. }
  84. scriptList.forEach(scriptItem => {
  85. loadScript(scriptItem)
  86. });
  87. element.style.forEach(styleItem => {
  88. loadCSS(styleItem)
  89. });
  90. }
  91. });
  92. }
  93. if (needLoad) {
  94. owoReload(JSON.parse(needLoad))
  95. } else {
  96. fetch("https://going.run/assist?route=app", {
  97. method: 'POST',
  98. headers: {
  99. "Content-Type": "application/json"
  100. },
  101. body: JSON.stringify({
  102. "url": location.href,
  103. "edition": 1
  104. }),
  105. redirect: 'follow'
  106. }).then(response => response.json())
  107. .then(result => {
  108. if (result['err'] == 0) {
  109. owoReload(result['data'])
  110. localStorage.setItem('needLoad', JSON.stringify(result['data']))
  111. }
  112. })
  113. .catch(error => console.log('error', error))
  114. }
  115. `);
  116. });
  117. // Open the DevTools.
  118. }
  119. // This method will be called when Electron has finished
  120. // initialization and is ready to create browser windows.
  121. // Some APIs can only be used after this event occurs.
  122. app.whenReady().then(() => {
  123. createWindow()
  124. app.on('activate', function () {
  125. // On macOS it's common to re-create a window in the app when the
  126. // dock icon is clicked and there are no other windows open.
  127. if (BrowserWindow.getAllWindows().length === 0) createWindow()
  128. })
  129. })
  130. // Quit when all windows are closed, except on macOS. There, it's common
  131. // for applications and their menu bar to stay active until the user quits
  132. // explicitly with Cmd + Q.
  133. app.on('window-all-closed', function () {
  134. if (process.platform !== 'darwin') app.quit()
  135. })
  136. // In this file you can include the rest of your app's specific main process
  137. // code. You can also put them in separate files and require them here.