如何使用 javascript 选择嵌入的视频?

我正在使用的网站嵌入了 youtube 视频,像这样

<iframe src="https://www.youtube.com/embed/spBaumdWlgQ?feature=oembed">

我无权访问代码,只能在控制台中执行任何操作。

最终,我想将播放速率更改为 youtube UI 允许的范围之外。但我无法选择视频

var video = document.getElementsByTagName('video')[0]

视频不存在!

(如果我检查视频,它最终对 document 可见,但这不是重点。

我最终想这样做

video.playbackRate = 3
stack overflow How to select an embeded video using javascript?
原文答案

答案:

作者头像

First you need to select the iframe, then select the element inside iframe.

This may work:

var iframe = document.getElementById("myFrame");
var elmnt = iframe.contentWindow.document.getElementsByTagName("H1")[0]
作者头像

To access any element inside iframe you need to access contentWindow property of iframe. Which in turn returns the window object. Use this window object to access anything inside an iframe.

var iframe = document.querySelector('iframe'),video;
     if (iframe) {
         video = iframe.contentWindow.document.getElementsByTagName('video');
     }
  
      if (video) {
           video.playbackRate = 3
      }

But one thing, since this is youtube video's iframe, you will face the CORS issue. And so you won't be able to manipulate iframe data *

Refer here to know more about the CORS issue in an iframe