I want to write a Greasemonkey script to detect when a YouTube video on a website starts playing to stop my winamp playing music. Everything works fine, my script detects the video, enables the API and also the onYouTubePlayerReady event gets called. But I have no idea how to register the onStateChange callback, this is my code:
unsafeWindow.onYouTubePlayerReady = function (playerId)
{
alert('Visible');
document.getElementById(playerId).addEventListener('onStateChange', 'stateChanged');
alert('Not visible, so the line above crashes');
}
unsafeWindow.stateChanged = function (state)
{
alert('never called, too');
}
Is there a solution for this problem or is it just impossible?
Okay, in order to get around some scoping problems, it’s best just to inject code that interacts with the YouTube API.
The following works in Firefox+Greasemonkey, and in Chrome or Chrome+Tampermonkey. It should also work on any browser that supports userscripts: