Youtube: Hide Recommended Videos

Hide the recommended videos section

目前為 2016-09-14 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name            Youtube: Hide Recommended Videos
// @namespace       https://github.com/Zren
// @version         1
// @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);
	console.log('target', target)
	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) {
		console.log(mutation.type, 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 hideRecommended() {
	var a = document.querySelector('.shelf-title-row a[href="/feed/recommended"]')
	if (a) {
		var section = getParent(a, '.item-section');
		section.style.display = 'none';
	}
}

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