我需要通过 Dart 添加带有 YouTube 嵌入源的 iframe,但它会引发异常:
void main() {
String videoID = "1nXRkk7Szu4";
IFrameElement frame = new IFrameElement();
frame.width = "560";
frame.height = "315";
frame.src = "http://www.youtube.com/embed/$videoID";
frame.setAttribute("allowfullscreen", "true");
frame.setAttribute("frameborder", "0");
querySelector("#sample_container_id").children.add(frame);
}
打破异常:SecurityError: Blocked a frame with origin " http://www.youtube.com " 访问跨域框架。
通过 JavaScript 就可以了。
脚本已更新。
更新:我找到了方法。我添加了带有这些值的沙盒属性:“allow-same-origin allow-scripts”+ 我在 src 中打开了带有后缀“?rel=0&html5=1”的 HTML5。