main.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. const { app, BrowserWindow } = require('electron')
  2. function createWindow () {
  3. const mainWindow = new BrowserWindow({
  4. width: 800,
  5. height: 600
  6. })
  7. mainWindow.webContents.session.on('select-serial-port', (event, portList, webContents, callback) => {
  8. // Add listeners to handle ports being added or removed before the callback for `select-serial-port`
  9. // is called.
  10. mainWindow.webContents.session.on('serial-port-added', (event, port) => {
  11. console.log('serial-port-added FIRED WITH', port)
  12. // Optionally update portList to add the new port
  13. })
  14. mainWindow.webContents.session.on('serial-port-removed', (event, port) => {
  15. console.log('serial-port-removed FIRED WITH', port)
  16. // Optionally update portList to remove the port
  17. })
  18. event.preventDefault()
  19. if (portList && portList.length > 0) {
  20. callback(portList[0].portId)
  21. } else {
  22. // eslint-disable-next-line n/no-callback-literal
  23. callback('') // Could not find any matching devices
  24. }
  25. })
  26. mainWindow.webContents.session.setPermissionCheckHandler((webContents, permission, requestingOrigin, details) => {
  27. if (permission === 'serial' && details.securityOrigin === 'file:///') {
  28. return true
  29. }
  30. return false
  31. })
  32. mainWindow.webContents.session.setDevicePermissionHandler((details) => {
  33. if (details.deviceType === 'serial' && details.origin === 'file://') {
  34. return true
  35. }
  36. return false
  37. })
  38. mainWindow.loadFile('index.html')
  39. mainWindow.webContents.openDevTools()
  40. }
  41. app.whenReady().then(() => {
  42. createWindow()
  43. app.on('activate', function () {
  44. if (BrowserWindow.getAllWindows().length === 0) createWindow()
  45. })
  46. })
  47. app.on('window-all-closed', function () {
  48. if (process.platform !== 'darwin') app.quit()
  49. })