在sandbox-iframe中嵌入youtube-iframe失败



当将youtube-iframe嵌入到另一个iframe中时,该iframe是沙盒化的(以防止XSS(,播放器在所有主要浏览器中都保持黑色。

见 https://jsfiddle.net/ms9fwLbk/

<!DOCTYPE html><html lang='en'><head><title>Sandbox iframe youtube problem</title></head><body>
<iframe sandbox='allow-scripts allow-presentation' width='600' height='400' srcdoc='
<!DOCTYPE html><html lang="en"><head><title>iframe</title></head><body>
<!-- original source-code shared from youtube: -->
<iframe width="560" height="315"
src="https://www.youtube.com/embed/QwS1r1mc888?controls=0"
frameborder="0"
allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture"
allowfullscreen></iframe>
</body></html>
'></iframe></body></html>

浏览器的控制台日志显示以下错误,因为 youtube-api 在 CORS 方面存在问题 (允许同源在这里不是选项,因为允许脚本已启用并且会使 cookie 容易受到攻击(

Uncaught DOMException: Failed to read the 'cookie' property from 'Document':
The document is sandboxed and lacks the 'allow-same-origin' flag.
at Vb.m.get (https://www.youtube.com/yts/jsbin/www-embed-player-vflwaq4V_/www-embed-player.js:142:47)
Access to XMLHttpRequest at 'https://www.youtube.com/error_204?...'
from origin 'null' has been blocked by CORS policy:
Response to preflight request doesn't pass access control check:
No 'Access-Control-Allow-Origin' header is present on the requested resource.

济于事

  • 允许沙盒中的更多功能,或在内部 iframe 上定义沙盒
  • 使用YouTube的旧对象嵌入源代码
  • 从YouTube播放器中删除允许参数
  • 使用 youtube-nocookie.com 应用增强隐私模式

如何让YouTube忽略cookie访问或飞行前来源限制?

(Vimeo在这种沙盒iframe中没有任何问题,但有些视频只能在YouTube上找到。

我遇到了同样的问题,必须设置以下沙箱属性才能正常工作

sandbox="allow-scripts allow-same-origin">

更多信息 https://www.w3schools.com/tags/att_iframe_sandbox.asp

最新更新