Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6772285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:31:55+00:00 2026-05-26T15:31:55+00:00

I want to write a Greasemonkey script to detect when a YouTube video on

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T15:31:55+00:00Added an answer on May 26, 2026 at 3:31 pm

    “the problem is just the listener on “onStateChange” “

    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:

    function GM_main () {
        window.stateChanged = function (state) {
            console.log ('GM: In stateChanged().  State = ', state);
        }
    
        window.onYouTubePlayerReady = function (playerId) {
            /*-- playerId is not being set by Youtube. Use
                hard-coded id (movie_player) instead.
            */
            var playerNode  = document.getElementById ("movie_player");
            if (playerNode) {
                /*--- Note, inside onYouTubePlayerReady ONLY, the YouTube API
                    seems to override addEventListener. Hence the nonstandard
                    parameters.
                */
                playerNode.addEventListener ('onStateChange', 'stateChanged');
    
                console.log ('GM: Listener installed just fine.');
            }
            else
                console.error ("GM: Player node not found!");
        }
    }
    
    addJS_Node (null, null, GM_main);
    
    function addJS_Node (text, s_URL, funcToRun, runOnLoad) {
        var D                                   = document;
        var scriptNode                          = D.createElement ('script');
        if (runOnLoad) {
            scriptNode.addEventListener ("load", runOnLoad, false);
        }
        scriptNode.type                         = "text/javascript";
        if (text)       scriptNode.textContent  = text;
        if (s_URL)      scriptNode.src          = s_URL;
        if (funcToRun)  scriptNode.textContent  = '(' + funcToRun.toString() + ')()';
    
        var targ = D.getElementsByTagName ('head')[0] || D.body || D.documentElement;
        targ.appendChild (scriptNode);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to write a greasemonkey script/chrome user script that takes any images with
I want to write a greasemonkey script to scrape sites and save data locally,
I am trying to write a GreaseMonkey script in which I want to find
i want to write a greasemonkey script that reloads every 10 sec. the same
I wanted to write a very simple greasemonkey script because I hate the are
I really really want to write a GreaseMonkey extension. I have written a lot
I'm curently busy on a site for which i want to write a greasemonkey
I'm trying to write a Greasemonkey script to change colours on the default GMail
I would like to write a greasemonkey script that given an xpath returns all
AdBlock sometimes fails to block popups, so using Greasemonkey I want to write my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.