Welcome What's new in Chrome extensions API reference Samples
Welcome What's new in Chrome extensions API reference Samples

WebUSB in extensions

Published on

The WebUSB API exposes non-standard Universal Serial Bus (USB) compatible devices to the web. This page describes aspects of the API that are particular to extensions. Refer to MDN for complete details of the WebUSB API.

Availability in extensions

Chrome 118 or later.

Permissions

No manifest file permissions are required; however WebUSB triggers the browser's user permission flow.

Manifest

No manifest keys are needed for this API.

Supporting contexts

This API may be used in almost any context; The WebUSB.requestDevice() method cannot be used in extension service workers. See the next section for details.

Chrome extension differences

Although WebUSB is available to extension service workers, WebUSB.requestDevice(), which returns a promise that resolves with a USBDevice instance, cannot be called in an extension service worker. To get around this, call requestDevice() from an extension page other than the extension service worker and send a message to the extension service worker.

The following code follows a typical pattern by calling requestDevice() as part of a permissions flow requiring a user gesture. When the device is acquired it sends a message to the service worker, which can then retrieve the device using getDevices().

popup.js:

myButton.addEventListener("click", async () => {
await navigator.usb.requestDevice({
filters: [{ vendorId: 0x1234, productId: 0x5678 }],
});
chrome.runtime.sendMessage("newDevice");
});

service-worker.js

chrome.runtime.onMessage.addListener(async (message) => {
if (message === "newDevice") {
const devices = await navigator.usb.getDevices();
for (const device of devices) {
// open device connection.
await device.open();
}
}
});

Published on Improve article

This site uses cookies to deliver and enhance the quality of its services and to analyze traffic. If you agree, cookies are also used to serve advertising and to personalize the content and advertisements that you see. Learn more about our use of cookies.