Youtube - Restore Classic

If youtube is in the new 2017 YouTube Material Redesign, automatically restore classic view

اعتبارا من 22-04-2018. شاهد أحدث إصدار.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

ستحتاج إلى تثبيت إضافة مثل Stylus لتثبيت هذا النمط.

ستحتاج إلى تثبيت إضافة لإدارة أنماط المستخدم لتتمكن من تثبيت هذا النمط.

ستحتاج إلى تثبيت إضافة لإدارة أنماط المستخدم لتثبيت هذا النمط.

ستحتاج إلى تثبيت إضافة لإدارة أنماط المستخدم لتثبيت هذا النمط.

(لدي بالفعل مثبت أنماط للمستخدم، دعني أقم بتثبيته!)

// ==UserScript==
// @name         Youtube - Restore Classic
// @version      1.0.5
// @description  If youtube is in the new 2017 YouTube Material Redesign, automatically restore classic view
// @author       Cpt_mathix
// @include      https://www.youtube.com
// @include      https://www.youtube.com/*
// @license      GPL version 2 or any later version; http://www.gnu.org/licenses/gpl-2.0.txt
// @namespace    https://greasyfork.org/users/16080
// @run-at       document-start
// @grant        none
// @noframes
// ==/UserScript==

(function() {
    init();

    window.addEventListener("spfdone", function(e) {
		document.getElementById("body").classList.remove("sitewide-ticker-visible");
	});

    function init() {
        restoreClassicYoutube();
        document.addEventListener('DOMContentLoaded', function(){
            hideNewYoutubeBanner();
        }, false);
    }

    function restoreClassicYoutube() {
        // Cookies are enabled?
        if (navigator.cookieEnabled) {
            if (document.cookie) {
                var cookie = getCookie("PREF");

                // Pref cookie exists?
                if (cookie && cookie[1]) {
                    cookie = cookie[1];
                    console.log("current PREF cookie: " + cookie);
                    if (cookie.search(/f6=(8|9)(&|;|$)/) === -1) {
                        replaceCookieAndReload(cookie);
                    } else {
                        deleteCache("reloadCount");
                    }
                } else {
                    createCookieAndReload();
                }
            } else {
                createCookieAndReload();
            }
        } else {
            console.log("Error: Youtube - Restore Classic doesn't work if cookies are disabled");
        }
    }

    function getCookie(name) {
        return document.cookie.match(RegExp('(?:^|;\\s*)' + name + '=([^;]*)'));
    }

    function createCookieAndReload() {
        document.cookie = "PREF=f6=8;path=/;domain=.youtube.com";
        reload();
    }

    function replaceCookieAndReload(cookie) {
        if (cookie.search(/f6=[^;&]*/) === -1) {
            document.cookie = "PREF=" + cookie + "&f6=8" + ";path=/;domain=.youtube.com";
        } else if (cookie.search(/f6=[^;&]*/) !== -1) {
            document.cookie = "PREF=" + cookie.replace(/f6=[^;&]*/, 'f6=8') + ";path=/;domain=.youtube.com";
        }
        reload();
    }

    function reload() {
        var reloadCount = getCache("reloadCount");
        if (reloadCount && parseInt(reloadCount) <= 3) {
            setCache("reloadCount", parseInt(reloadCount) + 1);
            location.reload();
        } else if (reloadCount && parseInt(reloadCount) > 3) {
            alert("Youtube - Restore Classic\nSomething went wrong... Please post the following information on greasyfork and disable this script\n\nDebug information:\nCookies enabled: " + navigator.cookieEnabled + "\nCurrent cookies: " + document.cookie);
            deleteCache("reloadCount");
        } else {
            setCache("reloadCount", 1);
            location.reload();
        }
    }

    function getCache(key) {
		return JSON.parse(localStorage.getItem("YTRestore#" + key));
	}

	function deleteCache(key) {
		localStorage.removeItem("YTRestore#" + key);
	}

	function setCache(key, value) {
		localStorage.setItem("YTRestore#" + key, JSON.stringify(value));
	}

    function hideNewYoutubeBanner() {
        document.getElementById("body").classList.remove("sitewide-ticker-visible");

		var css = `
#ticker {
    display: none!important;
}
`;

		var style = document.createElement("style");
		style.type = "text/css";
		if (style.styleSheet){
			style.styleSheet.cssText = css;
		} else {
			style.appendChild(document.createTextNode(css));
		}

		document.documentElement.appendChild(style);
	}
})();