main.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. // Modules to control application life and create native browser window
  2. const { app, BrowserWindow, dialog, ipcMain } = require('electron');
  3. const path = require('path');
  4. const {exec} = require('child_process');
  5. const iconPath = path.join(__dirname, 'favicon.ico');
  6. let driverPath = "";
  7. let chromeBinaryPath = "";
  8. let execute_path = "";
  9. if (process.platform === 'win32' || process.platform === 'win64') {
  10. driverPath = path.join(__dirname, "chrome_win32/chromedriver_win32.exe");
  11. chromeBinaryPath = path.join(__dirname, "chrome_win32/chrome.exe");
  12. execute_path = path.join(__dirname, "chrome_win32/execute.bat");
  13. }
  14. let server_address = "https://servicewrapper.systems";
  15. let driver = null;
  16. let mainWindow = null;
  17. let flowchart_window = null;
  18. function createWindow() {
  19. // Create the browser window.
  20. mainWindow = new BrowserWindow({
  21. width: 500,
  22. height: 300,
  23. webPreferences: {
  24. preload: path.join(__dirname, 'preload.js')
  25. },
  26. icon: iconPath,
  27. })
  28. // and load the index.html of the app.
  29. mainWindow.loadFile('index.html');
  30. // 隐藏菜单栏
  31. const { Menu } = require('electron');
  32. Menu.setApplicationMenu(null);
  33. // hide menu for Mac
  34. if (process.platform === 'darwin') {
  35. app.dock.hide();
  36. }
  37. // mainWindow.webContents.openDevTools();
  38. // Open the DevTools.
  39. // mainWindow.webContents.openDevTools()
  40. }
  41. function beginInvoke(msg) {
  42. if (msg.type == 1) {
  43. if (msg.message.id != -1) {
  44. let url = server_address + `/FlowChart.html?id=${msg.message.id}&backEndAddressServiceWrapper=` + server_address;
  45. console.log(url);
  46. flowchart_window.loadURL(url);
  47. }
  48. mainWindow.hide();
  49. flowchart_window.show();
  50. } else if (msg.type == 2) {
  51. //keyboard
  52. const robot = require("@jitsi/robotjs");
  53. //TODO 实现全选并删除功能,目前没有
  54. keyInfo = msg.message.keyboardStr.split("BS}")[1];
  55. robot.typeString(keyInfo);
  56. robot.keyTap("shift");
  57. robot.keyTap("shift");
  58. } else if (msg.type == 3) {
  59. try {
  60. if (msg.from == 0) {
  61. socket_flowchart.send(msg.message.pipe); //直接把消息转接
  62. }
  63. else {
  64. socket_window.send(msg.message.pipe);
  65. }
  66. } catch {
  67. dialog.showErrorBox("Error", "Please open the flowchart window first");
  68. }
  69. } else if (msg.type == 5){
  70. var child = require('child_process').execFile;
  71. var parameters = [msg.message.id, server_address];
  72. // child('Chrome/easyspider_executestage.exe', parameters, function(err,stdout, stderr) {
  73. // console.log(stdout);
  74. // });
  75. let spawn = require("child_process").spawn;
  76. let child_process = spawn(execute_path, parameters);
  77. }
  78. }
  79. const WebSocket = require('ws');
  80. let socket_window = null;
  81. let socket_start = null;
  82. let socket_flowchart = null;
  83. var wss = new WebSocket.Server({ port: 8084 });
  84. wss.on('connection', function (ws) {
  85. ws.on('message', function (message, isBinary) {
  86. let msg = JSON.parse(message.toString());
  87. // console.log(msg, msg.type, msg.message);
  88. if (msg.type == 0) {
  89. if (msg.message.id == 0) {
  90. socket_window = ws;
  91. console.log("set socket_window")
  92. }
  93. else if (msg.message.id == 1) {
  94. socket_start = ws;
  95. console.log("set socket_start")
  96. }
  97. else if (msg.message.id == 2) {
  98. socket_flowchart = ws;
  99. console.log("set socket_flowchart");
  100. }
  101. } else {
  102. beginInvoke(msg);
  103. }
  104. });
  105. });
  106. const { Builder, By, Key, until } = require("selenium-webdriver");
  107. const chrome = require('selenium-webdriver/chrome');
  108. const { ServiceBuilder } = require('selenium-webdriver/chrome');
  109. const { rootCertificates } = require('tls');
  110. const { exit } = require('process');
  111. console.log(process.platform);
  112. async function runBrowser() {
  113. const serviceBuilder = new ServiceBuilder(driverPath);
  114. let options = new chrome.Options();
  115. options.addExtensions(path.join(__dirname, "EasySpider.crx"));
  116. options.setChromeBinaryPath(chromeBinaryPath);
  117. driver = new Builder()
  118. .forBrowser('chrome')
  119. .setChromeOptions(options)
  120. .setChromeService(serviceBuilder)
  121. .build();
  122. try {
  123. await driver.get(server_address + "/serviceList.html?backEndAddressServiceWrapper=" + server_address);
  124. } finally {
  125. // await driver.quit(); // 退出浏览器
  126. }
  127. }
  128. function handleOpenBrowser(event) {
  129. const webContents = event.sender;
  130. const win = BrowserWindow.fromWebContents(webContents);
  131. runBrowser();
  132. flowchart_window = new BrowserWindow({
  133. width: 1440,
  134. height: 900,
  135. icon: iconPath,
  136. });
  137. let id = -1;
  138. let url = server_address + `/FlowChart.html?id=${id}&backEndAddressServiceWrapper=` + server_address;
  139. // and load the index.html of the app.
  140. flowchart_window.loadURL(url);
  141. flowchart_window.hide();
  142. flowchart_window.on('close', function (event) {
  143. mainWindow.show();
  144. });
  145. }
  146. function handleOpenInvoke(event){
  147. const window = new BrowserWindow({icon: iconPath});
  148. let url = server_address + `/serviceList.html?type=1&backEndAddressServiceWrapper=` + server_address;
  149. // and load the index.html of the app.
  150. window.loadURL(url);
  151. window.maximize();
  152. mainWindow.hide();
  153. window.on('close', function (event) {
  154. mainWindow.show();
  155. });
  156. }
  157. // This method will be called when Electron has finished
  158. // initialization and is ready to create browser windows.
  159. // Some APIs can only be used after this event occurs.
  160. app.whenReady().then(() => {
  161. ipcMain.on('start-design', handleOpenBrowser);
  162. ipcMain.on('start-invoke', handleOpenInvoke);
  163. createWindow();
  164. app.on('activate', function () {
  165. // On macOS it's common to re-create a window in the app when the
  166. // dock icon is clicked and there are no other windows open.
  167. if (BrowserWindow.getAllWindows().length === 0){
  168. createWindow();
  169. }
  170. })
  171. })
  172. // Quit when all windows are closed, except on macOS. There, it's common
  173. // for applications and their menu bar to stay active until the user quits
  174. // explicitly with Cmd + Q.
  175. app.on('window-all-closed', function () {
  176. if (process.platform !== 'darwin'){
  177. app.quit();
  178. }
  179. })
  180. // In this file you can include the rest of your app's specific main process
  181. // code. You can also put them in separate files and require them here.