2023-04-05 13:42:20 +00:00
|
|
|
const { app, BrowserWindow } = require('electron')
|
2022-11-22 21:50:32 +00:00
|
|
|
|
|
|
|
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) => {
|
2023-04-05 13:42:20 +00:00
|
|
|
// 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)
|
2023-04-05 13:42:20 +00:00
|
|
|
// Optionally update details.deviceList
|
2022-11-22 21:50:32 +00:00
|
|
|
})
|
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)
|
2023-04-05 13:42:20 +00:00
|
|
|
// Optionally update details.deviceList
|
2022-11-22 21:50:32 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
event.preventDefault()
|
|
|
|
if (details.deviceList && details.deviceList.length > 0) {
|
2023-04-05 13:42:20 +00:00
|
|
|
const deviceToReturn = details.deviceList.find((device) => {
|
2023-05-15 07:58:35 +00:00
|
|
|
return !grantedDeviceThroughPermHandler || (device.deviceId !== grantedDeviceThroughPermHandler.deviceId)
|
2022-11-22 21:50:32 +00:00
|
|
|
})
|
|
|
|
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
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
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
|
|
|
|
2023-05-30 16:35:25 +00:00
|
|
|
mainWindow.webContents.session.setUSBProtectedClassesHandler((details) => {
|
|
|
|
return details.protectedClasses.filter((usbClass) => {
|
|
|
|
// Exclude classes except for audio classes
|
|
|
|
return usbClass.indexOf('audio') === -1
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
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()
|
|
|
|
})
|