uicore.js 5.9 KB

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