3 回答

TA貢獻1817條經驗 獲得超14個贊
對于這兩個 API,如更新中所述,需要用戶手勢才能調用requestDevice()
orrequestPort()
方法。無法自動彈出此提示。(如果存在錯誤,請告知 Chrome 團隊,以便我們修復它。)
通過 WebUSB API 和 Web Serial API 授予站點的權限目前是單獨跟蹤的,因此通過其中一個 API 訪問設備的權限不會自動轉換為另一個權限。
目前還沒有一種方法可以通過編程忘記設備權限。這將需要navigator.permissions.revoke()方法,該方法已被放棄。不過,您可以在訪問網站時單擊地址欄中的“鎖定”圖標或轉至 chrome://settings/content/usbDevices(適用于 USB 設備)和 chrome://settings/,手動撤銷訪問設備的權限。 content/serialPorts(用于串行端口)。

TA貢獻1827條經驗 獲得超4個贊
要讓 Chrome“忘記”之前通過 navigator.usb.requestDevice API 配對的 WebUSB 設備:
打開與您想要忘記的設備配對的頁面
單擊地址欄中的圖標
單擊設備旁邊的 x。如果未列出任何內容,則表明該網頁沒有配對的設備。

TA貢獻1998條經驗 獲得超6個贊
新代碼不起作用。這似乎是因為 Chrome 已經通過舊代碼與該端口配對?!靶麓a”不可能起作用,因為正如 Kalido 的評論中指出的那樣,SerialAPI(由于其強大功能)需要用戶手勢才能連接。
我用來實際連接和獲取數據的代碼基本上是由OP中上述鏈接的幾個部分構建的:
navigator.serial.addEventListener('connect', e => {
// Add |e.target| to the UI or automatically connect.
console.log("connected");
});
navigator.serial.addEventListener('disconnect', e => {
// Remove |e.target| from the UI. If the device was open the disconnection can
// also be observed as a stream error.
console.log("disconnected");
});
console.log(navigator.serial);
document.addEventListener('DOMContentLoaded', async () => {
const connectButton = document.querySelector('#connect') as HTMLInputElement;
if (connectButton) {
connectButton.addEventListener('click', async () => {
try {
// Request Keiser Receiver from the user.
const port = await navigator.serial.requestPort({
filters: [{ usbVendorId: 0x2341, usbProductId: not_required }]
});
try {
// Open and begin reading.
await port.open({ baudRate: 115200 });
} catch (e) {
console.log(e);
}
while (port.readable) {
const reader = port.readable.getReader();
try {
while (true) {
const { value, done } = await reader.read();
if (done) {
// Allow the serial port to be closed later.
reader.releaseLock();
break;
}
if (value) {
console.log(value);
}
}
} catch (error) {
// TODO: Handle non-fatal read error.
console.log(error);
}
}
} catch (e) {
console.log("Permission to access a device was denied implicitly or explicitly by the user.");
console.log(e);
console.log(port);
}
}
}
設備特定的供應商和產品 ID 顯然會因設備而異。在上面的示例中,我插入了 Arduino 供應商 ID。
它沒有回答如何讓 Chrome“忘記”的問題,但我不確定這在使用 SerialAPI 時是否相關,因為需要手勢。
希望有更多經驗的人能夠發布更豐富的答案。
添加回答
舉報