Jos*_*low 5 javascript youtube-api
所以我尝试使用YouTube 的播放器 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
}
});
},
});
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1590 次 |
| 最近记录: |