main.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. // Modules to control application life and create native browser window
  2. const {app, BrowserWindow, dialog, ipcMain, screen} = require('electron');
  3. app.commandLine.appendSwitch("--disable-http-cache");
  4. const {Builder, By, Key, until} = require("selenium-webdriver");
  5. const chrome = require('selenium-webdriver/chrome');
  6. const {ServiceBuilder} = require('selenium-webdriver/chrome');
  7. const {rootCertificates} = require('tls');
  8. const {exit} = require('process');
  9. const path = require('path');
  10. const fs = require('fs');
  11. const {exec} = require('child_process');
  12. const iconPath = path.join(__dirname, 'favicon.ico');
  13. const task_server = require(path.join(__dirname, 'server.js'));
  14. let config = fs.readFileSync(path.join(task_server.getDir(), `config.json`), 'utf8');
  15. config = JSON.parse(config);
  16. task_server.start(config.webserver_port); //start local server
  17. let server_address = `${config.webserver_address}:${config.webserver_port}`;
  18. const websocket_port = 8084; //目前只支持8084端口,写死,因为扩展里面写死了
  19. console.log("server_address: " + server_address);
  20. let driverPath = "";
  21. let chromeBinaryPath = "";
  22. let execute_path = "";
  23. console.log(process.arch);
  24. if (process.platform === 'win32' && process.arch === 'ia32') {
  25. driverPath = path.join(__dirname, "chrome_win32/chromedriver_win32.exe");
  26. chromeBinaryPath = path.join(__dirname, "chrome_win32/chrome.exe");
  27. execute_path = path.join(__dirname, "chrome_win32/execute.bat");
  28. } else if (process.platform === 'win32' && process.arch === 'x64') {
  29. driverPath = path.join(__dirname, "chrome_win64/chromedriver_win64.exe");
  30. chromeBinaryPath = path.join(__dirname, "chrome_win64/chrome.exe");
  31. execute_path = path.join(__dirname, "chrome_win64/execute.bat");
  32. } else if (process.platform === 'darwin') {
  33. driverPath = path.join(__dirname, "chromedriver_mac64");
  34. chromeBinaryPath = path.join(__dirname, "chrome_mac64.app/Contents/MacOS/Google Chrome");
  35. execute_path = path.join(__dirname, "");
  36. } else if (process.platform === 'linux') {
  37. driverPath = path.join(__dirname, "chrome_linux64/chromedriver_linux64");
  38. chromeBinaryPath = path.join(__dirname, "chrome_linux64/chrome");
  39. execute_path = path.join(__dirname, "chrome_linux64/execute.sh");
  40. }
  41. console.log(driverPath, chromeBinaryPath, execute_path);
  42. let language = "en";
  43. let driver = null;
  44. let mainWindow = null;
  45. let flowchart_window = null;
  46. let current_handle = null;
  47. let old_handles = [];
  48. let handle_pairs = {};
  49. // var ffi = require('ffi-napi');
  50. // var libm = ffi.Library('libm', {
  51. // 'ceil': [ 'double', [ 'double' ] ]
  52. // });
  53. // libm.ceil(1.5); // 2
  54. // const {user32FindWindowEx,
  55. // winspoolGetDefaultPrinter,} = require('win32-api/fun');
  56. // async function testt(){
  57. // // 获取当前电脑当前用户默认打印机名
  58. // const printerName = await winspoolGetDefaultPrinter()
  59. // console.log(printerName);
  60. // }
  61. // testt();
  62. function createWindow() {
  63. // Create the browser window.
  64. mainWindow = new BrowserWindow({
  65. width: 520,
  66. height: 750,
  67. webPreferences: {
  68. preload: path.join(__dirname, 'src/js/preload.js')
  69. },
  70. icon: iconPath,
  71. // frame: false, //取消window自带的关闭最小化等
  72. resizable: false //禁止改变主窗口尺寸
  73. })
  74. // and load the index.html of the app.
  75. // mainWindow.loadFile('src/index.html');
  76. mainWindow.loadURL(server_address + '/index.html?user_data_folder=' + config.user_data_folder);
  77. // 隐藏菜单栏
  78. const {Menu} = require('electron');
  79. Menu.setApplicationMenu(null);
  80. mainWindow.on('close', function (e) {
  81. if (process.platform !== 'darwin') {
  82. app.quit();
  83. }
  84. });
  85. // mainWindow.webContents.openDevTools();
  86. // Open the DevTools.
  87. // mainWindow.webContents.openDevTools()
  88. }
  89. async function beginInvoke(msg, ws) {
  90. if (msg.type == 1) {
  91. if (msg.message.id != -1) {
  92. let url = "";
  93. if (language == "zh") {
  94. url = server_address + `/taskGrid/FlowChart_CN.html?id=${msg.message.id}&wsport=${websocket_port}&backEndAddressServiceWrapper=` + server_address;
  95. } else if (language == "en") {
  96. url = server_address + `/taskGrid/FlowChart.html?id=${msg.message.id}&wsport=${websocket_port}&backEndAddressServiceWrapper=` + server_address;
  97. }
  98. console.log(url);
  99. flowchart_window.loadURL(url);
  100. }
  101. mainWindow.hide();
  102. // Prints the currently focused window bounds.
  103. // This method has to be called on macOS before changing the window's bounds, otherwise it will throw an error.
  104. // It will prompt an accessibility permission request dialog, if needed.
  105. if(process.platform != "linux" && process.platform != "darwin"){
  106. const {windowManager} = require("node-window-manager");
  107. const window = windowManager.getActiveWindow();
  108. console.log(window);
  109. windowManager.requestAccessibility();
  110. // Sets the active window's bounds.
  111. let size = screen.getPrimaryDisplay().workAreaSize
  112. let width = parseInt(size.width)
  113. let height = parseInt(size.height * 0.6)
  114. window.setBounds({x: 0, y: size.height * 0.4, height: height, width: width});
  115. }
  116. flowchart_window.show();
  117. // flowchart_window.openDevTools();
  118. } else if (msg.type == 2) {
  119. //keyboard
  120. // const robot = require("@jitsi/robotjs");
  121. let keyInfo = msg.message.keyboardStr;
  122. let handles = await driver.getAllWindowHandles();
  123. console.log("handles", handles);
  124. let exit = false;
  125. let content_handle = handle_pairs[msg.message.id];
  126. console.log(msg.message.id, content_handle);
  127. let order = [...handles.filter(handle => handle != current_handle && handle != content_handle), current_handle, content_handle]; //搜索顺序
  128. let len = order.length;
  129. while (true) {
  130. // console.log("handles");
  131. try{
  132. let h = order[len - 1];
  133. console.log("current_handle", current_handle);
  134. if(h != null && handles.includes(h)){
  135. await driver.switchTo().window(h);
  136. current_handle = h;
  137. console.log("switch to handle: ", h);
  138. }
  139. // await driver.executeScript("window.stop();");
  140. // console.log("executeScript");
  141. let element = await driver.findElement(By.xpath(msg.message.xpath));
  142. console.log("Find Element at handle: ", current_handle);
  143. await element.sendKeys(Key.HOME, Key.chord(Key.SHIFT, Key.END), keyInfo);
  144. console.log("send key");
  145. break;
  146. } catch (error) {
  147. console.log("len", len);
  148. len = len - 1;
  149. if (len == 0) {
  150. break;
  151. }
  152. }
  153. // .then(function (element) {
  154. // console.log("element", element, handles);
  155. // element.sendKeys(Key.HOME, Key.chord(Key.SHIFT, Key.END), keyInfo);
  156. // exit = true;
  157. // }, function (error) {
  158. // console.log("error", error);
  159. // len = len - 1;
  160. // if (len == 0) {
  161. // exit = true;
  162. // }
  163. // }
  164. // );
  165. }
  166. // let handles = driver.getAllWindowHandles();
  167. // driver.switchTo().window(handles[handles.length - 1]);
  168. // driver.findElement(By.xpath(msg.message.xpath)).sendKeys(Key.HOME, Key.chord(Key.SHIFT, Key.END), keyInfo);
  169. // robot.keyTap("a", "control");
  170. // robot.keyTap("backspace");
  171. // robot.typeString(keyInfo);
  172. // robot.keyTap("shift");
  173. // robot.keyTap("shift");
  174. } else if (msg.type == 3) {
  175. try {
  176. if (msg.from == 0) {
  177. socket_flowchart.send(msg.message.pipe); //直接把消息转接
  178. let message = JSON.parse(msg.message.pipe);
  179. let type = message.type;
  180. console.log("FROM Browser: ", message);
  181. // if(type.indexOf("Click")>=0){
  182. // await new Promise(resolve => setTimeout(resolve, 2000)); //等两秒
  183. //
  184. // }
  185. } else {
  186. socket_window.send(msg.message.pipe);
  187. console.log("FROM Flowchart: ", JSON.parse(msg.message.pipe));
  188. }
  189. } catch (e) {
  190. console.log(e);
  191. }
  192. } else if (msg.type == 5) {
  193. let child = require('child_process').execFile;
  194. // 参数顺序: 1. task id 2. server address 3. saved_file_name 4. "remote" or "local" 5. user_data_folder
  195. // var parameters = [msg.message.id, server_address];
  196. let parameters = [];
  197. console.log(msg.message)
  198. if (msg.message.user_data_folder == null || msg.message.user_data_folder == undefined || msg.message.user_data_folder == "") {
  199. parameters = ["--id", "[" + msg.message.id + "]", "--server_address", server_address, "--user_data", 0];
  200. } else {
  201. let user_data_folder_path = path.join(task_server.getDir(), msg.message.user_data_folder);
  202. parameters = ["--id", "[" + msg.message.id + "]", "--server_address", server_address, "--user_data", 1];
  203. config.user_data_folder = msg.message.user_data_folder;
  204. config.absolute_user_data_folder = user_data_folder_path;
  205. fs.writeFileSync(path.join(task_server.getDir(), "config.json"), JSON.stringify(config));
  206. }
  207. // child('Chrome/easyspider_executestage.exe', parameters, function(err,stdout, stderr) {
  208. // console.log(stdout);
  209. // });
  210. let spawn = require("child_process").spawn;
  211. if (process.platform != "darwin" && msg.message.execute_type == 1) {
  212. let child_process = spawn(execute_path, parameters);
  213. child_process.stdout.on('data', function (data) {
  214. console.log(data.toString());
  215. });
  216. } else {
  217. ws.send(JSON.stringify({"config_folder": task_server.getDir() + "/", "easyspider_location": task_server.getEasySpiderLocation()}));
  218. }
  219. } else if (msg.type == 6) {
  220. try{
  221. flowchart_window.openDevTools();
  222. } catch {
  223. }
  224. }
  225. }
  226. const WebSocket = require('ws');
  227. let socket_window = null;
  228. let socket_start = null;
  229. let socket_flowchart = null;
  230. let wss = new WebSocket.Server({port: websocket_port});
  231. wss.on('connection', function (ws) {
  232. ws.on('message', async function (message, isBinary) {
  233. let msg = JSON.parse(message.toString());
  234. console.log("\n\nGET A MESSAGE: ", msg);
  235. // console.log(msg, msg.type, msg.message);
  236. if (msg.type == 0) {
  237. if (msg.message.id == 0) {
  238. socket_window = ws;
  239. console.log("set socket_window")
  240. } else if (msg.message.id == 1) {
  241. socket_start = ws;
  242. console.log("set socket_start")
  243. } else if (msg.message.id == 2) {
  244. socket_flowchart = ws;
  245. console.log("set socket_flowchart");
  246. } else { //其他的ID是用来标识不同的浏览器标签页的
  247. await new Promise(resolve => setTimeout(resolve, 2300));
  248. let handles = await driver.getAllWindowHandles();
  249. if(arrayDifference(handles, old_handles).length > 0){
  250. old_handles = handles;
  251. current_handle = handles[handles.length - 1];
  252. console.log("New tab opened, change current_handle to: ", current_handle);
  253. }
  254. handle_pairs[msg.message.id] = current_handle;
  255. console.log("Set handle_pair for id: ", msg.message.id, " to ", current_handle, ", title is: ", msg.message.title);
  256. socket_flowchart.send(JSON.stringify({"type": "title", "data": {"title":msg.message.title}}));
  257. // console.log("handle_pairs: ", handle_pairs);
  258. }
  259. } else if (msg.type == 10) {
  260. let leave_handle = handle_pairs[msg.message.id];
  261. if (leave_handle!=null && leave_handle!=undefined && leave_handle!="")
  262. {
  263. await driver.switchTo().window(leave_handle);
  264. console.log("Switch to handle: ", leave_handle);
  265. current_handle = leave_handle;
  266. }
  267. }
  268. else {
  269. await beginInvoke(msg, ws);
  270. }
  271. });
  272. });
  273. console.log(process.platform);
  274. async function runBrowser(lang = "en", user_data_folder = '') {
  275. const serviceBuilder = new ServiceBuilder(driverPath);
  276. let options = new chrome.Options();
  277. options.addArguments('--disable-blink-features=AutomationControlled');
  278. language = lang;
  279. if (lang == "en") {
  280. options.addExtensions(path.join(__dirname, "EasySpider_en.crx"));
  281. } else if (lang == "zh") {
  282. options.addExtensions(path.join(__dirname, "EasySpider_zh.crx"));
  283. }
  284. options.addExtensions(path.join(__dirname, "XPathHelper.crx"));
  285. options.setChromeBinaryPath(chromeBinaryPath);
  286. if (user_data_folder != "") {
  287. let dir = path.join(task_server.getDir(), user_data_folder);
  288. console.log(dir);
  289. options.addArguments("--user-data-dir=" + dir);
  290. config.user_data_folder = user_data_folder;
  291. fs.writeFileSync(path.join(task_server.getDir(), "config.json"), JSON.stringify(config));
  292. }
  293. driver = new Builder()
  294. .forBrowser('chrome')
  295. .setChromeOptions(options)
  296. .setChromeService(serviceBuilder)
  297. .build();
  298. await driver.manage().setTimeouts({implicit: 10000, pageLoad: 10000, script: 10000});
  299. await driver.executeScript("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})");
  300. const cdpConnection = await driver.createCDPConnection("page");
  301. let stealth_path = path.join(__dirname, "stealth.min.js");
  302. let stealth = fs.readFileSync(stealth_path, 'utf8');
  303. await cdpConnection.execute('Page.addScriptToEvaluateOnNewDocument', {
  304. source: stealth,
  305. });
  306. try {
  307. await driver.get(server_address + "/taskGrid/taskList.html?wsport=" + websocket_port + "&backEndAddressServiceWrapper=" + server_address + "&lang=" + lang);
  308. old_handles = await driver.getAllWindowHandles();
  309. current_handle = old_handles[old_handles.length - 1];
  310. } finally {
  311. // await driver.quit(); // 退出浏览器
  312. }
  313. }
  314. function handleOpenBrowser(event, lang = "en", user_data_folder = "") {
  315. const webContents = event.sender;
  316. const win = BrowserWindow.fromWebContents(webContents);
  317. runBrowser(lang, user_data_folder);
  318. let size = screen.getPrimaryDisplay().workAreaSize;
  319. let width = parseInt(size.width);
  320. let height = parseInt(size.height * 0.6);
  321. flowchart_window = new BrowserWindow({
  322. x: 0,
  323. y: 0,
  324. width: width,
  325. height: height,
  326. icon: iconPath,
  327. });
  328. let url = "";
  329. let id = -1;
  330. if (lang == "en") {
  331. url = server_address + `/taskGrid/FlowChart.html?id=${id}&wsport=${websocket_port}&backEndAddressServiceWrapper=` + server_address;
  332. } else if (lang == "zh") {
  333. url = server_address + `/taskGrid/FlowChart_CN.html?id=${id}&wsport=${websocket_port}&backEndAddressServiceWrapper=` + server_address;
  334. }
  335. // and load the index.html of the app.
  336. flowchart_window.loadURL(url);
  337. if(process.platform != "darwin"){
  338. flowchart_window.hide();
  339. }
  340. flowchart_window.on('close', function (event) {
  341. mainWindow.show();
  342. driver.quit();
  343. });
  344. }
  345. function handleOpenInvoke(event, lang = "en") {
  346. const window = new BrowserWindow({icon: iconPath});
  347. let url = "";
  348. language = lang;
  349. if (lang == "en") {
  350. url = server_address + `/taskGrid/taskList.html?type=1&wsport=${websocket_port}&backEndAddressServiceWrapper=` + server_address;
  351. } else if (lang == "zh") {
  352. url = server_address + `/taskGrid/taskList.html?type=1&wsport=${websocket_port}&backEndAddressServiceWrapper=` + server_address + "&lang=zh";
  353. }
  354. // and load the index.html of the app.
  355. window.loadURL(url);
  356. window.maximize();
  357. mainWindow.hide();
  358. window.on('close', function (event) {
  359. mainWindow.show();
  360. });
  361. }
  362. // This method will be called when Electron has finished
  363. // initialization and is ready to create browser windows.
  364. // Some APIs can only be used after this event occurs.
  365. app.whenReady().then(() => {
  366. ipcMain.on('start-design', handleOpenBrowser);
  367. ipcMain.on('start-invoke', handleOpenInvoke);
  368. createWindow();
  369. app.on('activate', function () {
  370. // On macOS it's common to re-create a window in the app when the
  371. // dock icon is clicked and there are no other windows open.
  372. if (BrowserWindow.getAllWindows().length === 0) {
  373. createWindow();
  374. }
  375. })
  376. })
  377. // Quit when all windows are closed, except on macOS. There, it's common
  378. // for applications and their menu bar to stay active until the user quits
  379. // explicitly with Cmd + Q.
  380. app.on('window-all-closed', function () {
  381. if (process.platform !== 'darwin') {
  382. app.quit();
  383. }
  384. })
  385. // In this file you can include the rest of your app's specific main process
  386. // code. You can also put them in separate files and require them here.
  387. function arrayDifference(arr1, arr2) {
  388. return arr1.filter(item => !arr2.includes(item));
  389. }