所以我尝试使用YouTube's player API,它在前2或3个实例中效果很好。然后它崩溃了并停止工作,拒绝渲染我传递给它的任何视频。
似乎每500毫秒大约两次在控制台中引发此错误。
www-widgetapi.js:100无法在“ DOMWindow”上执行“ postMessage”: 提供的目标原点('https://www.youtube.com')不匹配 收件人窗口的来源('http://localhost')。
这是代码
let ajax_url = "http://localhost/scripts/ajax";
let newest_video;
let player;
$(document).ready(() => {
loadVideo();
});
function onPlayerReady(event) {
event.target.playVideo();
}
function onPlayerStateChange(event) {
if(event.data === 0) {
loadVideo(true);
}
}
function loadVideo(next = false) {
$.ajax({
url: ajax_url + '/get_link.php',
data: {
"selection": (next ? "not_first" : "first")
},
type: 'GET',
error: function(data) {
alert(data);
},
success: function(data) {
newest_video = data;
let videoId = data.substring(data.lastIndexOf('=') + 1);
if (next) {
alert('loading second video<br>' + data + '<br>' + videoId);
}
player = new YT.Player('player', {
width: '1100',
height: '590',
videoId: videoId,
events: {
onReady: onPlayerReady,
onStateChange: onPlayerStateChange
}
});
},
});
}