uicore.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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('./assets/js/isdev')
  11. const LoggerUtil = require('./assets/js/loggerutil')
  12. const loggerUICore = LoggerUtil('%c[UICore]', 'color: #000668; font-weight: bold')
  13. const loggerAutoUpdater = LoggerUtil('%c[AutoUpdater]', 'color: #000668; font-weight: bold')
  14. const loggerAutoUpdaterSuccess = LoggerUtil('%c[AutoUpdater]', 'color: #209b07; font-weight: bold')
  15. // Log deprecation and process warnings.
  16. process.traceProcessWarnings = true
  17. process.traceDeprecation = true
  18. // Disable eval function.
  19. // eslint-disable-next-line
  20. window.eval = global.eval = function () {
  21. throw new Error('Sorry, this app does not support window.eval().')
  22. }
  23. // Display warning when devtools window is opened.
  24. remote.getCurrentWebContents().on('devtools-opened', () => {
  25. console.log('%cThe console is dark and full of terrors.', 'color: white; -webkit-text-stroke: 4px #a02d2a; font-size: 60px; font-weight: bold')
  26. console.log('%cIf you\'ve been told to paste something here, you\'re being scammed.', 'font-size: 16px')
  27. console.log('%cUnless you know exactly what you\'re doing, close this window.', 'font-size: 16px')
  28. })
  29. // Disable zoom, needed for darwin.
  30. webFrame.setZoomLevel(0)
  31. webFrame.setVisualZoomLevelLimits(1, 1)
  32. webFrame.setLayoutZoomLevelLimits(0, 0)
  33. // Initialize auto updates in production environments.
  34. let updateCheckListener
  35. if(!isDev){
  36. ipcRenderer.on('autoUpdateNotification', (event, arg, info) => {
  37. switch(arg){
  38. case 'checking-for-update':
  39. loggerAutoUpdater.log('Checking for update..')
  40. settingsUpdateButtonStatus('Checking for Updates..', true)
  41. break
  42. case 'update-available':
  43. loggerAutoUpdaterSuccess.log('New update available', info.version)
  44. populateSettingsUpdateInformation(info)
  45. break
  46. case 'update-downloaded':
  47. loggerAutoUpdaterSuccess.log('Update ' + info.version + ' ready to be installed.')
  48. settingsUpdateButtonStatus('Install Now', false, () => {
  49. if(!isDev){
  50. ipcRenderer.send('autoUpdateAction', 'installUpdateNow')
  51. }
  52. })
  53. showUpdateUI(info)
  54. break
  55. case 'update-not-available':
  56. loggerAutoUpdater.log('No new update found.')
  57. settingsUpdateButtonStatus('Check for Updates')
  58. break
  59. case 'ready':
  60. updateCheckListener = setInterval(() => {
  61. ipcRenderer.send('autoUpdateAction', 'checkForUpdate')
  62. }, 1800000)
  63. ipcRenderer.send('autoUpdateAction', 'checkForUpdate')
  64. break
  65. case 'realerror':
  66. if(info != null && info.code != null){
  67. if(info.code === 'ERR_UPDATER_INVALID_RELEASE_FEED'){
  68. loggerAutoUpdater.log('No suitable releases found.')
  69. } else if(info.code === 'ERR_XML_MISSED_ELEMENT'){
  70. loggerAutoUpdater.log('No releases found.')
  71. } else {
  72. loggerAutoUpdater.error('Error during update check..', info)
  73. loggerAutoUpdater.debug('Error Code:', info.code)
  74. }
  75. }
  76. break
  77. default:
  78. loggerAutoUpdater.log('Unknown argument', arg)
  79. break
  80. }
  81. })
  82. }
  83. /**
  84. * Send a notification to the main process changing the value of
  85. * allowPrerelease. If we are running a prerelease version, then
  86. * this will always be set to true, regardless of the current value
  87. * of val.
  88. *
  89. * @param {boolean} val The new allow prerelease value.
  90. */
  91. function changeAllowPrerelease(val){
  92. ipcRenderer.send('autoUpdateAction', 'allowPrereleaseChange', val)
  93. }
  94. function showUpdateUI(info){
  95. //TODO Make this message a bit more informative `${info.version}`
  96. document.getElementById('image_seal_container').setAttribute('update', true)
  97. document.getElementById('image_seal_container').onclick = () => {
  98. /*setOverlayContent('Update Available', 'A new update for the launcher is available. Would you like to install now?', 'Install', 'Later')
  99. setOverlayHandler(() => {
  100. if(!isDev){
  101. ipcRenderer.send('autoUpdateAction', 'installUpdateNow')
  102. } else {
  103. console.error('Cannot install updates in development environment.')
  104. toggleOverlay(false)
  105. }
  106. })
  107. setDismissHandler(() => {
  108. toggleOverlay(false)
  109. })
  110. toggleOverlay(true, true)*/
  111. switchView(getCurrentView(), VIEWS.settings, 500, 500, () => {
  112. settingsNavItemListener(document.getElementById('settingsNavUpdate'), false)
  113. })
  114. }
  115. }
  116. /* jQuery Example
  117. $(function(){
  118. loggerUICore.log('UICore Initialized');
  119. })*/
  120. document.addEventListener('readystatechange', function () {
  121. if (document.readyState === 'interactive'){
  122. loggerUICore.log('UICore Initializing..')
  123. // Bind close button.
  124. Array.from(document.getElementsByClassName('fCb')).map((val) => {
  125. val.addEventListener('click', e => {
  126. const window = remote.getCurrentWindow()
  127. window.close()
  128. })
  129. })
  130. // Bind restore down button.
  131. Array.from(document.getElementsByClassName('fRb')).map((val) => {
  132. val.addEventListener('click', e => {
  133. const window = remote.getCurrentWindow()
  134. if(window.isMaximized()){
  135. window.unmaximize()
  136. } else {
  137. window.maximize()
  138. }
  139. document.activeElement.blur()
  140. })
  141. })
  142. // Bind minimize button.
  143. Array.from(document.getElementsByClassName('fMb')).map((val) => {
  144. val.addEventListener('click', e => {
  145. const window = remote.getCurrentWindow()
  146. window.minimize()
  147. document.activeElement.blur()
  148. })
  149. })
  150. // Remove focus from social media buttons once they're clicked.
  151. Array.from(document.getElementsByClassName('mediaURL')).map(val => {
  152. val.addEventListener('click', e => {
  153. document.activeElement.blur()
  154. })
  155. })
  156. } else if(document.readyState === 'complete'){
  157. //266.01
  158. //170.8
  159. //53.21
  160. // Bind progress bar length to length of bot wrapper
  161. //const targetWidth = document.getElementById("launch_content").getBoundingClientRect().width
  162. //const targetWidth2 = document.getElementById("server_selection").getBoundingClientRect().width
  163. //const targetWidth3 = document.getElementById("launch_button").getBoundingClientRect().width
  164. document.getElementById('launch_details').style.maxWidth = 266.01
  165. document.getElementById('launch_progress').style.width = 170.8
  166. document.getElementById('launch_details_right').style.maxWidth = 170.8
  167. document.getElementById('launch_progress_label').style.width = 53.21
  168. }
  169. }, false)
  170. /**
  171. * Open web links in the user's default browser.
  172. */
  173. $(document).on('click', 'a[href^="http"]', function(event) {
  174. event.preventDefault()
  175. shell.openExternal(this.href)
  176. })
  177. /**
  178. * Opens DevTools window if you hold (ctrl + shift + i).
  179. * This will crash the program if you are using multiple
  180. * DevTools, for example the chrome debugger in VS Code.
  181. */
  182. document.addEventListener('keydown', function (e) {
  183. if((e.key === 'I' || e.key === 'i') && e.ctrlKey && e.shiftKey){
  184. let window = remote.getCurrentWindow()
  185. window.toggleDevTools()
  186. }
  187. })