0

I am trying to run a function every time the player state changes but onStateChange is not firing when I use the following code in my extension.

function onPlayerStateChange( newState ) {
    console.log( "New state: " + newState );
}

player = document.getElementById( "movie_player" );
player.addEventListener( "onStateChange", "onPlayerStateChange" );

It runs fine when I run it through the chrome javascript console but for some reason I have not been able to get it to work with my extension.

This is the relevant part of the manifest.json:

"content_scripts": [ {
  "matches": [ "*://*.youtube.com/watch*" ],
  "js": [ "jquery.js", "page.js" ]
} ],

And this is a part of page.js:

function onPlayerStateChange( newState ) {
    console.log( "New state: " + newState );
}

player = document.getElementById( "movie_player" );
player.addEventListener( "onStateChange", "onPlayerStateChange" );
Gsx
  • 942
  • 8
  • 14
  • Try player.addEventListener( "onStateChange", onPlayerStateChange); – 方 觉 Aug 05 '13 at 06:38
  • 1
    @方觉 You can't do this. The Flash code in the player finds the callback function via `window['whateverFunctionNameYouPassed']`. You can't pass a function reference like with the normal `addEventListener` calls. You could always generate a random symbol and bind the function to *that* however. – David-SkyMesh Aug 05 '13 at 06:44
  • @Gsx You probably need to define the `onPlayerStateChange` function within the scope of the website's window. e.g: `targetWindow['onPlayerStateChange'] = function(newState) { ... }` where targetWindow is the DOM window your extension is targeting. – David-SkyMesh Aug 05 '13 at 06:47

0 Answers0