最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to set feature policy headers in JS - Stack Overflow

programmeradmin1浏览0评论

I am getting the following errors in my console on my site:

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'.

I am a front end web developer so I'm trying to set this in JS as described here without messing with my server config.

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

results in:

["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"]

Also

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

results in:

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

I am getting the following errors in my console on my site:

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'.

I am a front end web developer so I'm trying to set this in JS as described here https://developers.google.com/web/updates/2018/06/feature-policy#js without messing with my server config.

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

results in:

["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"]

Also

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

results in:

Invalid origin url for feature 'unsized-media': self.
Share Improve this question edited Jul 25, 2020 at 23:11 halfer 20.4k19 gold badges108 silver badges201 bronze badges asked Jul 23, 2020 at 9:18 Paddy HallihanPaddy Hallihan 1,6865 gold badges33 silver badges89 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 13

You can't.

Feature policies are set by the server (via the Feature-Policy or Permissions-Policy headers) and control what JavaScript is allowed to do.

It would be pointless if the JavaScript could grant itself permission to do anything it liked.

发布评论

评论列表(0)

  1. 暂无评论