uicore.js 8.0 KB

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