我在将 Youtube iframe API 与最新稳定版 Chrome(版本 85)结合使用时遇到问题。我知道一个月前一切正常,但现在,即使完全遵循 Youtube iframe API 文档中的最基本示例:
https://developers.google.com/youtube/iframe_api_reference#Getting_Started
不再工作了。onReady 和 onStateChange 事件未被触发,并且“播放器”对象缺少其大部分功能,例如 player.playVideo() 未定义。这个问题在我测试过的任何其他浏览器上都没有发生,只有当我在测试这个时登录到我的 Youtube 帐户时才会发生。
我怀疑问题的根源是来自 Youtube 的请求发送的 cookie,因为我在“发现问题”选项卡的控制台中收到此错误:
“通过指定其SameSite属性来指示是否在跨站请求中发送cookie”
,而 Chrome 在 85 版本的注释中提到了这一点:
“拒绝不安全的 SameSite=None cookie”
如果我退出 Youtube,这与播放器工作的事实相符,因为 Youtube 使用这些 cookie 根据您的个人资料推荐不同的视频。
我现在正在寻找解决方法,我唯一能找到的是,如果我手动创建 iframe,而不是使用 API,我可以在 iframe 的 src 中放置“youtube-nocookie”而不是“youtube”,但是这样我就没有可以参考的对象来控制播放器,例如,如果我必须创建一个自定义按钮来暂停/播放视频。我想主要是在 Youtube 上修复他们的 API,但是现在有什么方法可以解决这个问题吗?
这是一个 Codepen 来说明问题,代码取自上面链接的 Youtube Iframe API 文档:
https://codepen.io/Gabielovv/pen/VwadJvg?editors=1111
// 2. This code loads the IFrame Player API code asynchronously.
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
// 3. This function creates an <iframe> (and YouTube player)
// after the API code downloads.
var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
height: '390',
width: '640',
videoId: 'M7lc1UVf-VE',
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
// 4. The API will call this function when the video player is ready.
function onPlayerReady(event) {
console.log("onPlayerReady");
event.target.playVideo();
}
芜湖不芜
桃花长相依
相关分类