Youtube 2020 improvements

All Youtube improvements in one pack

À partir de 2021-03-30. Voir la dernière version.

// ==UserScript==
// @name         Youtube 2020 improvements
// @version      0.7.7
// @description  All Youtube improvements in one pack
// @author       Burlaka.net
// @match        *://*.youtube.com/*
// @match        *://youtube.com/*
// @require      https://code.jquery.com/jquery-latest.js
// @grant        none
// @namespace    http://tampermonkey.net/
// ==/UserScript==

(function() {
    'use strict';

    $('body').append('<style>\
#playlist .header {display:none !important;}\
body:not(.no-scroll) #player-container {width: 100%;max-width: 854px;height:100%;max-height:480px; margin: 0 auto;}\
body:not(.no-scroll) #player-theater-container {max-height:480px !important}\
body:not(.no-scroll) #container.ytd-masthead {height:34px}\
#page-manager.ytd-app{margin-top:34px}\
body.no-scroll #page-manager.ytd-app {margin-top:0}\
.viewed-opacity {opacity: 0.5}\
</style>');

    var watchlater = `<ytd-mini-guide-entry-renderer class="style-scope ytd-mini-guide-renderer" aria-selected="false" role="tab" tabindex="0" aria-label="Watch later">
        <a id="endpoint" tabindex="-1" class="yt-simple-endpoint style-scope ytd-mini-guide-entry-renderer" title="Watch later" href="/playlist?list=WL">
            <yt-icon id="icon" class="guide-icon style-scope ytd-mini-guide-entry-renderer"><svg viewBox="0 0 24 24" preserveAspectRatio="xMidYMid meet" focusable="false" class="style-scope yt-icon" style="pointer-events: none; display: block; width: 100%; height: 100%;">
                    <g class="style-scope yt-icon">
                        <path d="M18.7 8.7H5.3V7h13.4v1.7zm-1.7-5H7v1.6h10V3.7zm3.3 8.3v6.7c0 1-.7 1.6-1.6 1.6H5.3c-1 0-1.6-.7-1.6-1.6V12c0-1 .7-1.7 1.6-1.7h13.4c1 0 1.6.8 1.6 1.7zm-5 3.3l-5-2.7V18l5-2.7z" class="style-scope yt-icon"></path>
                    </g>
                </svg>
            </yt-icon>
            <span class="title style-scope ytd-mini-guide-entry-renderer">Watch later</span>
            <paper-tooltip animation-delay="0" offset="4" position="right" class="style-scope ytd-mini-guide-entry-renderer" role="tooltip" tabindex="-1" hidden="" style="--paper-tooltip-delay-in:0ms;">
                <div id="tooltip" class="style-scope paper-tooltip hidden">
                    Watch later
                </div>
            </paper-tooltip>
        </a>
    </ytd-mini-guide-entry-renderer>`;

    var watchlater2 = '<div style="margin: 0 auto;"><a href="/playlist?list=WL" class="yt-simple-endpoint ytd-mini-guide-entry-renderer"><span class="title style-scope ytd-mini-guide-entry-renderer">Watch later</span></a></div>';

    $('#items.ytd-mini-guide-renderer').on('mouseover', function(e) {
        $('#items.ytd-mini-guide-renderer').append(watchlater2);
        watchlater2 = '';
        //console.log('wl-added');
    });


    // comment autoexpand onmouseover
    $(document).on('mouseover', '.ytd-item-section-renderer .ytd-comment-renderer ytd-expander', function(e) {
        var $this = $(this);
        $this.removeAttr('collapsed');
        $this.parent().find('#more').attr('hidden');
    });

    // video info autoexpand onmouseover
    $(document).on('mouseover', 'ytd-expander.ytd-video-secondary-info-renderer', function(e) {
        var $this = $(this);
        $this.removeAttr('collapsed');
        $this.find('#collapsible').removeAttr('hidden');
        $this.find('#less').removeAttr('hidden');
        $this.find('#more').attr('hidden');
    });


    // expand video name in tootltip (recommendations after video ends)
    $(document).on('mouseover', '.ytp-ce-covering-overlay .ytp-ce-video-title', function(e) {
        $(this).parent().attr('title', $(this).text());
    });

    // expand video name in watchlater playlist
    $('#items.playlist-items.ytd-playlist-panel-renderer').on('mouseover', '#container.ytd-playlist-panel-video-renderer', function(e) {
        $(this).attr('title', $(this).find('#video-title.ytd-playlist-panel-video-renderer').text());
    });

    // set opacity to viewed videos
    $(document).on('scroll', function() {
        if (window.scrollY == 0) {
            $('.viewed-opacity').removeClass('viewed-opacity');
        }
        $('#progress.ytd-thumbnail-overlay-resume-playback-renderer').parent().parent().parent().addClass('viewed-opacity'); //.parent()
    });

    // add video date to video views title
    $(document).on('mouseover', '#count.ytd-video-primary-info-renderer ytd-video-view-count-renderer', function(e) {
        $(this).attr('title', $('#date.ytd-video-primary-info-renderer yt-formatted-string.ytd-video-primary-info-renderer').text());
    });

    // add title on embed videos
    $(document).on('mouseover', '.ytp-title-link', function(e) {
        $(this).attr('title', $(this).text());
    });

    // hide elements over video
    $('.ytp-right-controls').prepend('<button class="ytp-button" onclick="$(\'.ytp-ce-element\').toggle();return false;" style="padding: 0px 3px;position: relative;font-size: 1.1em;top: -1.2em;right: 0.3em;">Hide</button>');


    /*
      Stop playlist autoplay
    */
    (function() {
      var ypm;
      function f() {
        if (ypm) {
          ypm.canAutoAdvance_ = false;
        } else {
          ypm = document.getElementsByTagName('yt-playlist-manager')[0];
        }
      }
      f();
      setInterval(f, 5000);
    })();

    /*
      Channel videos redirect
    */
    document.addEventListener('mouseover', getLink);

    function getLink(linkElement) {
        var url = linkElement.target.toString();

        if ((url.search(/www.youtube.com/) != -1) && (url.match(/\//g).length < 5)) {
            if ((url.match(/https:\/\/www.youtube.com\/channel\//i) && (/videos/.test(url) == false))) {
                changeLink(linkElement);
            } else if ((url.match(/https:\/\/www.youtube.com\/c\//i) && (/videos/.test(url) == false))) {
                changeLink(linkElement);
            } else if ((url.match(/https:\/\/www.youtube.com\/user\//i) && (/videos/.test(url) == false))) {
                changeLink(linkElement);
            }
        }
    }

    function changeLink(linkElement) {
        var newUrl = linkElement.target.toString().concat("/videos");
        linkElement.target.href = newUrl;
    }


    /*
      Player keyboard shortcuts
    */
    var volume;
    $(document).ready(function() {
        $(window).keydown(function(e) {
            var player = $('.video-stream.html5-main-video')[0];
            var player_wrap = $('.html5-video-player');

/*
            if (!player_wrap.is(":focus") && !$('input').is(":focus") && !$('textarea').is(":focus") && !$('.comment-simplebox-text').is(":focus") && !$('[contenteditable="true"]').is(":focus")) {
                if (e.keyCode == 0 || e.keyCode == 32) { // Space = play/pause
                    e.preventDefault();
                    if (player.paused == false) {
                        player.pause();
                    } else {
                        player.play();
                    }
                }
            }
*/
            if ((e.ctrlKey || e.metaKey) && e.keyCode == 38) { // Ctrl + Up = Volume up
                volume = player.volume + 0.1;
                if (volume > 1) volume = 1;
                player.volume = volume;
            }

            if ((e.ctrlKey || e.metaKey) && e.keyCode == 40) { // Ctrl + Down = Volume down
                volume = player.volume - 0.1;
                if (volume < 0) volume = 0;
                player.volume = volume;
            }
            if (e.which === 27) { // Esc Shift+Tab = focus on body //e.shiftKey
                e.preventDefault();
                $('#page').focus();
                $('video').blur();
                $('#masthead-search-term').focus();
                $('#yt-masthead-container').focus();

            } else if (e.which == 9) { // Tab = focus on video element
                e.preventDefault();
                $('video').focus();
            }
        });
    });

})();