uicore.js 7.4 KB

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