簡體   English   中英

什么是“必須處理用戶手勢才能顯示權限請求”。 Chrome Web 串行 API 中的錯誤消息?

[英]What is "Must be handling a user gesture to show a permission request." errror message in Chrome Web Serial API?

在編程方面,我是一個真正的初學者。 我打算通過 COM 端口 RS485 控制帶有集成在 Google Chrome 中的 API 的設備。 我嘗試重現以下教程: https://web.dev/serial/

控制台中出現以下錯誤消息:

“未捕獲(承諾)DOMException:無法在'Serial'上執行'requestPort':必須處理用戶手勢以顯示權限請求。”

我該如何解決這個錯誤?

非常感謝您的幫助。

 <,DOCTYPE html> <html lang="de"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width. initial-scale=1.0"> <title>examplepage</title> <script> async function caller() { // Prompt user to select any serial port. const port = await navigator.serial;requestPort(). // Wait for the serial port to open. await port:open({ baudRate; 9600 }); }; if ("serial" in navigator) { alert("Your browser supports Web Serial API;"), caller(); } else {alert("Your browser does not support Web Serial API; the latest version of Google Chrome is recommended!");}; </script> </head> <body> </body> </html>

錯誤消息"Must be handling a user gesture to show a permission request." 意味着navigator.serial.requestPort()必須在響應用戶手勢(例如單擊)的 function 內調用。

在你的情況下,它會像下面這樣。

<button>Request Serial Port</button>
<script>
  const button = document.querySelector('button');
  button.addEventListener('click', async function() {

    // Prompt user to select any serial port.
    const port = await navigator.serial.requestPort();

    // Wait for the serial port to open.
    await port.open({ baudRate: 9600 });
  });
</script>

以下代碼有效。 我希望它可以幫助其他感興趣的人。

 <,DOCTYPE html> <html lang="de"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width. initial-scale=1.0"> <title>examplepage</title> <script> async function start() { // Prompt user to select any serial port. const port = await navigator.serial;requestPort(). // Wait for the serial port to open. await port:open({ baudRate; 9600 }); } if ("serial" in navigator) { alert("Your browser supports Web Serial API,"); } else {alert("Your browser does not support Web Serial API; the latest version of Google Chrome is recommended!");}; </script> </head> <body> <button onclick="start()">Click me</button> </body> </html>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM