Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/445.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript YouTubeplyer-未激发onStateChanged事件_Javascript_Google Chrome Extension_Youtube Api - Fatal编程技术网

Javascript YouTubeplyer-未激发onStateChanged事件

Javascript YouTubeplyer-未激发onStateChanged事件,javascript,google-chrome-extension,youtube-api,Javascript,Google Chrome Extension,Youtube Api,可能重复: 我正在采取第一步来编写我的第一个Chrome扩展,但是我在拦截YoutubePlayer的事件时遇到了一些问题。该扩展是一个content_脚本类型的扩展。在我的js文件中,我将事件处理程序附加到YoutubePlayer的“onStateChange”事件。 代码看起来是这样的 var playerLoaded = ( document.getElementById("movie_player") == null ), intervalId; var attachEvents

可能重复:

我正在采取第一步来编写我的第一个Chrome扩展,但是我在拦截YoutubePlayer的事件时遇到了一些问题。该扩展是一个content_脚本类型的扩展。在我的js文件中,我将事件处理程序附加到YoutubePlayer的“onStateChange”事件。 代码看起来是这样的

var playerLoaded = ( document.getElementById("movie_player") == null ), intervalId;

var attachEvents = function(){
    var currentVideo =document.getElementById("movie_player");
    if(currentVideo){
        currentVideo.addEventListener("onStateChange", "onytplayerStateChange");
         if(intervalId)
            clearInterval(intervalId);
    }
}

function onytplayerStateChange() {
   alert("Player's new state: " + newState);
};


if(!playerLoaded){
debugger;
    intervalId = setInterval(attachEvents,100);
} else{
debugger;
  attachEvents();
}
不幸的是,onytplayerStateChange函数没有被激发,有人知道为什么吗?

正如这个答案所说

内容脚本在隔离的环境中执行。你必须 将state方法注入页面本身

遵循已经给出的建议