SHARE
TWEET

Untitled

a guest Oct 21st, 2019 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* eslint global-require: off */
  2. /* eslint no-unreachable: off */
  3. /** * This module executes inside of electron's main process. You can start * electron renderer process from here and communicate with the other processes * through IPC.
  4.  *
  5.  * When running `yarn build` or `yarn build-main`, this file is compiled to
  6.  * `./app/main.prod.js` using webpack. This gives us some performance wins.
  7.  *
  8.  * @flow
  9.  */
  10. import {
  11.   app,
  12.   BrowserWindow,
  13.   session,
  14.   ipcMain,
  15.   Menu,
  16.   crashReporter
  17. } from "electron";
  18. import * as keytar from "keytar";
  19. import open from "open";
  20. import * as Sentry from "@sentry/node";
  21. import * as taskBot from "./utils/taskBot";
  22. import profile_converter from "./utils/profile-conveter";
  23. import { setTestUrl } from "./utils/checkProxy";
  24. import { update as discordRichPresenceUpdate } from "./discordRichPresence";
  25. import sentryDSN from "./sentryDSN";
  26. import { checkAppUpdates } from "./appUpdater";
  27.  
  28. const isProd = process.env.NODE_ENV === "production";
  29.  
  30. if (isProd) {
  31.   Sentry.init({
  32.     dsn: sentryDSN
  33.   });
  34. }
  35.  
  36. crashReporter.start({
  37.   productName: "AltX Bot tools",
  38.   companyName: "Aestell",
  39.   submitURL: "",
  40.   uploadToServer: false // TODO: remove later
  41. });
  42.  
  43. // spawn("../scripts/./scripts");
  44. let mainWindow = null;
  45.  
  46. if (isProd) {
  47.   const sourceMapSupport = require("source-map-support");
  48.   sourceMapSupport.install();
  49. }
  50.  
  51. if (
  52.   process.env.NODE_ENV === "development" ||
  53.   process.env.DEBUG_PROD === "true"
  54. ) {
  55.   require("electron-debug")();
  56. }
  57.  
  58. discordRichPresenceUpdate();
  59.  
  60. /* const installExtensions = async () => {
  61.   const installer = require("electron-devtools-installer");
  62.   const forceDownload = !!process.env.UPGRADE_EXTENSIONS;
  63.   const extensions = ["REACT_DEVELOPER_TOOLS", "REDUX_DEVTOOLS"];
  64.  
  65.   return Promise.all(
  66.     extensions.map(name => installer.default(installer[name], forceDownload))
  67.   ).catch(console.log);
  68. }; */
  69.  
  70. /**
  71.  * Add event listeners...
  72.  */
  73.  
  74. app.on("window-all-closed", () => {
  75.   // Respect the OSX convention of having the application in memory even
  76.   // after all windows have been closed
  77.   if (process.platform !== "darwin") {
  78.     app.quit();
  79.   }
  80. });
  81. app.commandLine.appendSwitch("disable-web-security");
  82. app.disableHardwareAcceleration();
  83. app.on("ready", async () => {
  84.   try {
  85.     // await session.clearCache();
  86.   } catch (err) {
  87.     console.error(err);
  88.   }
  89.   session.defaultSession.webRequest.onHeadersReceived((details, callback) => {
  90.     callback({
  91.       responseHeaders: {
  92.         ...details.responseHeaders,
  93.         "Content-Security-Policy": ["default-src 'none'"]
  94.       }
  95.     });
  96.   });
  97.   // if (
  98.   //   process.env.NODE_ENV === "development" ||
  99.   //   process.env.DEBUG_PROD === "true"
  100.   // ) {
  101.   //   await installExtensions();
  102.   // }
  103.  
  104.   mainWindow = new BrowserWindow({
  105.     show: false,
  106.     minWidth: 450,
  107.     minHeight: 200,
  108.     width: 450,
  109.     height: 200,
  110.     frame: false,
  111.     transparent: true,
  112.     webPreferences: {
  113.       nodeIntegration: true
  114.     }
  115.   });
  116.  
  117.   mainWindow.setResizable(false);
  118.  
  119.   mainWindow.loadURL(`file://${__dirname}/app.html`);
  120.   mainWindow.setMenu(null);
  121.   // @TODO: Use 'ready-to-show' event
  122.   //        https://github.com/electron/electron/blob/master/docs/api/browser-window.md#using-ready-to-show-event
  123.   mainWindow.webContents.on("did-finish-load", () => {
  124.     if (!mainWindow) {
  125.       throw new Error('"mainWindow" is not defined');
  126.     }
  127.     if (process.env.START_MINIMIZED) {
  128.       mainWindow.minimize();
  129.     } else {
  130.       mainWindow.show();
  131.       mainWindow.focus();
  132.     }
  133.   });
  134.  
  135.   // mainWindow.webContents.openDevTools();
  136.  
  137.   mainWindow.webContents.on("new-window", (event, url) => {
  138.     event.preventDefault();
  139.     open(url);
  140.   });
  141.  
  142.   mainWindow.on("closed", () => {
  143.     mainWindow = null;
  144.   });
  145.  
  146.   const template = [
  147.     {
  148.       label: "Application",
  149.       submenu: [
  150.         {
  151.           label: "About Application",
  152.           selector: "orderFrontStandardAboutPanel:"
  153.         },
  154.         { type: "separator" },
  155.         {
  156.           label: "Quit",
  157.           accelerator: "Command+Q",
  158.           click() {
  159.             app.quit();
  160.           }
  161.         }
  162.       ]
  163.     },
  164.     {
  165.       label: "Edit",
  166.       submenu: [
  167.         { label: "Undo", accelerator: "CmdOrCtrl+Z", selector: "undo:" },
  168.         { label: "Redo", accelerator: "Shift+CmdOrCtrl+Z", selector: "redo:" },
  169.         { type: "separator" },
  170.         { label: "Cut", accelerator: "CmdOrCtrl+X", selector: "cut:" },
  171.         { label: "Copy", accelerator: "CmdOrCtrl+C", selector: "copy:" },
  172.         { label: "Paste", accelerator: "CmdOrCtrl+V", selector: "paste:" },
  173.         {
  174.           label: "Select All",
  175.           accelerator: "CmdOrCtrl+A",
  176.           selector: "selectAll:"
  177.         }
  178.       ]
  179.     }
  180.   ];
  181.  
  182.   Menu.setApplicationMenu(Menu.buildFromTemplate(template));
  183.  
  184.   taskBot.setSender(mainWindow.webContents);
  185.  
  186.   // const menuBuilder = new MenuBuilder(mainWindow);
  187.   // menuBuilder.buildMenu();
  188. });
  189.  
  190. /**
  191.  * Add iPC events
  192.  */
  193. // eslint-disable-next-line no-unused-vars
  194. const browsers = [];
  195. ipcMain.on("runCommand", async () => {});
  196.  
  197. ipcMain.on("startTask", async (event, task) => {
  198.   taskBot.addTask(task);
  199. });
  200.  
  201. ipcMain.on("stopTask", async (event, task) => {
  202.   taskBot.stopTask(task);
  203. });
  204.  
  205. ipcMain.on("showTask", async (event, task) => {
  206.   taskBot.showTask(task);
  207. });
  208.  
  209. ipcMain.on("hideTask", async (event, task) => {
  210.   taskBot.hideTask(task);
  211. });
  212.  
  213. ipcMain.on("changeSetting", async (event, data) => {
  214.   taskBot.changeSetting(
  215.     data.duration,
  216.     data.maxProfile,
  217.     data.gsearch,
  218.     data.youtube
  219.   );
  220. });
  221.  
  222. ipcMain.on("closeWindow", () => {
  223.   mainWindow.webContents.send("saveAccounts");
  224.   mainWindow.webContents.send("saveProxies");
  225.   mainWindow.webContents.send("saveSettings");
  226.   mainWindow.webContents.send("saveBillings");
  227.   app.exit();
  228. });
  229.  
  230. ipcMain.on("miniWindow", () => {
  231.   mainWindow.minimize();
  232. });
  233.  
  234. process.on("uncaughtException", err => {
  235.   if (!isProd) {
  236.     console.error("uncaught", err);
  237.   }
  238. });
  239.  
  240. ipcMain.on("convertProfile", async (event, data) => {
  241.   const { src_path, dst_path, src_format, dst_format } = data;
  242.   try {
  243.     await profile_converter(src_path, dst_path, src_format, dst_format);
  244.     // eslint-disable-next-line
  245.     event.returnValue = "converted";
  246.   } catch (err) {
  247.     if (!isProd) {
  248.       console.error(err);
  249.     }
  250.     Sentry.captureException(err);
  251.     // eslint-disable-next-line
  252.     event.returnValue = err.message;
  253.   }
  254. });
  255.  
  256. ipcMain.on("setTestUrl", (event, data) => {
  257.   setTestUrl(data.url);
  258. });
  259.  
  260. ipcMain.on("activated", (event, data) => {
  261.   // console.log("activated");
  262.   mainWindow.setSize(1366, 768);
  263.   mainWindow.setMinimumSize(1366, 768);
  264.   mainWindow.setPosition(200, 100);
  265.   if (data && data.apiKey) keytar.setPassword("apiKey", "OCIO", data.apiKey);
  266. });
  267.  
  268. ipcMain.on("getApiKey", async event => {
  269.   const key = await keytar.getPassword("apiKey", "OCIO");
  270.   // keytar.deletePassword("apiKey", "OCIO");
  271.   // eslint-disable-next-line
  272.   event.returnValue = key;
  273. });
  274.  
  275. ipcMain.on("resetApiKey", async () => {
  276.   mainWindow.setSize(450, 200);
  277.   mainWindow.setContentSize(450, 200);
  278.   mainWindow.center();
  279.   return keytar.deletePassword("apiKey", "OCIO");
  280. });
  281.  
  282. ipcMain.on("stopAndLogout", async () => {
  283.   try {
  284.     await taskBot.stopAll();
  285.   } catch (err) {
  286.     if (!isProd) {
  287.       console.error(err);
  288.     }
  289.     Sentry.captureException(err);
  290.   } finally {
  291.     mainWindow.setSize(450, 200);
  292.     mainWindow.setContentSize(450, 200);
  293.     mainWindow.center();
  294.     return keytar.deletePassword("apiKey", "OCIO");
  295.   }
  296. });
  297.  
  298. ipcMain.on("startOneClick", async (event, task) => {
  299.   taskBot.startOneClick(task);
  300. });
  301.  
  302. ipcMain.on("windowError", (event, error) => {
  303.   if (!isProd) {
  304.     console.error(error);
  305.   }
  306.   Sentry.captureException(error);
  307. });
  308.  
  309. ipcMain.on("getAppDataPath", event => {
  310.   // eslint-disable-next-line
  311.   event.returnValue = app.getPath("appData");
  312. });
  313.  
  314. ipcMain.on("checkAppUpdates", event => {
  315.   checkAppUpdates().then(updateInfo => {
  316.     event.sender.send("appUpdateInfo", updateInfo);
  317.   });
  318. });
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top