Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to remove certain "related videos" by keyword from the Youtube player (which shows at the end of a video). The purpose is that sometimes there are certain content that I don't want to see.

I am a total greasemonkey and javascript noob. Nevertheless, the best I have come up with so far is as follows. This example removes any "related videos" with the keyword "anatomy".

JavaScript
==UserScript==
// @name     Remove related youtubes2
// @include  http://*.youtube.com/*
// @require  http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js
// @require  https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant    GM_addStyle
// ==/UserScript==

waitForKeyElements ("#player-api", action);

function action (jNode) {
    var playerapi = jNode.html ();
    var lastapi = jNode.data ("lastapi") || "";
    if (playerapi != lastapi) {
        playerapi = playerapi.replace(/title%3D(.(?!%26))*?anatomy(?!%2C)(.(?!%2C))*?id%3D[\w-]{11}/ig, 'id%3D');
        playerapi = playerapi.replace(/id%3D[\w-]{11}(?!%2C)(?=([^&](?!%2C))*?anatomy)/ig, 'id%3D');
        jNode.html (playerapi);
        jNode.data ("lastapi", jNode.html ());
    }
    return true;
}

To explain it in natural language, the above code relies on waitForKeyElements to listen for changes that occur to the #player-api element, which contains the "related videos". The function "action" saves the #player-api into playerapi. lastapi is the old playerapi content. If there has been a change to playerapi, the "related videos" with the keyword "anatomy" will be removed by regex expressions. playerapi is saved into lastapi for comparison next time.

Now try searching for "grey's anatomy" (sorry, I don't really have anything against the show, just an example). Then go to the end of the video. You will see that any "related videos" that have "anatomy" in its title will have been removed (replaced by a black square among the other "related videos"). This works as intended (as do the regex expressions).

The problem begins when you click a related video from the sidebar (to the right). Go to the end of the video again. You will see that the script has stopped filtering subsequent "related videos" with the keyword "anatomy". If you press reload from your browser however, the script filters properly again. (I know how to remove the sidebar videos, and have not included the code here).

Diagnosis: when you click a "related video" from the sidebar, Youtube does not update the #player-api element itself, but some other element in the page dynamically (and I don't know which). #player-api itself does not change (unless you reload the entire Youtube page), so the function "action" never finds a changed #player-api to act on unless you "reload" the entire page.

Questions: Does Youtube refresh an area of inline code when you click a related video from the sidebar? If so, how can I account for this? Is there an easier way to do all this?

Thank you in advance.
Posted
Updated 25-Oct-13 0:19am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900