FeaturePolicy: allowsFeature() 方法

實驗性: 這是一項實驗性技術
在生產中使用此技術之前,請仔細檢查瀏覽器相容性表格

FeaturePolicy 介面的 allowsFeature() 方法允許對許可權策略 (Permissions Policy) 的各個指令進行內省。如果指定的上下文(或者未指定上下文時的預設上下文)允許使用指定的功能,則返回 true,否則返回 false

語法

js
allowsFeature(feature)
allowsFeature(feature, origin)

引數

feature

用於檢查其可用性的特定功能名稱。

origin 可選

用於檢查其可用性的特定源名稱。如果未指定,則使用預設源。

返回值

一個布林值 (Boolean),當且僅當允許使用該功能時為 true

示例

以下示例查詢文件是否可以透過許可權策略使用攝像頭 API。請注意,如果使用者尚未授予相應的許可權,攝像頭 API 可能會受到許可權 API 的限制。

js
// First, get the Feature Policy object
const featurePolicy = document.featurePolicy;

// Then query feature for specific
const allowed = featurePolicy.allowsFeature("camera");

if (allowed) {
  console.log("FP allows camera.");
} else {
  console.log("FP does not allows camera.");
}

規範

此特性似乎未在任何規範中定義。

瀏覽器相容性