Chrome 126 于近期发布了稳定版本,其中一个比较有意思的更新是给 HTML 带来一个新的元素:
,它将从这个版本开始试用,并且正在努力走向标准化。
今天我们一起来看下这个
元素的用法。
Web 权限提示的问题
当 Web 应用程序需要访问浏览器的高级功能时,需要向用户主动请求许可。例如,当百度地图使用
Geolocation API
获取用户的地理位置时,浏览器会提示用户申请权限,这是权限规范中定义明确的概念。
申请权限的触发方式一般分为两类,被动隐式触发,或者主动显示触发:
例如,
Geolocation API
是一个强大的
API
,它的使用依赖于首次使用时隐式询问的方法。例如,当程序调用
navigator.geolocation.getCurrentPosition()
方法时,权限提示框会在第一次调用时自动弹出,还有另外一个例子是
navigator.mediaDevices.getUserMedia()
。
一些其他的
API
,如
Notification API
或
Device Orientation API
,通常有一种显式的方式通过静态方法来请求权限,如
Notification.requestPermission()
或
DeviceMotionEvent.requestPermission()
。
functionshowCameraWarning() { // Show warning that the app isn't fully usable // unless the camera permission is granted. }
const permissionStatus = await navigator.permissions.query({name: "camera"}); permissionStatus.addEventListener('change', () => { // Run the check when the status changes. if (permissionStatus.state === "granted") { useCamera(); } // Run the initial check. if