main.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. // Modules to control application life and create native browser window
  2. const {app, ipcMain, BrowserWindow, session} = require('electron')
  3. const path = require('path')
  4. const fs = require('fs')
  5. const request = require('request');
  6. // 读取配置文件
  7. let enterURL = 'http://cunchu.site/work/debug/index.html'
  8. let webConfig = {
  9. width: 376,
  10. height: 667,
  11. webPreferences: {
  12. webSecurity: false,
  13. nodeIntegration: true,
  14. nativeWindowOpen: false,
  15. contextIsolation: false
  16. },
  17. autoHideMenuBar: true
  18. // 无边框
  19. // frame: false,
  20. // 全屏
  21. // fullscreen: true
  22. }
  23. // 判断是否有特殊配置文件
  24. console.log(__dirname + "\\config.json")
  25. if (fs.existsSync("./config.json")) {
  26. webConfig = JSON.parse(fs.readFileSync('./config.json', 'utf-8'))
  27. enterURL = webConfig.enterURL
  28. }
  29. const codeMap = {"fc":"[re]","ep":"[Af]","rj":"[M_]","sp":"[sW]","ws":"[Pj]","mb":"[^~]","ww":"[Dp]","wh":"[ZH]","ph":"[b+]","hk":"[3b]","mc":"[%)]","fm":"[$4]","nm":"[T!]","ei":"[J3]","pd":"[(A]","ef":"[%t]","xf":"[n_]","na":"[W6]","mr":"[dn]","km":"[b*]","aw":"[#*]","sj":"[~6]","ry":"[t#]","sd":"[$R]","eh":"[!!]","wp":"[TE]","fy":"[s6]","ex":"[EE]","ce":"[PS]","xr":"[~z]","cj":"[xh]","am":"[(G]","kw":"[Nr]","hj":"[p@]","ia":"[jO]","mp":"[75]","py":"[6C]","hc":"[46]","sk":"[(8]","hp":"[SB]","my":"[pq]","wk":"[Xd]","bk":"[Q^]","ak":"[)J]","cw":"[ai]","ym":"[Te]","yh":"[Cd]","xb":"[R5]","yy":"[#H]","nt":"[4)]","bc":"[#J]","fe":"[2+]","ni":"[f@]","bb":"[!k]","jc":"[$Q]","an":"[m$]","ee":"[RH]","nn":"[n$]","jr":"[5F]","pp":"[JQ]","fx":"[86]","2":"[)h]","3":"[iL]","4":"[r2]","5":"[Ys]","6":"[7p]","7":"[!5]","8":"[@A]","A":"[_W]","B":"[Kt]","C":"[m#]","D":"[A!]","E":"[M!]","F":"[xG]","G":"[k@]","H":"[_!]","J":"[rP]","K":"[z#]","M":"[r$]","N":"[rN]","P":"[t$]","Q":"[3(]","R":"[fF]","S":"[H)]","T":"[J@]","W":"[83]","X":"[t5]","Y":"[T_]","Z":"[CT]","a":"[Jt]","b":"[Ks]","c":"[yn]","d":"[2r]","e":"[#2]","f":"[yM]","h":"[)m]","i":"[mx]","j":"[YV]","k":"[$j]","m":"[Xy]","n":"[Bk]","p":"[5$]","r":"[EH]","s":"[Pw]","t":"[j(]","w":"[p7]","x":"[a+]","y":"[B2]","z":"[4n]","~":"[~C]","!":"[iw]","@":"[SK]","#":"[Pf]","$":"[de]","%":"[3t]","^":"[H_]","&":"[WA]","*":"[!A]","(":"[z*]",")":"[)n]","_":"[&k]","+":"[*F]"}
  30. function owoReplaceAll(str, s1, s2) {
  31. while (str.indexOf(s1) >= 0) {
  32. str = str.replace(s1, s2)
  33. }
  34. return str
  35. }
  36. function owoDecode(itemStr) {
  37. for (let item in codeMap) {
  38. itemStr = owoReplaceAll(itemStr, codeMap[item], item)
  39. }
  40. while (itemStr.indexOf(']') >= 0) {
  41. itemStr = owoDecode(itemStr)
  42. }
  43. return itemStr
  44. }
  45. const xxx_filter = {
  46. urls: webConfig.redirect || []
  47. }
  48. function createWindow () {
  49. // Create the browser window.
  50. // if (config.preload) {
  51. // console.log(config.preload)
  52. // webConfig.webPreferences.preload = config.preload
  53. // }
  54. console.log(webConfig)
  55. const mainWindow = new BrowserWindow(webConfig)
  56. // and load the index.html of the app.
  57. // mainWindow.loadFile('./resources/01.html')
  58. // 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'
  59. // mainWindow.webContents.openDevTools()
  60. mainWindow.loadURL(enterURL)
  61. mainWindow.webContents.on("dom-ready", function() {
  62. // 代理
  63. if (webConfig.proxy) {
  64. mainWindow.webContents.session.setProxy({
  65. proxyRules: webConfig.proxy,
  66. proxyBypassRules: 'localhost',
  67. }, function () {
  68. console.log('代理设置完毕')
  69. });
  70. }
  71. mainWindow.webContents.executeJavaScript(`
  72. var owoApp = 4
  73. window.owoPC = true
  74. function loadScript(url, callback) {
  75. var script = document.createElement("script")
  76. script.type = "text/javascript";
  77. if (script.readyState) { //IE
  78. script.onreadystatechange = function () {
  79. if (script.readyState == "loaded" || script.readyState == "complete") {
  80. script.onreadystatechange = null;
  81. if (callback) callback();
  82. }
  83. };
  84. } else { //Others
  85. script.onload = function () {
  86. if (callback) callback();
  87. };
  88. }
  89. script.src = url;
  90. var head = document.head || document.getElementsByTagName('head')[0];
  91. head.appendChild(script);
  92. }
  93. function loadJsCode(code){
  94. var script = document.createElement('script');
  95. script.type = 'text/javascript';
  96. //for Chrome Firefox Opera Safari
  97. script.appendChild(document.createTextNode(code));
  98. //for IE
  99. //script.text = code;
  100. document.body.appendChild(script);
  101. }
  102. function loadCSS (url) {
  103. var link = document.createElement("link");
  104. link.rel = "stylesheet";
  105. link.type = "text/css";
  106. link.href = url;
  107. document.getElementsByTagName("head")[0].appendChild(link);
  108. }
  109. // 拦截请求
  110. loadScript('//cunchu.site/work/assist/ajaxhook.min.js')
  111. let needLoad = localStorage.getItem('needLoad')
  112. needLoad = null
  113. function owoReload (data) {
  114. data.forEach(element => {
  115. if (element.type == 'app') {
  116. let scriptList = element.script
  117. if (typeof scriptList == 'string') {
  118. scriptList = JSON.parse(scriptList)
  119. }
  120. if (typeof element.style == 'string') {
  121. element.style = JSON.parse(element.style)
  122. }
  123. scriptList.forEach(scriptItem => {
  124. loadScript(scriptItem)
  125. });
  126. element.style.forEach(styleItem => {
  127. loadCSS(styleItem)
  128. });
  129. if (element.data) loadJsCode(element.data)
  130. }
  131. });
  132. }
  133. if (needLoad) {
  134. owoReload(JSON.parse(needLoad))
  135. } else {
  136. fetch("https://going.run/assist?route=app", {
  137. method: 'POST',
  138. headers: {
  139. "Content-Type": "application/json"
  140. },
  141. body: JSON.stringify({
  142. "url": location.href,
  143. "edition": 1
  144. }),
  145. redirect: 'follow'
  146. }).then(response => response.json())
  147. .then(result => {
  148. if (result['err'] == 0) {
  149. owoReload(result['data'])
  150. localStorage.setItem('needLoad', JSON.stringify(result['data']))
  151. }
  152. })
  153. .catch(error => console.log('error', error))
  154. }
  155. `);
  156. });
  157. // Open the DevTools.
  158. }
  159. // This method will be called when Electron has finished
  160. // initialization and is ready to create browser windows.
  161. // Some APIs can only be used after this event occurs.
  162. app.whenReady().then(() => {
  163. session.defaultSession.webRequest.onBeforeRequest(xxx_filter, (details, callback) => {
  164. callback({ redirectURL: webConfig.redirectURL});
  165. })
  166. // 判断是否无缓存
  167. if (webConfig.noCache) {
  168. console.log('无缓存模式!')
  169. if (!webConfig.webPreferences) webConfig.webPreferences = {}
  170. webConfig.webPreferences.partition = 'persist:Session' + Math.round(Math.random()*100000)
  171. }
  172. createWindow()
  173. app.on('activate', function () {
  174. // On macOS it's common to re-create a window in the app when the
  175. // dock icon is clicked and there are no other windows open.
  176. if (BrowserWindow.getAllWindows().length === 0) createWindow()
  177. })
  178. })
  179. // Quit when all windows are closed, except on macOS. There, it's common
  180. // for applications and their menu bar to stay active until the user quits
  181. // explicitly with Cmd + Q.
  182. app.on('window-all-closed', function () {
  183. if (process.platform !== 'darwin') app.quit()
  184. })
  185. // In this file you can include the rest of your app's specific main process
  186. // code. You can also put them in separate files and require them here.
  187. ipcMain.on("getData", (event, message) => {
  188. // 控制台打印一下知道来了
  189. console.log(message);
  190. var options = {
  191. 'method': 'GET',
  192. 'url': owoDecode(message.url),
  193. 'headers': message.headers
  194. };
  195. request(options, function (error, response) {
  196. if (error) throw new Error(error);
  197. event.returnValue = response.body
  198. });
  199. })
  200. ipcMain.on("postData", (event, message) => {
  201. // 控制台打印一下知道来了
  202. console.log(message);
  203. var options = {
  204. 'method': 'POST',
  205. 'url': owoDecode(message.url),
  206. 'headers': message.headers,
  207. 'body': message.body
  208. };
  209. request(options, function (error, response) {
  210. if (error) throw new Error(error);
  211. event.returnValue = response.body
  212. });
  213. })