main.js 4.4 KB

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