Created
October 25, 2019 18:49
-
-
Save vsevolod/06bd4bed9ca8fe3b31b4a31939f6ab4f to your computer and use it in GitHub Desktop.
BlackList youtube trends channels (Greasemonkey script)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Blacklist Youtube Channels | |
// @version 1 | |
// @grant GM.setValue | |
// @grant GM.getValue | |
// @include https://www.youtube.com/* | |
// ==/UserScript== | |
const blacklistChannelName = 'BlacklistYoutubeChannels'; | |
var channels = []; | |
getParent = function(element, localName) { | |
if (element) { | |
if (element.localName == localName) { | |
return element; | |
} else { | |
return getParent(element.parentNode, localName); | |
}; | |
}; | |
}; | |
removeChannel = function(channelName) { | |
let linksSelector = `a[href='/channel/${channelName}'], a[href='/user/${channelName}']`; | |
let links = document.querySelectorAll(linksSelector); | |
links.forEach(function(el){ | |
let parent = getParent(el, 'ytd-video-renderer'); | |
if (parent) { | |
parent.remove(); | |
}; | |
}); | |
}; | |
// Remove blacklisted channels | |
(async () => { | |
channels = await GM.getValue(blacklistChannelName, []); | |
console.log('Blacklist Channels:', channels); | |
for(let idx in channels) { | |
removeChannel(channels[idx]); | |
}; | |
})(); | |
// Add links | |
const channelDivs = document.querySelectorAll('ytd-channel-name'); | |
channelDivs.forEach(function(channelDiv){ | |
if (channelDiv.localName) { | |
const linkToChannel = channelDiv.querySelector('a[href^="/channel/"], a[href^="/user/"]') | |
if (linkToChannel) { | |
const channel = linkToChannel.href.split('/').pop(); | |
if (channel) { | |
let link = document.createElement('a'); | |
link.onclick = function() { | |
(async () => { | |
channels.push(channel); | |
await GM.setValue(blacklistChannelName, [...new Set(channels)]); | |
removeChannel(channel); | |
})(); | |
}; | |
link.innerHTML=" To BlackList "; | |
channelDiv.appendChild(link); | |
}; | |
}; | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment