YouTube Age Bypass

shitty but works in 2018 (requires to manually reload page)

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         YouTube Age Bypass
// @description  shitty but works in 2018 (requires to manually reload page)
// @version      1.0.1
// @grant        none
// @include	     https://www.youtube.com/*
// @run-at       document-end
// @namespace https://greasyfork.org/users/149768
// ==/UserScript==
(function() {
    "use strict";

    const inject = function() {
        "use strict";

        var overriddenVideo = null

        function haveVideoData() {
            return typeof window.ytInitialPlayerResponse != 'undefined' && window.ytInitialPlayerResponse != null;
        }

        function isCurrentVideoAgeRestricted() {
            return typeof window.ytInitialPlayerResponse.playabilityStatus.desktopLegacyAgeGateReason != 'undefined' && window.ytInitialPlayerResponse.playabilityStatus.desktopLegacyAgeGateReason;
        }

        function getVideoId() {
            return window.ytInitialPlayerResponse.videoDetails.videoId;
        }

        function removeNode(n) {
            if (n != null) n.parentNode.removeChild(n);
        }

        function waitForNodeId(node, cb) {
            var ival = setInterval(function(node, cb) {
                if (document.getElementById(node) != null) {
                    clearInterval(ival);
                    cb();
                }
            }, 100, node, cb);
        }

        function checkAndUnrestrict() {

            if (overriddenVideo != null) { // if we have injected a video and navigate away, we need to clean up
                console.log("Navigating away from page, removing iframe");
                removeNode(overriddenVideo);
            }

            if (!haveVideoData() || !isCurrentVideoAgeRestricted()) {
                return;
            }

            console.log("Is video age restricted: " + isCurrentVideoAgeRestricted())

            waitForNodeId('player-container', function() {
                console.log("Found player container");
                removeNode(document.getElementById('error-screen'));

                waitForNodeId('container', function() {
                    console.log("Found movie player");

                    var oldplayer = document.getElementById('container');
                    var container = document.querySelector('div.ytd-player');

                    var videoplayer = document.createElement('div');
                    videoplayer.className = "html5-video-player ytp-transparent ytp-hide-info-bar ytp-large-width-mode iv-module-loaded paused-mode";

                    //

                    var playercontent = document.createElement('div');
                    playercontent.className = "ytp-player-content ytp-iv-player-content";


                    var playerframe = window.document.createElement("iframe");
                    playerframe.setAttribute("src", "//www.youtube.com/embed/" + getVideoId() + "?autoplay=1&showinfo=0&rel=0");
                    playerframe.setAttribute("id", "movie_player");
                    playerframe.setAttribute("frameBorder", "0");
                    playerframe.setAttribute("width", "100%");
                    playerframe.setAttribute("height", "100%");

                    container.appendChild(videoplayer);
                    videoplayer.appendChild(playercontent);
                    playercontent.appendChild(playerframe);

                    waitForNodeId('player', function() {
                        removeNode(document.getElementById('player'));
                    });

                    overriddenVideo = playerframe;
                    console.log("injected new video");

                });


            });
        }

        checkAndUnrestrict();
    }


    const script = document.createElement("script");
    const target = document.head || document.documentElement;
    script.text = "(" + inject.toString() + ")();";

    target.appendChild(script);
    target.removeChild(script);
})();