main.js 866 B

12345678910111213141516171819202122232425262728293031323334353637
  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) : app.requestSingleInstanceLock();
  23. app.on('second-instance', (event, args, workingDirectory, data) => {
  24. setImmediate(() => {
  25. console.log([JSON.stringify(args), JSON.stringify(data)].join('||'));
  26. app.exit(0);
  27. });
  28. });
  29. if (!gotTheLock) {
  30. app.exit(1);
  31. }