问题描述
当 Google Colab 为代码单元创建 IFrame 时,是否可以添加对“usb”策略的支持,例如:
import IPython
from google.colab import output
IPython.core.display.display(IPython.display.HTML('''
<button id="button">test</button>
<script>
document.querySelector('#button').onclick = async () => {
device = await navigator.usb.requestDevice({ filters: [{
vendorId: 0xABCD,classCode: 0xFF,// vendor-specific
protocolCode: 0x01
}]});
};
</script>
'''))
使用以下内容创建 Iframe:
<iframe allow="accelerometer; autoplay; camera; gyroscope; magnetometer; microphone; serial; xr-spatial-tracking" sandBox="allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-popups-to-escape-sandBox" src="https://ro7dmmbz8ec-496ff2e9c6d22116-0-colab.googleusercontent.com/outputframe.html?vrz=colab-20210722-060124-RC00_386208851" class="" style="height: 40px;"></iframe>
我需要“usb”;添加到“允许”属性列表中。由于我目前收到以下错误:
VM10:3 未捕获(承诺)DOMException:无法在“USB”上执行“requestDevice”:权限策略不允许访问功能“usb”。 在 HTMLButtonElement.document.querySelector.onclick (:3:34)
解决方法
看起来 Google Colaboratory 是基于 Jupyter 项目的。我发现一个 open issue 与添加必要的 allow
属性以在笔记本内的 iframe 中启用 Geolocation API 相关。启用 WebUSB 等 API 需要相同的属性。我建议在项目的 issue tracker 上提出一个专门要求 WebUSB 支持的问题。