您可以在两个位置获得相关视频:在带有网格的视频末尾,以及在视频底部暂停时。我已经找到了一种方法,可以在最后删除它们,并使底部的那些至少对大多数企业来说是可以忍受的。
1。删除视频末尾的相关视频
IFrame Player API: Events
为避免在视频末尾显示相关视频,我刚刚停止了视频,使其返回显示缩略图和播放按钮:
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 Embedded Players and Player Parameters
rel=0 不再避免显示任何相关视频;现在,它仍会显示相关视频,但至少它们只会来自您的频道。这在 2018 年 9 月 25 日左右的某个时候发生了变化 (documentation)。
通过在 YT.Player 中设置playerVars,我们至少可以让相关视频只显示我们频道的视频。文档不清楚您必须将 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: Accessing and modifying DOM nodes。似乎因为我们要从 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,而是 that may be a Chromium and Firefox thing。也许这在现场是一个合法的选择;当/如果我尝试这样做时,我会更新这篇文章,让你知道我是否成功。