main.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. const {app, BrowserWindow} = require('electron')
  2. const path = require('path')
  3. function createWindow () {
  4. const mainWindow = new BrowserWindow({
  5. width: 800,
  6. height: 600
  7. })
  8. mainWindow.webContents.session.on('select-serial-port', (event, portList, webContents, callback) => {
  9. event.preventDefault()
  10. if (portList && portList.length > 0) {
  11. callback(portList[0].portId)
  12. } else {
  13. callback('') //Could not find any matching devices
  14. }
  15. })
  16. mainWindow.webContents.session.on('serial-port-added', (event, port) => {
  17. console.log('serial-port-added FIRED WITH', port)
  18. })
  19. mainWindow.webContents.session.on('serial-port-removed', (event, port) => {
  20. console.log('serial-port-removed FIRED WITH', port)
  21. })
  22. mainWindow.webContents.session.setPermissionCheckHandler((webContents, permission, requestingOrigin, details) => {
  23. if (permission === 'serial' && details.securityOrigin === 'file:///') {
  24. return true
  25. }
  26. })
  27. mainWindow.webContents.session.setDevicePermissionHandler((details) => {
  28. if (details.deviceType === 'serial' && details.origin === 'file://') {
  29. return true
  30. }
  31. })
  32. mainWindow.loadFile('index.html')
  33. mainWindow.webContents.openDevTools()
  34. }
  35. app.whenReady().then(() => {
  36. createWindow()
  37. app.on('activate', function () {
  38. if (BrowserWindow.getAllWindows().length === 0) createWindow()
  39. })
  40. })
  41. app.on('window-all-closed', function () {
  42. if (process.platform !== 'darwin') app.quit()
  43. })