main.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /* eslint global-require: off, no-console: off, promise/always-return: off */
  2. /**
  3. * This module executes inside of electron's main process. You can start
  4. * electron renderer process from here and communicate with the other processes
  5. * through IPC.
  6. *
  7. * When running `npm run build` or `npm run build:main`, this file is compiled to
  8. * `./src/main.js` using webpack. This gives us some performance wins.
  9. */
  10. const path = require ('path');
  11. const { app, BrowserWindow, shell, ipcMain } = require('electron');
  12. const { autoUpdater } = require('electron-updater');
  13. const log = require('electron-log');
  14. const puppeteer = require('puppeteer');
  15. class AppUpdater {
  16. constructor() {
  17. log.transports.file.level = 'info';
  18. autoUpdater.logger = log;
  19. autoUpdater.checkForUpdatesAndNotify();
  20. }
  21. }
  22. let mainWindow = null;
  23. ipcMain.on('ipc-example', async (event, arg) => {
  24. const msgTemplate = (pingPong) => `IPC test: ${pingPong}`;
  25. console.log(msgTemplate(arg));
  26. event.reply('ipc-example', msgTemplate('pong'));
  27. });
  28. if (process.env.NODE_ENV === 'production') {
  29. const sourceMapSupport = require('source-map-support');
  30. sourceMapSupport.install();
  31. }
  32. const isDebug =
  33. process.env.NODE_ENV === 'development' || process.env.DEBUG_PROD === 'true';
  34. if (isDebug) {
  35. require('electron-debug')();
  36. }
  37. const installExtensions = async () => {
  38. const installer = require('electron-devtools-installer');
  39. const forceDownload = !!process.env.UPGRADE_EXTENSIONS;
  40. const extensions = ['REACT_DEVELOPER_TOOLS'];
  41. return installer
  42. .default(
  43. extensions.map((name) => installer[name]),
  44. forceDownload
  45. )
  46. .catch(console.log);
  47. };
  48. const createWindow = async () => {
  49. if (isDebug) {
  50. await installExtensions();
  51. }
  52. const RESOURCES_PATH = app.isPackaged
  53. ? path.join(process.resourcesPath, 'assets')
  54. : path.join(__dirname, '../../assets');
  55. const getAssetPath = (...paths) => {
  56. return path.join(RESOURCES_PATH, ...paths);
  57. };
  58. mainWindow = new BrowserWindow({
  59. show: false,
  60. width: 1024,
  61. height: 728,
  62. icon: getAssetPath('icon.png'),
  63. webPreferences: {
  64. preload: app.isPackaged
  65. ? path.join(__dirname, 'preload.js')
  66. : path.join(__dirname, '../../.erb/dll/preload.js'),
  67. },
  68. });
  69. // mainWindow.loadURL('');
  70. async function func() {
  71. const browser = await puppeteer.launch({
  72. headless: false,
  73. defaultViewport: false
  74. })
  75. const page = await browser.newPage();
  76. await page.goto('http://yct.sh.gov.cn/portal_yct/')
  77. await page.waitForSelector('#scroll_list')
  78. const data = await page.$$eval('#scroll_list', data => {
  79. return data.map(a => {
  80. return {
  81. 'title': a.children[0].title
  82. }
  83. });
  84. });
  85. console.log(data)
  86. // await browser.close();
  87. }
  88. puppeteer.launch().then(
  89. func()
  90. );
  91. mainWindow.on('ready-to-show', () => {
  92. if (!mainWindow) {
  93. throw new Error('"mainWindow" is not defined');
  94. }
  95. if (process.env.START_MINIMIZED) {
  96. mainWindow.minimize();
  97. } else {
  98. mainWindow.show();
  99. }
  100. });
  101. mainWindow.on('closed', () => {
  102. mainWindow = null;
  103. });
  104. // Open urls in the user's browser
  105. mainWindow.webContents.setWindowOpenHandler((edata) => {
  106. shell.openExternal(edata.url);
  107. return { action: 'deny' };
  108. });
  109. // Remove this if your app does not use auto updates
  110. // eslint-disable-next-line
  111. new AppUpdater();
  112. };
  113. /**
  114. * Add event listeners...
  115. */
  116. app.on('window-all-closed', () => {
  117. // Respect the OSX convention of having the application in memory even
  118. // after all windows have been closed
  119. if (process.platform !== 'darwin') {
  120. app.quit();
  121. }
  122. });
  123. app
  124. .whenReady()
  125. .then(() => {
  126. createWindow();
  127. app.on('activate', () => {
  128. // On macOS it's common to re-create a window in the app when the
  129. // dock icon is clicked and there are no other windows open.
  130. if (mainWindow === null) createWindow();
  131. });
  132. })
  133. .catch(console.log);