mistUI.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /**
  2. @module preloader MistUI
  3. */
  4. require('babel-register');
  5. // Initialise the Redux store
  6. window.store = require('./rendererStore');
  7. require('./include/common')('mist');
  8. require('./include/web3CurrentProvider.js');
  9. const { ipcRenderer, remote, webFrame } = require('electron'); // eslint-disable-line import/newline-after-import
  10. const { Menu, MenuItem } = remote;
  11. const dbSync = require('../dbSync.js');
  12. const i18n = require('../i18n.js');
  13. const mist = require('./include/mistAPI.js');
  14. require('./include/setBasePath')('interface');
  15. window.mist = mist();
  16. window.mistMode = remote.getGlobal('mode');
  17. window.dbSync = dbSync;
  18. window.dirname = remote.getGlobal('dirname');
  19. window.ipc = ipcRenderer;
  20. window.i18n = require('../i18n.js');
  21. // remove require and module, because node-integration is on
  22. delete window.module;
  23. delete window.require;
  24. // prevent overwriting the Dapps Web3
  25. // delete global.Web3;
  26. // delete window.Web3;
  27. // set the langauge for the electron interface
  28. // ipcRenderer.send('setLanguage', navigator.language.substr(0,2));
  29. // A message coming from other window, to be passed to a webview
  30. ipcRenderer.on('uiAction_windowMessage', (e, type, id, error, value) => {
  31. if (type === 'requestAccount' || (type === 'connectAccount' && !error)) {
  32. Tabs.update(
  33. { webviewId: id },
  34. { $addToSet: { 'permissions.accounts': value } }
  35. );
  36. }
  37. // forward to the webview (TODO: remove and manage in the ipcCommunicator?)
  38. const tab = Tabs.findOne({ webviewId: id });
  39. if (tab) {
  40. webview = $(`webview[data-id=${tab._id}]`)[0];
  41. if (webview) {
  42. webview.send('uiAction_windowMessage', type, error, value);
  43. }
  44. }
  45. });
  46. ipcRenderer.on('uiAction_enableBlurOverlay', (e, value) => {
  47. $('html').toggleClass('has-blur-overlay', !!value);
  48. });
  49. // Wait for webview toggle
  50. ipcRenderer.on('uiAction_toggleWebviewDevTool', (e, id) => {
  51. const webview = Helpers.getWebview(id);
  52. if (!webview) {
  53. return;
  54. }
  55. if (webview.isDevToolsOpened()) {
  56. webview.closeDevTools();
  57. } else {
  58. webview.openDevTools();
  59. }
  60. });
  61. // randomize accounts and drop half
  62. // also certainly remove the web3.ethbase one
  63. const randomizeAccounts = (acc, coinbase) => {
  64. let accounts = _.shuffle(acc);
  65. accounts = _.rest(accounts, (accounts.length / 2).toFixed(0));
  66. accounts = _.without(accounts, coinbase);
  67. return accounts;
  68. };
  69. // Run tests
  70. ipcRenderer.on('uiAction_runTests', (e, type) => {
  71. if (type === 'webview') {
  72. web3.eth.getAccounts((error, accounts) => {
  73. if (error) return;
  74. web3.eth.getCoinbase((coinbaseError, coinbase) => {
  75. if (coinbaseError) return;
  76. Tabs.upsert('tests', {
  77. position: -1,
  78. name: 'Tests',
  79. url: '', // is hardcoded in webview.html to prevent hijacking
  80. permissions: {
  81. accounts: randomizeAccounts(accounts, coinbase)
  82. }
  83. });
  84. Tracker.afterFlush(() => {
  85. LocalStore.set('selectedTab', 'tests');
  86. });
  87. // update the permissions, when accounts change
  88. Tracker.autorun(() => {
  89. const accountList = _.pluck(
  90. EthAccounts.find({}, { fields: { address: 1 } }).fetch(),
  91. 'address'
  92. );
  93. Tabs.update('tests', {
  94. $set: {
  95. 'permissions.accounts': randomizeAccounts(accountList, coinbase)
  96. }
  97. });
  98. });
  99. });
  100. });
  101. }
  102. });
  103. // CONTEXT MENU
  104. const currentMousePosition = { x: 0, y: 0 };
  105. const menu = new Menu();
  106. // menu.append(new MenuItem({ type: 'separator' }));
  107. menu.append(
  108. new MenuItem({
  109. label: i18n.t('mist.rightClick.reload'),
  110. accelerator: 'Command+R',
  111. click() {
  112. const webview = Helpers.getWebview(LocalStore.get('selectedTab'));
  113. if (LocalStore.get('selectedTab') === 'wallet') {
  114. return console.log('Cannot refresh the wallet');
  115. }
  116. if (webview) {
  117. webview.reloadIgnoringCache();
  118. }
  119. }
  120. })
  121. );
  122. menu.append(
  123. new MenuItem({
  124. label: i18n.t('mist.rightClick.openDevTools'),
  125. click() {
  126. const webview = Helpers.getWebview(LocalStore.get('selectedTab'));
  127. if (webview) {
  128. webview.openDevTools();
  129. }
  130. }
  131. })
  132. );
  133. menu.append(
  134. new MenuItem({
  135. label: i18n.t('mist.rightClick.inspectElements'),
  136. click() {
  137. const webview = Helpers.getWebview(LocalStore.get('selectedTab'));
  138. if (webview) {
  139. webview.inspectElement(currentMousePosition.x, currentMousePosition.y);
  140. }
  141. }
  142. })
  143. );
  144. window.addEventListener(
  145. 'contextmenu',
  146. e => {
  147. e.preventDefault();
  148. // OPEN CONTEXT MENU over webviews
  149. if ($('webview:hover')[0]) {
  150. currentMousePosition.x = e.layerX;
  151. currentMousePosition.y = e.layerY;
  152. menu.popup(remote.getCurrentWindow());
  153. }
  154. },
  155. false
  156. );
  157. document.addEventListener(
  158. 'keydown',
  159. e => {
  160. // RELOAD current webview, unless on wallet tab
  161. if (
  162. LocalStore.get('selectedTab') !== 'wallet' &&
  163. e.metaKey &&
  164. e.keyCode === 82
  165. ) {
  166. const webview = Helpers.getWebview(LocalStore.get('selectedTab'));
  167. if (webview) {
  168. webview.reloadIgnoringCache();
  169. }
  170. }
  171. },
  172. false
  173. );