替代 window["onYouTubePlayerAPIReady"]

Alternative to window["onYouTubePlayerAPIReady"]

是否有 window["onYouTubePlayerAPIReady"] 的替代方案?如果 youTube 播放器 API 已准备就绪,但 window["onYouTubePlayerAPIReady"] 已被另一个无法修改的脚本使用,我需要执行一个函数。

如果您有多个 onYouTubePlayerAPIReady 函数的声明,之前的函数将被覆盖并且永远不会被调用。

另一种解决方案是保留对第一个 onYouTubePlayerAPIReady 函数的引用,然后在您自己的或第二个 onYouTubePlayerAPIReady 函数中调用您保留的函数引用。

setTimeout( function() {
    if ( typeof window.onYouTubePlayerAPIReady !== 'undefined' ) {
        if ( typeof window.gambitOtherYTAPIReady === 'undefined' ) {
            window.gambitOtherYTAPIReady = [];
        }
        window.gambitOtherYTAPIReady.push( window.onYouTubePlayerAPIReady );
    }
    window.onYouTubePlayerAPIReady = function() {

        // Initialize YT.Player and do stuff here

        if ( typeof window.gambitOtherYTAPIReady !== 'undefined' ) {
            if ( window.gambitOtherYTAPIReady.length ) {
                window.gambitOtherYTAPIReady.pop()();
            }
        }
    }

查看此 page 了解更多信息。