main.js 868 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. const { app } = require('electron');
  2. // Send data from the second instance to the first instance.
  3. const sendAdditionalData = app.commandLine.hasSwitch('send-data');
  4. app.whenReady().then(() => {
  5. console.log('started'); // ping parent
  6. });
  7. let obj = {
  8. level: 1,
  9. testkey: 'testvalue1',
  10. inner: {
  11. level: 2,
  12. testkey: 'testvalue2'
  13. }
  14. };
  15. if (app.commandLine.hasSwitch('data-content')) {
  16. obj = JSON.parse(app.commandLine.getSwitchValue('data-content'));
  17. if (obj === 'undefined') {
  18. obj = undefined;
  19. }
  20. }
  21. const gotTheLock = sendAdditionalData
  22. ? app.requestSingleInstanceLock(obj)
  23. : app.requestSingleInstanceLock();
  24. app.on('second-instance', (event, args, workingDirectory, data) => {
  25. setImmediate(() => {
  26. console.log([JSON.stringify(args), JSON.stringify(data)].join('||'));
  27. app.exit(0);
  28. });
  29. });
  30. if (!gotTheLock) {
  31. app.exit(1);
  32. }