SergejVolkov / YouTube Restrict (Block Clips And Suggestions)

// ==UserScript==
// @name         YouTube Restrict (Block Clips And Suggestions)
// @namespace    http://www.youtube.com/
// @version      1.0.5
// @description  Restrict YouTube videos down to containing keywords from the 'safe' list
// @author       SergejVolkov
// @match        http://www.youtube.com/*
// @match        https://www.youtube.com/*
// @license      MIT
// @grant        none
// ==/UserScript==


// Allowed keywords should either be in a video title or a channel name.
// Otherwise, the content will be blocked.
// For example,
var keywords = ['Music', 'City Pop', 'Jazz', 'Lecture', 'Math', 'Calculus'];


function BlockClip() {
    var channel_name = document.querySelector('ytd-channel-name.ytd-video-owner-renderer a').textContent.toLowerCase();
    var i;
    var doc_title = document.title.toLowerCase();
    for (i = 0; i < keywords.length; i++) {
        if (doc_title.includes(keywords[i]) || channel_name.includes(keywords[i])) {
            return;
        }
    }
    
    if (document.querySelector('ytd-player .html5-video-player.playing-mode') != null) {
        document.querySelector('ytd-player .html5-video-player .ytp-play-button').click();
    }
    
    var style = document.createElement('style');
    style.innerHTML = '/* ytd-player { display: none; } */ ytd-watch-next-secondary-results-renderer { visibility: hidden; } ytd-comment-thread-renderer, yt-formatted-string.ytd-video-primary-info-renderer span, video.html5-main-video { display: none; } #related.style-scope.ytd-watch-flexy, ytd-watch-flexy[flexy] #player-container.ytd-watch-flexy {color: var(--yt-spec-text-primary); text-align: center; font-size: 20px; padding-top: 20px; }';
    document.head.appendChild(style);
    
    document.querySelector('ytd-watch-flexy[flexy] #player-container.ytd-watch-flexy').textContent = 'Video Blocked';
    document.querySelector('#related.style-scope.ytd-watch-flexy').textContent = 'Playlist Blocked';
    var titles = document.querySelectorAll('yt-formatted-string.ytd-video-primary-info-renderer');
    titles[1].textContent = 'Unfortunately, Your Provider Has Blocked This Clip';
}

function BlockMainPage() {
    var style = document.createElement('style');
    style.innerHTML = '#contents.ytd-rich-grid-renderer { display: none; } ytd-rich-grid-renderer {color: var(--yt-spec-text-primary); text-align: center; font-size: 20px; padding-top: 20px; }';
    document.head.appendChild(style);
    
    document.querySelector('ytd-rich-grid-renderer').textContent = 'Suggestions Blocked';
}

function BlockChannel() {
    var i;
    var doc_title = document.title.toLowerCase();
    for (i = 0; i < keywords.length; i++) {
        if (doc_title.includes(keywords[i])) {
            return;
        }
    }
    
    if (document.querySelector('ytd-player .html5-video-player.playing-mode') != null) {
        document.querySelector('ytd-player .html5-video-player .ytp-play-button').click();
    }
    
    var style = document.createElement('style');
    style.innerHTML = '#contents.ytd-section-list-renderer, #channel-header-container yt-formatted-string.ytd-channel-name span { display: none; } ytd-section-list-renderer {color: var(--yt-spec-text-primary); text-align: center; font-size: 20px; padding-top: 20px; }';
    document.head.appendChild(style);
    
    document.querySelector('#channel-header-container yt-formatted-string.ytd-channel-name').textContent = 'Content Blocked';
  	document.querySelector('ytd-section-list-renderer').textContent = 'Unfortunately, your provider has blocked this channel';
}

function Restrict() {
    if (window.location.href.includes('youtube.com/watch?')) {
        BlockClip();
    } else if (window.location.href.endsWith('youtube.com/')) {
        BlockMainPage();
    } else if (window.location.href.includes('youtube.com/channel/') || window.location.href.includes('youtube.com/c/') || window.location.href.includes('youtube.com/user/')) {
        BlockChannel();
    }
}

function TimeOut() {
    setTimeout(Restrict, 1000);
}


for (var i_k = 0; i_k < keywords.length; i_k++) {
    keywords[i_k] = keywords[i_k].toLowerCase();
}

window.addEventListener('click', TimeOut, true);

// After page load
TimeOut();