uicore.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /**
  2. * Core UI functions are initialized in this file. This prevents
  3. * unexpected errors from breaking the core features. Specifically,
  4. * actions in this file should not require the usage of any internal
  5. * modules, excluding dependencies.
  6. */
  7. // Requirements
  8. const $ = require('jquery');
  9. const {ipcRenderer, remote, shell, webFrame} = require('electron')
  10. const isDev = require('electron-is-dev')
  11. // Disable eval function.
  12. // eslint-disable-next-line
  13. window.eval = global.eval = function () {
  14. throw new Error('Sorry, this app does not support window.eval().')
  15. }
  16. // Display warning when devtools window is opened.
  17. remote.getCurrentWebContents().on('devtools-opened', () => {
  18. console.log('%cThe console is dark and full of terrors.', 'color: white; -webkit-text-stroke: 4px #a02d2a; font-size: 60px; font-weight: bold')
  19. console.log('%cIf you\'ve been told to paste something here, you\'re being scammed.', 'font-size: 16px')
  20. console.log('%cUnless you know exactly what you\'re doing, close this window.', 'font-size: 16px')
  21. })
  22. // Disable zoom, needed for darwin.
  23. webFrame.setZoomLevel(0)
  24. webFrame.setVisualZoomLevelLimits(1, 1)
  25. webFrame.setLayoutZoomLevelLimits(0, 0)
  26. // Initialize auto updates in production environments.
  27. // TODO Make this the case after implementation is done.
  28. let updateCheckListener
  29. if(!isDev){
  30. ipcRenderer.on('autoUpdateNotification', (event, arg, info) => {
  31. switch(arg){
  32. case 'checking-for-update':
  33. console.log('%c[AutoUpdater]', 'color: #a02d2a; font-weight: bold', 'Checking for update..')
  34. break
  35. case 'update-available':
  36. console.log('%c[AutoUpdater]', 'color: #a02d2a; font-weight: bold', 'New update available', info.version)
  37. break
  38. case 'update-downloaded':
  39. console.log('%c[AutoUpdater]', 'color: #a02d2a; font-weight: bold', 'Update ' + info.version + ' ready to be installed.')
  40. showUpdateUI(info)
  41. break
  42. case 'update-not-available':
  43. console.log('%c[AutoUpdater]', 'color: #a02d2a; font-weight: bold', 'No new update found.')
  44. break
  45. case 'ready':
  46. updateCheckListener = setInterval(() => {
  47. ipcRenderer.send('autoUpdateAction', 'checkForUpdate')
  48. }, 1800000)
  49. ipcRenderer.send('autoUpdateAction', 'checkForUpdate')
  50. case 'realerror':
  51. if(info != null && info.code != null){
  52. if(info.code === 'ERR_UPDATER_INVALID_RELEASE_FEED'){
  53. console.log('%c[AutoUpdater]', 'color: #a02d2a; font-weight: bold', 'No suitable releases found.')
  54. } else if(info.code === 'ERR_XML_MISSED_ELEMENT'){
  55. console.log('%c[AutoUpdater]', 'color: #a02d2a; font-weight: bold', 'No releases found.')
  56. } else {
  57. console.error('%c[AutoUpdater]', 'color: #a02d2a; font-weight: bold', 'Error during update check..', info)
  58. console.debug('%c[AutoUpdater]', 'color: #a02d2a; font-weight: bold', 'Error Code:', info.code)
  59. }
  60. }
  61. break
  62. default:
  63. console.log('%c[AutoUpdater]', 'color: #a02d2a; font-weight: bold', 'Unknown argument', arg)
  64. break
  65. }
  66. })
  67. ipcRenderer.send('autoUpdateAction', 'initAutoUpdater')
  68. }
  69. function showUpdateUI(info){
  70. //TODO Make this message a bit more informative `${info.version}`
  71. document.getElementById('image_seal_container').setAttribute('update', true)
  72. document.getElementById('image_seal_container').onclick = () => {
  73. setOverlayContent('Update Available', 'A new update for the launcher is available. Would you like to install now?', 'Install', 'Later')
  74. setOverlayHandler(() => {
  75. if(!isDev){
  76. ipcRenderer.send('autoUpdateAction', 'installUpdateNow')
  77. } else {
  78. console.error('Cannot install updates in development environment.')
  79. toggleOverlay(false)
  80. }
  81. })
  82. setDismissHandler(() => {
  83. toggleOverlay(false)
  84. })
  85. toggleOverlay(true, true)
  86. }
  87. }
  88. /* jQuery Example
  89. $(function(){
  90. console.log('UICore Initialized');
  91. })*/
  92. document.addEventListener('readystatechange', function () {
  93. if (document.readyState === 'interactive'){
  94. console.log('UICore Initializing..');
  95. // Bind close button.
  96. Array.from(document.getElementsByClassName('fCb')).map((val) => {
  97. val.addEventListener('click', e => {
  98. const window = remote.getCurrentWindow()
  99. window.close()
  100. })
  101. })
  102. // Bind restore down button.
  103. Array.from(document.getElementsByClassName('fRb')).map((val) => {
  104. val.addEventListener('click', e => {
  105. const window = remote.getCurrentWindow()
  106. if(window.isMaximized()){
  107. window.unmaximize()
  108. } else {
  109. window.maximize()
  110. }
  111. document.activeElement.blur()
  112. })
  113. })
  114. // Bind minimize button.
  115. Array.from(document.getElementsByClassName('fMb')).map((val) => {
  116. val.addEventListener('click', e => {
  117. const window = remote.getCurrentWindow()
  118. window.minimize()
  119. document.activeElement.blur()
  120. })
  121. })
  122. } else if(document.readyState === 'complete'){
  123. //266.01
  124. //170.8
  125. //53.21
  126. // Bind progress bar length to length of bot wrapper
  127. //const targetWidth = document.getElementById("launch_content").getBoundingClientRect().width
  128. //const targetWidth2 = document.getElementById("server_selection").getBoundingClientRect().width
  129. //const targetWidth3 = document.getElementById("launch_button").getBoundingClientRect().width
  130. document.getElementById("launch_details").style.maxWidth = 266.01
  131. document.getElementById("launch_progress").style.width = 170.8
  132. document.getElementById("launch_details_right").style.maxWidth = 170.8
  133. document.getElementById("launch_progress_label").style.width = 53.21
  134. }
  135. }, false)
  136. /**
  137. * Open web links in the user's default browser.
  138. */
  139. $(document).on('click', 'a[href^="http"]', function(event) {
  140. event.preventDefault();
  141. //console.log(os.homedir())
  142. shell.openExternal(this.href)
  143. })
  144. /**
  145. * Opens DevTools window if you hold (ctrl + shift + i).
  146. * This will crash the program if you are using multiple
  147. * DevTools, for example the chrome debugger in VS Code.
  148. */
  149. document.addEventListener('keydown', function (e) {
  150. if(e.key === 'I' && e.ctrlKey && e.shiftKey){
  151. let window = remote.getCurrentWindow()
  152. window.toggleDevTools()
  153. }
  154. })