main.js 5.5 KB

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