您可以在两个位置获得相关视频:在带有网格的视频末尾,以及在视频底部暂停时。我已经找到了一种在最后删除它们的方法,并使底部的那些至少对大多数企业来说是可以忍受的。
1.删除视频末尾的相关视频
IFrame 播放器 API:事件
为避免在视频末尾显示相关视频,我只是停止了视频,使其返回显示缩略图和播放按钮:
var player = new YT.Player('player', {
   height: '390',
   width: '640',
   events: {
      'onStateChange': function(event){
         switch(event.data){
            // Stop the video on ending so recommended videos don't pop up
            case 0:     // ended
               player.stopVideo();
               break;
            case -1:    // unstarted
            case 1:     // playing
            case 2:     // paused
            case 3:     // buffering
            case 5:     // video cued
            default:
               break;
         }
      }
   }
});
您也可以替换player.stopVideo();为您想要运行的任何其他代码。
2.在视频底部制作相关视频只显示您的视频
IFrame Player API:YouTube 嵌入式播放器和播放器参数
rel=0不再避免显示任何相关视频;现在,它仍会显示相关视频,但至少它们只会来自您的频道。这在 2018 年 9 月 25 日左右的某个时间发生了变化(文档)。
通过playerVars在 YT.Player 中设置,我们至少可以让相关视频只显示我们频道的视频。文档不清楚您必须playerVars设置为对象,但您可以像这样设置它:
var player = new YT.Player('player', {
   ...
   playerVars:{
      rel:              0
      modestbranding:   1,       // If you're trying to remove branding I figure this is helpful to mention as well; removes the YouTube logo from the bottom controls of the player
      // color:         'white', // Can't have this and modestbranding active simultaneously (just a note in case you run into this)
   },
   ...
});
2A。从底部删除相关视频的潜在方法
如果我有时间,我可能会更多地研究它,但这里可能有答案:
我们可以轻松地访问 iframe 对象,但我们不能对它做任何事情:IFrame Player API:访问和修改 DOM 节点。似乎因为我们要从 YouTube 编辑 iframe,所以存在安全问题(不管我们实际在做什么)。理想情况下,我可以使用 删除“更多视频”文本player.getIframe().contentWindow.document.querySelector('.ytp-pause-overlay.ytp-scroll-min').remove(),但是当我运行时player.getIframe().contentWindow.document出现错误SecurityError: Permission denied to access property "document" on cross-origin object。
但playerVars也有一个origin值可以让我们访问 iframe 的对象:
var player = new YT.Player('player', {
   ...
   playerVars:{
      origin:           'https://mywebsite.com'
   },
   ...
});
它不适用于 localhost,但这可能是 Chromium 和 Firefox 的事情。也许这在现场网站上是一个合法的选择;当/如果我尝试让你知道我是否成功时,我会更新这篇文章。