Last active
May 19, 2020 03:08
-
-
Save mryellow/6b81ad26575d464521edcd6a5fb1dca0 to your computer and use it in GitHub Desktop.
Remove SBL from Youtube
This file contains 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 Remove SBL from Youtube | |
// @namespace http://tampermonkey.net/ | |
// @version 0.3 | |
// @description Remove click-bait from your bass world! | |
// @author Mr-Yellow | |
// @match https://www.youtube.com/* | |
// @grant none | |
// @require http://code.jquery.com/jquery-3.3.1.min.js | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
const username = 'devinebass'; | |
const config = { | |
batchSize: 25, | |
batchTime: 2500 | |
}; | |
let cnt = 0; | |
const classes = [ | |
'ytd-item-section-renderer', | |
'ytd-grid-renderer', | |
'ytd-watch-next-secondary-results-renderer' | |
]; | |
const hasWrapper = (classList) => { | |
if (classList) { | |
for (let i=0; i<classes.length; i++) { | |
if (classList.contains(classes[i])) return true; | |
} | |
} | |
return false; | |
}; | |
let queue = []; | |
const removeNode = (node) => { | |
if (!node) return; | |
const init = (!queue.length); | |
queue.push(node); | |
if (init) { | |
setTimeout(() => { | |
processBatch(); | |
}, 50); | |
} | |
}; | |
const processBatch = () => { | |
if (!queue.length) return; | |
console.log('Pending', username, queue.length); | |
for (let i=0; i<config.batchSize || 10; i++) { | |
const el = queue.pop(); | |
if (!el) break; | |
setTimeout(() => { | |
$(el).remove(); | |
cnt++; | |
if (cnt % 100 === 0) console.log('Removed', username, cnt); | |
}); | |
} | |
setTimeout(() => { | |
processBatch(); | |
}, config.batchTime || 5000); | |
}; | |
const targetNode = document.getElementsByTagName('body')[0]; | |
const callback = function(mutationsList, observer) { | |
for (let mutation of mutationsList) { | |
if (mutation.type !== 'childList' || !hasWrapper(mutation.target.classList)) continue; | |
for (let node of mutation.addedNodes) { | |
if (hasWrapper(node.classList) && node.innerHTML.indexOf(username) !== -1) | |
removeNode(node); | |
} | |
} | |
}; | |
const observer = new MutationObserver(callback); | |
observer.observe(targetNode, { attributes: false, childList: true, subtree: true }); | |
//observer.disconnect(); | |
// Hide initial videos. | |
const links = $('a[href$="'+username+'"'); | |
for (let i=0; i<links.length; i++) { | |
const parents = $(links[i]).parentsUntil(classes.join(',')); | |
for (let parent of parents) { | |
if (parent.tagName !== 'DIV' && hasWrapper(parent.classList)) | |
removeNode(parent); | |
} | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment