Tip of the day - Javascript

Un trucchetto al giorno toglie le complicazioni di torno!

Guarda tutti i nostri piccoli consigli per mantenerti allenato o conoscere qualcosa di nuovo sul magico mondo JS.

Vedi gli altri tips

How to request and detect the USB Devices (Experimental)

2021-06-10 - JavaScript

1// JS Tip of the Day
2// How to request and detect the USB Devices (Experimental)
3
4// Request a Device
5const filters = {filters: []}; // You could filter by vendorId/productId
6
7await navigator.usb.requestDevice(filters)
8.then(device => console.log(`Device: ${device.productName}`))
9.catch(e => console.log(`No Device: ${e}`));
10
11// Get all a paired attached devices
12await navigator.usb.getDevices()
13.then(devices => {
14  console.log(`Devices: ${devices.length}`);
15  devices.forEach(device => console.log(`Device: ${device.productName}`));
16});
17
18// NB: Yes...I used top level await! :D
#JStipoftheday