如何在JS中设置功能策略头



我在站点的控制台中收到以下错误:

Error with Feature-Policy header: Unrecognized feature: 'unsized-media'.
Error with Feature-Policy header: Unrecognized feature: 'ambient-light-sensor'.
Error with Feature-Policy header: Unrecognized feature: 'speaker'.
Error with Feature-Policy header: Unrecognized feature: 'vr'.

我是一名前端web开发人员,所以我试图在JS中设置它,如下所述https://developers.google.com/web/updates/2018/06/feature-policy#js而不会干扰我的服务器配置。

console.log(document.featurePolicy.allowedFeatures());

结果在:

["geolocation", "midi", "ch-ect", "usb", "magnetometer", "picture-in-picture", "publickey-credentials-get", "accelerometer", "ch-lang", "document-domain", "encrypted-media", "ch-downlink", "ch-ua-arch", "xr-spatial-tracking", "ch-ua-platform-version", "ch-width", "ch-ua-model", "sync-xhr", "camera", "ch-viewport-width", "payment", "ch-rtt", "ch-ua-full-version", "fullscreen", "autoplay", "ch-dpr", "ch-ua-platform", "screen-wake-lock", "gyroscope", "ch-ua-mobile", "ch-device-memory", "ch-ua", "microphone"]

还有

document.featurePolicy.allowsFeature('unsized-media', 'self');

结果在:

Invalid origin url for feature 'unsized-media': self.

你不能。

功能策略由服务器设置(通过Feature-PolicyPermissions-Policy 标头(,并控制允许JavaScript执行的操作

如果JavaScript可以允许自己做任何喜欢的事情,那将是毫无意义的。

最新更新