YoutubePlayer - 未触发onStateChanged事件

时间:2012-08-20 15:27:08

标签: javascript google-chrome-extension youtube-api

  

可能重复:
  addEventListener in content script not working

我正在制作我的第一个Chrome扩展程序的第一步,但是我在拦截来自YoutubePlayer的事件时遇到了一些问题。扩展名是content_scripts类型扩展名。在我的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函数没有被触发,有人知道为什么吗?

1 个答案:

答案 0 :(得分:1)

正如这个答案所说

  

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

按照已经提供的建议Insert code into the page context using a content script