2022-11-22 21:50:32 +00:00
|
|
|
const {app, BrowserWindow} = require('electron')
|
|
|
|
const e = require('express')
|
|
|
|
const path = require('path')
|
|
|
|
|
|
|
|
function createWindow () {
|
|
|
|
const mainWindow = new BrowserWindow({
|
|
|
|
width: 800,
|
|
|
|
height: 600
|
|
|
|
})
|
2023-02-01 11:59:16 +00:00
|
|
|
|
2022-11-22 21:50:32 +00:00
|
|
|
let grantedDeviceThroughPermHandler
|
|
|
|
|
|
|
|
mainWindow.webContents.session.on('select-usb-device', (event, details, callback) => {
|
|
|
|
//Add events to handle devices being added or removed before the callback on
|
|
|
|
//`select-usb-device` is called.
|
2023-02-01 11:59:16 +00:00
|
|
|
mainWindow.webContents.session.on('usb-device-added', (event, device) => {
|
2022-11-22 21:50:32 +00:00
|
|
|
console.log('usb-device-added FIRED WITH', device)
|
|
|
|
//Optionally update details.deviceList
|
|
|
|
})
|
2023-02-01 11:59:16 +00:00
|
|
|
|
2022-11-22 21:50:32 +00:00
|
|
|
mainWindow.webContents.session.on('usb-device-removed', (event, device) => {
|
|
|
|
console.log('usb-device-removed FIRED WITH', device)
|
|
|
|
//Optionally update details.deviceList
|
|
|
|
})
|
|
|
|
|
|
|
|
event.preventDefault()
|
|
|
|
if (details.deviceList && details.deviceList.length > 0) {
|
|
|
|
const deviceToReturn = details.deviceList.find((device) => {
|
|
|
|
if (!grantedDeviceThroughPermHandler || (device.deviceId != grantedDeviceThroughPermHandler.deviceId)) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
if (deviceToReturn) {
|
2023-02-01 11:59:16 +00:00
|
|
|
callback(deviceToReturn.deviceId)
|
2022-11-22 21:50:32 +00:00
|
|
|
} else {
|
|
|
|
callback()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
mainWindow.webContents.session.setPermissionCheckHandler((webContents, permission, requestingOrigin, details) => {
|
|
|
|
if (permission === 'usb' && details.securityOrigin === 'file:///') {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2023-02-01 11:59:16 +00:00
|
|
|
|
2022-11-22 21:50:32 +00:00
|
|
|
mainWindow.webContents.session.setDevicePermissionHandler((details) => {
|
|
|
|
if (details.deviceType === 'usb' && details.origin === 'file://') {
|
2023-02-01 11:59:16 +00:00
|
|
|
if (!grantedDeviceThroughPermHandler) {
|
2022-11-22 21:50:32 +00:00
|
|
|
grantedDeviceThroughPermHandler = details.device
|
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2023-02-01 11:59:16 +00:00
|
|
|
|
2022-11-22 21:50:32 +00:00
|
|
|
mainWindow.loadFile('index.html')
|
|
|
|
}
|
|
|
|
|
|
|
|
app.whenReady().then(() => {
|
|
|
|
createWindow()
|
2023-02-01 11:59:16 +00:00
|
|
|
|
2022-11-22 21:50:32 +00:00
|
|
|
app.on('activate', function () {
|
|
|
|
if (BrowserWindow.getAllWindows().length === 0) createWindow()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
app.on('window-all-closed', function () {
|
|
|
|
if (process.platform !== 'darwin') app.quit()
|
|
|
|
})
|