willyb321

Untitled

Sep 24th, 2016
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.65 KB | None | 0 0
  1. 'use strict';
  2. const path = require('path');
  3. const os = require('os');
  4. const fs = require('fs.extra');
  5. const electron = require('electron');
  6. const tableify = require('tableify');
  7. const {dialog} = require('electron');
  8. const LineByLineReader = require('line-by-line');
  9.  
  10. const JSONParsed = [];
  11. const logPath = path.join(os.homedir(), 'Saved Games', 'Frontier Developments', 'Elite Dangerous');
  12. const app = electron.app;
  13. let loadFile;
  14. let win;
  15.  
  16. // adds debug features like hotkeys for triggering dev tools and reload
  17. require('electron-debug')();
  18. // prevent window being garbage collected
  19. let mainWindow;
  20. function createMainWindow() {
  21. win = new electron.BrowserWindow({
  22. width: 600,
  23. height: 400
  24. });
  25.  
  26. win.on('closed', onClosed);
  27.  
  28. // menu functions be here
  29. }
  30. function onClosed() {
  31. // dereference the window
  32. // for multiple windows store them in an array
  33. mainWindow = null;
  34. }
  35. function funcLoad() {
  36. loadFile = dialog.showOpenDialog({properties: ['openFile',
  37. {defaultPath: logPath}]});
  38. if (/\.[log]+$/i.test(loadFile) === true) {
  39. fs.writeFile(`${process.resourcesPath}/index2.html`, '', err => {
  40. if (err) {
  41. return console.log(err);
  42. }
  43. });
  44. readLine();
  45. win.loadURL(`file:///${loadFile}`);
  46. } else {
  47. console.log('Please load a .log file.');
  48. }
  49. }
  50. function readLine() {
  51. const loadPls = loadFile;
  52. const lr = new LineByLineReader(loadPls[0]);
  53.  
  54. lr.on('error', err => {
  55. return console.log(err);
  56. });
  57.  
  58. lr.on('line', line => {
  59. const lineParse = JSON.parse(line);
  60. JSONParsed.push(lineParse);
  61. // console.log(JSONParsed);
  62. const html = tableify(lineParse) + '<hr>';
  63. fs.appendFile(`${process.resourcesPath}/index2.html`, html, err => {
  64. if (err) {
  65. return console.log(err);
  66. }
  67. });
  68. });
  69. fs.appendFile(`${process.resourcesPath}/index2.html`, '<script src="https://use.fontawesome.com/a39359b6f9.js"></script><style>body {padding: 0; margin: 0; } body {background-color: #313943; color: #bbc8d8; font-family: \'Lato\'; font-size: 22px; font-weight: 500; line-height: 36px; margin-bottom: 36px; text-align: center; } header {position: absolute; width: 500px; height: 250px; top: 50%; left: 50%; margin-top: -125px; margin-left: -250px; text-align: center; } header h1 {font-size: 60px; font-weight: 100; margin: 0; padding: 0; } #grad {background: -webkit-linear-gradient(left, #5A3F37 , #2C7744); /* For Safari 5.1 to 6.0 */ background: -o-linear-gradient(right, #5A3F37 , #2C7744); /* For Opera 11.1 to 12.0 */ background: -moz-linear-gradient(right,#5A3F37 , #2C7744); /* For Firefox 3.6 to 15 */ background: linear-gradient(to right, #5A3F37 , #2C7744); /* Standard syntax */ } hr {color: red; }</style><link href="https://fonts.googleapis.com/css?family=Lato:400,400italic,700" rel="stylesheet" type="text/css">', err => {
  70. if (err) {
  71. return console.log(err);
  72. }
  73. });
  74. lr.on('end', () => {
  75. console.log('done!');
  76. console.log('The file was saved!');
  77. win.loadURL(`file://${process.resourcesPath}/index2.html`);
  78. // win.loadURL(`file://${process.resourcesPath}/index2.html`);
  79.  
  80. });
  81. return win;
  82. }
  83. function readLineMemory() {
  84. var html = '';
  85. loadFile = dialog.showOpenDialog({properties: ['openFile']})
  86. const lr = new LineByLineReader(loadFile[0]);
  87.  
  88. lr.on('error', err => {
  89. return console.log(err);
  90. });
  91.  
  92. lr.on('line', line => {
  93. const lineParse = JSON.parse(line);
  94. JSONParsed.push(lineParse);
  95. let htmlTabled = tableify(lineParse) + '<hr>';
  96. html = html + htmlTabled;
  97. html = 'data:text/html,' + encodeURIComponent(html);
  98. })
  99. lr.on('end', function () {
  100.  
  101. });
  102. return win.loadURL(html);
  103. }
  104. function funcSave() {
  105. dialog.showSaveDialog(fileName => {
  106. if (fileName === undefined) {
  107. console.log('You didn\'t save the file');
  108. return;
  109. }
  110. // fileName is a string that contains the path and filename created in the save file dialog.
  111. fs.copy(`${process.resourcesPath}/index2.html`, fileName, err => {
  112. if (err) {
  113. console.log(err.message);
  114. }
  115. });
  116. }
  117. );
  118. }
  119. function funcSaveJSON() {
  120. dialog.showSaveDialog(fileName => {
  121. if (fileName === undefined) {
  122. console.log('You didn\'t save the file');
  123. return;
  124. }
  125. const JSONParsedSave = JSON.stringify(JSONParsed);
  126. fs.writeFile(fileName, JSONParsedSave, err => {
  127. if (err) {
  128. console.log(err.message);
  129. }
  130. });
  131. });
  132. }
  133. app.on('window-all-closed', () => {
  134. if (process.platform !== 'darwin') {
  135. app.quit();
  136. }
  137. fs.writeFile(`${process.resourcesPath}/index2.html`, '', err => {
  138. if (err) {
  139. return console.log(err);
  140. }
  141. });
  142. });
  143. app.on('activate', () => {
  144. if (!mainWindow) {
  145. mainWindow = createMainWindow();
  146. }
  147. });
  148.  
  149. app.on('ready', () => {
  150. mainWindow = createMainWindow();
  151. });
  152.  
  153. const {Menu} = require('electron');
  154.  
  155. const template = [
  156. {
  157. label: 'File',
  158. submenu: [
  159. {label: 'Save as HTML', click: funcSave},
  160. {label: 'Save as JSON', click: funcSaveJSON},
  161. {label: 'Load', click: funcLoad},
  162. {label: 'test', click: readLineMemory
  163. }}
  164.  
  165. ]
  166. },
  167. {
  168. label: 'Edit',
  169. submenu: [
  170. {
  171. role: 'selectall'
  172. }
  173. ]
  174. },
  175. {
  176. label: 'View',
  177. submenu: [
  178. {
  179. label: 'Reload',
  180. accelerator: 'CmdOrCtrl+R',
  181. click(item, focusedWindow) {
  182. if (focusedWindow) {
  183. focusedWindow.reload();
  184. }
  185. }
  186. },
  187. {
  188. role: 'togglefullscreen'
  189. }
  190. ]
  191. },
  192. {
  193. role: 'window',
  194. submenu: [
  195. {
  196. role: 'minimize'
  197. },
  198. {
  199. role: 'close'
  200. }
  201. ]
  202. },
  203. {
  204. role: 'help',
  205. submenu: [
  206. {
  207. label: 'Learn More',
  208. click() {
  209. require('electron').shell.openExternal('http://electron.atom.io');
  210. }
  211. }
  212. ]
  213. }
  214. ];
  215.  
  216. if (process.platform === 'darwin') {
  217. const name = require('electron').remote.app.getName();
  218.  
  219. template.unshift({
  220. label: name,
  221. submenu: [
  222. {
  223. role: 'about'
  224. },
  225. {
  226. type: 'separator'
  227. },
  228. {
  229. role: 'services',
  230. submenu: []
  231. },
  232. {
  233. type: 'separator'
  234. },
  235. {
  236. role: 'hide'
  237. },
  238. {
  239. role: 'hideothers'
  240. },
  241. {
  242. role: 'unhide'
  243. },
  244. {
  245. type: 'separator'
  246. },
  247. {
  248. role: 'quit'
  249. }
  250. ]
  251. });
  252. // Edit menu.
  253. template[1].submenu.push(
  254. {
  255. type: 'separator'
  256. },
  257. {
  258. label: 'Speech',
  259. submenu: [
  260. {
  261. role: 'startspeaking'
  262. },
  263. {
  264. role: 'stopspeaking'
  265. }
  266. ]
  267. }
  268. );
  269. // Window menu.
  270. template[3].submenu = [
  271. {
  272. label: 'Close',
  273. accelerator: 'CmdOrCtrl+W',
  274. role: 'close'
  275. },
  276. {
  277. label: 'Minimize',
  278. accelerator: 'CmdOrCtrl+M',
  279. role: 'minimize'
  280. },
  281. {
  282. label: 'Zoom',
  283. role: 'zoom'
  284. },
  285. {
  286. type: 'separator'
  287. },
  288. {
  289. label: 'Bring All to Front',
  290. role: 'front'
  291. }
  292. ];
  293. }
  294.  
  295. const menu = Menu.buildFromTemplate(template);
  296. Menu.setApplicationMenu(menu);
Advertisement
Add Comment
Please, Sign In to add comment