简体   繁体   English

Chrome,允许自动连接 HID 设备

[英]Chrome, allow autoconnect for HID device

So I'm trying to read out a USB-scale thats connected to my pc.所以我正在尝试读取连接到我的电脑的 USB 秤。 I use chrome's experimental HID api.我使用 chrome 的实验性 HID api。

I use Tampermonekey as userscript injector to extend a website's functionality.我使用 Tampermonekey 作为用户脚本注入器来扩展网站的功能。

The script I inject looks like this:我注入的脚本如下所示:

navigator.hid.requestDevice({ filters: [{ vendorId: 0x0922, productId: 0x8003}] }).then((devices) => {   
    if (devices.length == 0) return;
    devices[0].open().then(() => {
        if(disconnected) {
            disconnected = false
        }
        console.log("Opened device: " + devices[0].productName);
        devices[0].addEventListener("inputreport", handleInputReport);
        devices[0].sendReport(outputReportId, outputReport).then(() => {
            console.log("Sent output report " + outputReportId);
        });
    });
});

When I run it just like this(inline) I get the message in chrome:当我像这样(内联)运行它时,我在 chrome 中收到消息:

DOMException: Failed to execute 'requestDevice' on 'HID': Must be handling a user gesture to show a permission request. DOMException:无法在“HID”上执行“requestDevice”:必须处理用户手势以显示权限请求。

Basically, the code needs to be inside an event listener and the listener needs to be triggered by user input to run.基本上,代码需要在事件侦听器中,并且侦听器需要由用户输入触发才能运行。

Al fine and dandy, except that this has to be initialized hundreds of times a day. Al fine and dandy, 除了这必须每天初始化数百次。 I tried running this code in edge and here it just works without user input.我尝试在 edge 中运行此代码,在这里它无需用户输入即可运行。

Is there a way I can disable this security feature(completely or only for the site im using it on) in chrome?有没有办法可以在 chrome 中禁用此安全功能(完全或仅针对使用它的网站)? I know edge is based on chromium so I expect it to be possible, but am unable to find how/where我知道边缘是基于铬的,所以我希望它是可能的,但我无法找到如何/在哪里

You can useHID.getDevices() to retrieve an HID device that the user has already granted access to.您可以使用HID.getDevices()检索用户已授予访问权限的 HID 设备。

My suggestion would be to check for the device you want with getDevices first.我的建议是首先使用 getDevices 检查您想要的设备。 If you can't find the device, then make something the user can interact with that will allow you to use requestDevice to connect to the device.如果您找不到该设备,则制作一些用户可以与之交互的东西,这样您就可以使用 requestDevice 连接到该设备。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM