gpt4 book ai didi

javascript - 如何在 navigator.permissions 中枚举支持的权限名称?

转载 作者:行者123 更新时间:2023-12-01 16:25:20 25 4
gpt4 key购买 nike

在给定的浏览器环境中,支持的 permissions可能与 the registry spec 不同.

在我写这个问题的时候,规范列表包括以下名称:

  • accelerometer
  • ambient-light-sensor
  • background-fetch
  • background-sync
  • bluetooth
  • camera
  • clipboard-read
  • clipboard-write
  • device-info
  • display-capture
  • geolocation
  • gyroscope
  • magnetometer
  • microphone
  • midi
  • nfc
  • notifications
  • persistent-storage
  • push
  • speaker

  • 环境支持可能不仅是规范列表的子集,还可能是规范中未列出的其他权限。例如,Chrome 83 支持 periodic-background-sync . (我已经包含了一个代码片段来演示。)
    如何获取当前环境中支持的权限名称的枚举列表?我怎么能 以编程方式 知道什么是可能的吗?

    // ✅ = supported
    // ❌ = not supported

    const getPermissionStatus = name => navigator.permissions.query({name});

    const logSupportedPermissions = async (...names) => {
    for (const name of names) {
    try {
    const status = await getPermissionStatus(name);
    console.log(`✅ ${name} (${status.state})`)
    }
    catch (err) {
    console.log(`❌ ${name}`)
    }
    }
    };

    const names = [
    'clipboard-write', // listed, works in Chrome 83
    'periodic-background-sync', // not listed, but works in Chrome 83
    'display-capture', // listed, but throws in Chrome 83
    'a-nonexistent-permission-name', // not listed, throws
    ];

    logSupportedPermissions(...names);

    最佳答案

    从浏览器来源获取

  • chromium
  • firefox
  • firefox second
  • Opera
  • 关于javascript - 如何在 navigator.permissions 中枚举支持的权限名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62706697/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com