uicore.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. if(!isDev){
  29. ipcRenderer.on('autoUpdateNotification', (event, arg, info) => {
  30. switch(arg){
  31. case 'checking-for-update':
  32. console.log('Checking for update..')
  33. break
  34. case 'update-available':
  35. console.log('New update available', info.version)
  36. break
  37. case 'update-downloaded':
  38. console.log('Update ' + info.version + ' ready to be installed.')
  39. showUpdateUI(info)
  40. break
  41. case 'update-not-available':
  42. console.log('No new update found.')
  43. break
  44. case 'ready':
  45. ipcRenderer.send('autoUpdateAction', 'checkForUpdate')
  46. case 'error':
  47. console.log('Error during update check..')
  48. console.debug('Error Code:', info != null ? info.code : null)
  49. if(err != null && err.code != null){
  50. if(err.code === 'ERR_UPDATER_INVALID_RELEASE_FEED'){
  51. console.log('No suitable releases found.')
  52. }
  53. }
  54. break
  55. default:
  56. console.log('Unknown argument', arg)
  57. break
  58. }
  59. })
  60. ipcRenderer.send('autoUpdateAction', 'initAutoUpdater')
  61. }
  62. function showUpdateUI(info){
  63. //TODO Make this message a bit more informative `${info.version}`
  64. document.getElementById('image_seal_container').setAttribute('update', true)
  65. document.getElementById('image_seal_container').onclick = () => {
  66. setOverlayContent('Update Available', 'A new update for the launcher is available. Would you like to install now?', 'Install', 'Later')
  67. setOverlayHandler(() => {
  68. if(!isDev){
  69. ipcRenderer.send('autoUpdateAction', 'installUpdateNow')
  70. } else {
  71. console.error('Cannot install updates in development environment.')
  72. toggleOverlay(false)
  73. }
  74. })
  75. setDismissHandler(() => {
  76. toggleOverlay(false)
  77. })
  78. toggleOverlay(true, true)
  79. }
  80. }
  81. /* jQuery Example
  82. $(function(){
  83. console.log('UICore Initialized');
  84. })*/
  85. document.addEventListener('readystatechange', function () {
  86. if (document.readyState === 'interactive'){
  87. console.log('UICore Initializing..');
  88. // Bind close button.
  89. Array.from(document.getElementsByClassName('fCb')).map((val) => {
  90. val.addEventListener('click', e => {
  91. const window = remote.getCurrentWindow()
  92. window.close()
  93. })
  94. })
  95. // Bind restore down button.
  96. Array.from(document.getElementsByClassName('fRb')).map((val) => {
  97. val.addEventListener('click', e => {
  98. const window = remote.getCurrentWindow()
  99. if(window.isMaximized()){
  100. window.unmaximize()
  101. } else {
  102. window.maximize()
  103. }
  104. document.activeElement.blur()
  105. })
  106. })
  107. // Bind minimize button.
  108. Array.from(document.getElementsByClassName('fMb')).map((val) => {
  109. val.addEventListener('click', e => {
  110. const window = remote.getCurrentWindow()
  111. window.minimize()
  112. document.activeElement.blur()
  113. })
  114. })
  115. } else if(document.readyState === 'complete'){
  116. //266.01
  117. //170.8
  118. //53.21
  119. // Bind progress bar length to length of bot wrapper
  120. //const targetWidth = document.getElementById("launch_content").getBoundingClientRect().width
  121. //const targetWidth2 = document.getElementById("server_selection").getBoundingClientRect().width
  122. //const targetWidth3 = document.getElementById("launch_button").getBoundingClientRect().width
  123. document.getElementById("launch_details").style.maxWidth = 266.01
  124. document.getElementById("launch_progress").style.width = 170.8
  125. document.getElementById("launch_details_right").style.maxWidth = 170.8
  126. document.getElementById("launch_progress_label").style.width = 53.21
  127. }
  128. }, false)
  129. /**
  130. * Open web links in the user's default browser.
  131. */
  132. $(document).on('click', 'a[href^="http"]', function(event) {
  133. event.preventDefault();
  134. //console.log(os.homedir())
  135. shell.openExternal(this.href)
  136. })
  137. /**
  138. * Opens DevTools window if you hold (ctrl + shift + i).
  139. * This will crash the program if you are using multiple
  140. * DevTools, for example the chrome debugger in VS Code.
  141. */
  142. document.addEventListener('keydown', function (e) {
  143. if(e.key === 'I' && e.ctrlKey && e.shiftKey){
  144. let window = remote.getCurrentWindow()
  145. window.toggleDevTools()
  146. }
  147. })