'use strict'; const TIMEOUT_CHECK = 500 ; // With 250 it seems that it produces a memory leak and slowdown in some versions, reason unknown const usbDevices = { filters: [ {'vendorId': 1155, 'productId': 57105}, // STM Device in DFU Mode || Digital Radio in USB mode {'vendorId': 10473, 'productId': 393}, // GD32 DFU Bootloader ] }; const PortHandler = new function () { this.initialPorts = false; this.port_detected_callbacks = []; this.port_removed_callbacks = []; this.dfu_available = false; this.port_available = false; this.showAllSerialDevices = false; this.showVirtualMode = false; }; PortHandler.initialize = function () { const portPickerElementSelector = "div#port-picker #port"; this.portPickerElement = $(portPickerElementSelector); this.selectList = document.querySelector(portPickerElementSelector); this.initialWidth = this.selectList.offsetWidth + 12; this.showVirtualMode = ConfigStorage.get('showVirtualMode').showVirtualMode; this.showAllSerialDevices = ConfigStorage.get('showAllSerialDevices').showAllSerialDevices; // fill dropdown with version numbers generateVirtualApiVersions(); // start listening, check after TIMEOUT_CHECK ms this.check(); }; PortHandler.check = function () { const self = this; if (!self.port_available) { self.check_usb_devices(); } if (!self.dfu_available) { self.check_serial_devices(); } setTimeout(function () { self.check(); }, TIMEOUT_CHECK); }; PortHandler.check_serial_devices = function () { const self = this; serial.getDevices(function(currentPorts) { // auto-select port (only during initialization) if (!self.initialPorts) { currentPorts = self.updatePortSelect(currentPorts); self.selectPort(currentPorts); self.initialPorts = currentPorts; } else { self.removePort(currentPorts); self.detectPort(currentPorts); } }); }; PortHandler.check_usb_devices = function (callback) { const self = this; chrome.usb.getDevices(usbDevices, function (result) { const dfuElement = self.portPickerElement.children("[value='DFU']"); if (result.length) { if (!dfuElement.length) { self.portPickerElement.empty(); let usbText; if (result[0].productName) { usbText = (`DFU - ${result[0].productName}`); } else { usbText = "DFU"; } self.portPickerElement.append($('