In a given browser environment, the supported permissions will likely differ from the registry spec.
At the time of my writing this question, the spec list includes the following names: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
The environment support might be not only a subset of the spec list, but also additional permissions not listed in the spec. For example, Chrome 83 supports periodic-background-sync
. (I've included a code snippet to demonstrate.)
How can I get an enumerated list of the names of the supported permissions in the current environment? How can I programmatically know what's possible?
// ✅ = 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);