0

Possible Duplicate:
addEventListener in content script not working

I'm making first steps to write mine first Chrome extensionm, however I have some problems with intercepting events from YoutubePlayer. The extension is a content_scripts type extension .In my js file I'm attaching event handler to "onStateChange" event of YoutubePlayer. The code looks like that

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();
}

Unfortunatelly onytplayerStateChange function is not fired, does anybody know why?

Community
  • 1
  • 1
Berial
  • 557
  • 1
  • 8
  • 23

1 Answers1

1

As this answer says

Content scripts are executed in an isolated environment. You have to inject the state method in the page itself.

Follow the advice already given there Insert code into the page context using a content script

Community
  • 1
  • 1
Mike Grace
  • 16,636
  • 8
  • 59
  • 79