uicore.js 7.4 KB

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