index.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. const remoteMain = require('@electron/remote/main')
  2. remoteMain.initialize()
  3. // Requirements
  4. const { app, BrowserWindow, ipcMain, Menu } = require('electron')
  5. const autoUpdater = require('electron-updater').autoUpdater
  6. const ejse = require('ejs-electron')
  7. const fs = require('fs')
  8. const isDev = require('./app/assets/js/isdev')
  9. const path = require('path')
  10. const semver = require('semver')
  11. const { pathToFileURL } = require('url')
  12. const { AZURE_CLIENT_ID, MSFT_OPCODE, MSFT_REPLY_TYPE, MSFT_ERROR } = require('./app/assets/js/ipcconstants')
  13. // Setup auto updater.
  14. function initAutoUpdater(event, data) {
  15. if(data){
  16. autoUpdater.allowPrerelease = true
  17. } else {
  18. // Defaults to true if application version contains prerelease components (e.g. 0.12.1-alpha.1)
  19. // autoUpdater.allowPrerelease = true
  20. }
  21. if(isDev){
  22. autoUpdater.autoInstallOnAppQuit = false
  23. autoUpdater.updateConfigPath = path.join(__dirname, 'dev-app-update.yml')
  24. }
  25. if(process.platform === 'darwin'){
  26. autoUpdater.autoDownload = false
  27. }
  28. autoUpdater.on('update-available', (info) => {
  29. event.sender.send('autoUpdateNotification', 'update-available', info)
  30. })
  31. autoUpdater.on('update-downloaded', (info) => {
  32. event.sender.send('autoUpdateNotification', 'update-downloaded', info)
  33. })
  34. autoUpdater.on('update-not-available', (info) => {
  35. event.sender.send('autoUpdateNotification', 'update-not-available', info)
  36. })
  37. autoUpdater.on('checking-for-update', () => {
  38. event.sender.send('autoUpdateNotification', 'checking-for-update')
  39. })
  40. autoUpdater.on('error', (err) => {
  41. event.sender.send('autoUpdateNotification', 'realerror', err)
  42. })
  43. }
  44. // Open channel to listen for update actions.
  45. ipcMain.on('autoUpdateAction', (event, arg, data) => {
  46. switch(arg){
  47. case 'initAutoUpdater':
  48. console.log('Initializing auto updater.')
  49. initAutoUpdater(event, data)
  50. event.sender.send('autoUpdateNotification', 'ready')
  51. break
  52. case 'checkForUpdate':
  53. autoUpdater.checkForUpdates()
  54. .catch(err => {
  55. event.sender.send('autoUpdateNotification', 'realerror', err)
  56. })
  57. break
  58. case 'allowPrereleaseChange':
  59. if(!data){
  60. const preRelComp = semver.prerelease(app.getVersion())
  61. if(preRelComp != null && preRelComp.length > 0){
  62. autoUpdater.allowPrerelease = true
  63. } else {
  64. autoUpdater.allowPrerelease = data
  65. }
  66. } else {
  67. autoUpdater.allowPrerelease = data
  68. }
  69. break
  70. case 'installUpdateNow':
  71. autoUpdater.quitAndInstall()
  72. break
  73. default:
  74. console.log('Unknown argument', arg)
  75. break
  76. }
  77. })
  78. // Redirect distribution index event from preloader to renderer.
  79. ipcMain.on('distributionIndexDone', (event, res) => {
  80. event.sender.send('distributionIndexDone', res)
  81. })
  82. // Disable hardware acceleration.
  83. // https://electronjs.org/docs/tutorial/offscreen-rendering
  84. app.disableHardwareAcceleration()
  85. const REDIRECT_URI_PREFIX = 'https://login.microsoftonline.com/common/oauth2/nativeclient?'
  86. // Microsoft Auth Login
  87. let msftAuthWindow
  88. let msftAuthSuccess
  89. let msftAuthViewSuccess
  90. let msftAuthViewOnClose
  91. ipcMain.on(MSFT_OPCODE.OPEN_LOGIN, (ipcEvent, ...arguments_) => {
  92. if (msftAuthWindow) {
  93. ipcEvent.reply(MSFT_OPCODE.REPLY_LOGIN, MSFT_REPLY_TYPE.ERROR, MSFT_ERROR.ALREADY_OPEN, msftAuthViewOnClose)
  94. return
  95. }
  96. msftAuthSuccess = false
  97. msftAuthViewSuccess = arguments_[0]
  98. msftAuthViewOnClose = arguments_[1]
  99. msftAuthWindow = new BrowserWindow({
  100. title: 'Microsoft Login',
  101. backgroundColor: '#222222',
  102. width: 520,
  103. height: 600,
  104. frame: true,
  105. icon: getPlatformIcon('SealCircle')
  106. })
  107. msftAuthWindow.on('closed', () => {
  108. msftAuthWindow = undefined
  109. })
  110. msftAuthWindow.on('close', () => {
  111. if(!msftAuthSuccess) {
  112. ipcEvent.reply(MSFT_OPCODE.REPLY_LOGIN, MSFT_REPLY_TYPE.ERROR, MSFT_ERROR.NOT_FINISHED, msftAuthViewOnClose)
  113. }
  114. })
  115. msftAuthWindow.webContents.on('did-navigate', (_, uri) => {
  116. if (uri.startsWith(REDIRECT_URI_PREFIX)) {
  117. let queries = uri.substring(REDIRECT_URI_PREFIX.length).split('#', 1).toString().split('&')
  118. let queryMap = {}
  119. queries.forEach(query => {
  120. const [name, value] = query.split('=')
  121. queryMap[name] = decodeURI(value)
  122. })
  123. ipcEvent.reply(MSFT_OPCODE.REPLY_LOGIN, MSFT_REPLY_TYPE.SUCCESS, queryMap, msftAuthViewSuccess)
  124. msftAuthSuccess = true
  125. msftAuthWindow.close()
  126. msftAuthWindow = null
  127. }
  128. })
  129. msftAuthWindow.removeMenu()
  130. msftAuthWindow.loadURL(`https://login.microsoftonline.com/consumers/oauth2/v2.0/authorize?prompt=select_account&client_id=${AZURE_CLIENT_ID}&response_type=code&scope=XboxLive.signin%20offline_access&redirect_uri=https://login.microsoftonline.com/common/oauth2/nativeclient`)
  131. })
  132. // Microsoft Auth Logout
  133. let msftLogoutWindow
  134. let msftLogoutSuccess
  135. let msftLogoutSuccessSent
  136. ipcMain.on(MSFT_OPCODE.OPEN_LOGOUT, (ipcEvent, uuid, isLastAccount) => {
  137. if (msftLogoutWindow) {
  138. ipcEvent.reply(MSFT_OPCODE.REPLY_LOGOUT, MSFT_REPLY_TYPE.ERROR, MSFT_ERROR.ALREADY_OPEN)
  139. return
  140. }
  141. msftLogoutSuccess = false
  142. msftLogoutSuccessSent = false
  143. msftLogoutWindow = new BrowserWindow({
  144. title: 'Microsoft Logout',
  145. backgroundColor: '#222222',
  146. width: 520,
  147. height: 600,
  148. frame: true,
  149. icon: getPlatformIcon('SealCircle')
  150. })
  151. msftLogoutWindow.on('closed', () => {
  152. msftLogoutWindow = undefined
  153. })
  154. msftLogoutWindow.on('close', () => {
  155. if(!msftLogoutSuccess) {
  156. ipcEvent.reply(MSFT_OPCODE.REPLY_LOGOUT, MSFT_REPLY_TYPE.ERROR, MSFT_ERROR.NOT_FINISHED)
  157. } else if(!msftLogoutSuccessSent) {
  158. msftLogoutSuccessSent = true
  159. ipcEvent.reply(MSFT_OPCODE.REPLY_LOGOUT, MSFT_REPLY_TYPE.SUCCESS, uuid, isLastAccount)
  160. }
  161. })
  162. msftLogoutWindow.webContents.on('did-navigate', (_, uri) => {
  163. if(uri.startsWith('https://login.microsoftonline.com/common/oauth2/v2.0/logoutsession')) {
  164. msftLogoutSuccess = true
  165. setTimeout(() => {
  166. if(!msftLogoutSuccessSent) {
  167. msftLogoutSuccessSent = true
  168. ipcEvent.reply(MSFT_OPCODE.REPLY_LOGOUT, MSFT_REPLY_TYPE.SUCCESS, uuid, isLastAccount)
  169. }
  170. if(msftLogoutWindow) {
  171. msftLogoutWindow.close()
  172. msftLogoutWindow = null
  173. }
  174. }, 5000)
  175. }
  176. })
  177. msftLogoutWindow.removeMenu()
  178. msftLogoutWindow.loadURL('https://login.microsoftonline.com/common/oauth2/v2.0/logout')
  179. })
  180. // Keep a global reference of the window object, if you don't, the window will
  181. // be closed automatically when the JavaScript object is garbage collected.
  182. let win
  183. function createWindow() {
  184. win = new BrowserWindow({
  185. width: 980,
  186. height: 552,
  187. icon: getPlatformIcon('SealCircle'),
  188. frame: false,
  189. webPreferences: {
  190. preload: path.join(__dirname, 'app', 'assets', 'js', 'preloader.js'),
  191. nodeIntegration: true,
  192. contextIsolation: false
  193. },
  194. backgroundColor: '#171614'
  195. })
  196. remoteMain.enable(win.webContents)
  197. ejse.data('bkid', Math.floor((Math.random() * fs.readdirSync(path.join(__dirname, 'app', 'assets', 'images', 'backgrounds')).length)))
  198. win.loadURL(pathToFileURL(path.join(__dirname, 'app', 'app.ejs')).toString())
  199. /*win.once('ready-to-show', () => {
  200. win.show()
  201. })*/
  202. win.removeMenu()
  203. win.resizable = true
  204. win.on('closed', () => {
  205. win = null
  206. })
  207. }
  208. function createMenu() {
  209. if(process.platform === 'darwin') {
  210. // Extend default included application menu to continue support for quit keyboard shortcut
  211. let applicationSubMenu = {
  212. label: 'Application',
  213. submenu: [{
  214. label: 'About Application',
  215. selector: 'orderFrontStandardAboutPanel:'
  216. }, {
  217. type: 'separator'
  218. }, {
  219. label: 'Quit',
  220. accelerator: 'Command+Q',
  221. click: () => {
  222. app.quit()
  223. }
  224. }]
  225. }
  226. // New edit menu adds support for text-editing keyboard shortcuts
  227. let editSubMenu = {
  228. label: 'Edit',
  229. submenu: [{
  230. label: 'Undo',
  231. accelerator: 'CmdOrCtrl+Z',
  232. selector: 'undo:'
  233. }, {
  234. label: 'Redo',
  235. accelerator: 'Shift+CmdOrCtrl+Z',
  236. selector: 'redo:'
  237. }, {
  238. type: 'separator'
  239. }, {
  240. label: 'Cut',
  241. accelerator: 'CmdOrCtrl+X',
  242. selector: 'cut:'
  243. }, {
  244. label: 'Copy',
  245. accelerator: 'CmdOrCtrl+C',
  246. selector: 'copy:'
  247. }, {
  248. label: 'Paste',
  249. accelerator: 'CmdOrCtrl+V',
  250. selector: 'paste:'
  251. }, {
  252. label: 'Select All',
  253. accelerator: 'CmdOrCtrl+A',
  254. selector: 'selectAll:'
  255. }]
  256. }
  257. // Bundle submenus into a single template and build a menu object with it
  258. let menuTemplate = [applicationSubMenu, editSubMenu]
  259. let menuObject = Menu.buildFromTemplate(menuTemplate)
  260. // Assign it to the application
  261. Menu.setApplicationMenu(menuObject)
  262. }
  263. }
  264. function getPlatformIcon(filename){
  265. let ext
  266. switch(process.platform) {
  267. case 'win32':
  268. ext = 'ico'
  269. break
  270. case 'darwin':
  271. case 'linux':
  272. default:
  273. ext = 'png'
  274. break
  275. }
  276. return path.join(__dirname, 'app', 'assets', 'images', `${filename}.${ext}`)
  277. }
  278. app.on('ready', createWindow)
  279. app.on('ready', createMenu)
  280. app.on('window-all-closed', () => {
  281. // On macOS it is common for applications and their menu bar
  282. // to stay active until the user quits explicitly with Cmd + Q
  283. if (process.platform !== 'darwin') {
  284. app.quit()
  285. }
  286. })
  287. app.on('activate', () => {
  288. // On macOS it's common to re-create a window in the app when the
  289. // dock icon is clicked and there are no other windows open.
  290. if (win === null) {
  291. createWindow()
  292. }
  293. })