Youtube: Hide Recommended Videos

Hide the recommended videos section

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name            Youtube: Hide Recommended Videos
// @namespace       https://github.com/Zren
// @version         2
// @description     Hide the recommended videos section
// @icon            https://youtube.com/favicon.ico
// @author          Zren
// @include         http*://*.youtube.com/*
// @include         http*://youtube.com/*
// @include         http*://*.youtu.be/*
// @include         http*://youtu.be/*
// @run-at          document-start
// ==/UserScript==

//--- Utils
function observe(selector, config, callback) {
	var observer = new MutationObserver(function(mutations) {
		mutations.forEach(function(mutation){
			callback(mutation);
		});
	});
	var target = document.querySelector(selector);
	observer.observe(target, config);
	return observer;
}

var documentLoaded = false;
function tickUntilLoad(callback) {
	function tick() {
		callback();
		if (!documentLoaded) {
			window.requestAnimationFrame(tick);
		}
	}
	callback();
	window.requestAnimationFrame(tick);
}

function watchBodyForChanged(callback) {
	callback();
	observe('body', {
		attributes: true,
	}, function(mutation) {
		if (mutation.attributeName === 'class') {
			callback();
		}
	});
}

//---
function getParent(e, selector) {
	var e2 = e.parentNode;
	while (e2) {
		if (e2.matches(selector)) {
			return e2;
		}
		e2 = e2.parentNode;
	}
	return null;
}

function hideSection(url) {
	var a = document.querySelector('.shelf-title-row a[href="' + url + '"]');
	if (a) {
		var section = getParent(a, '.item-section');
		section.style.display = 'none';
	}
}

function hideStuff() {
	hideSection("/feed/recommended");
	//hideSection("/feed/history"); // Watch it Again
	//hideSection("/user/IGNentertainment"); // IGN
}

tickUntilLoad(hideStuff);
document.addEventListener('DOMContentLoaded', function(){
	documentLoaded = true;
	hideStuff();
	watchBodyForChanged(hideStuff);
});