Skip to content

Instantly share code, notes, and snippets.

@Nathaniel-Wu
Last active July 16, 2021 04:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nathaniel-Wu/fabd62df2d6146121fa4b9cfcae08763 to your computer and use it in GitHub Desktop.
Save Nathaniel-Wu/fabd62df2d6146121fa4b9cfcae08763 to your computer and use it in GitHub Desktop.
Open channels with updates in new tabs, to be launched from Tampermonkey context menu
// ==UserScript==
// @name YouTube Check Out Updated Channels
// @namespace http://tampermonkey.net/
// @version 0.5
// @description Open channels with updates in new tabs
// @author Nathaniel Wu
// @include *www.youtube.com/*
// @license Apache-2.0
// @supportURL https://gist.github.com/Nathaniel-Wu/fabd62df2d6146121fa4b9cfcae08763
// @run-at context-menu
// ==/UserScript==
(function () {
'use strict';
function is_channel_updated(channel) {
return window.getComputedStyle(channel.querySelector('div#newness-dot')).display == "block";
}
function check_for_updates(subscriptions) {
subscriptions.querySelectorAll('ytd-guide-entry-renderer').forEach(channel => {
if (is_channel_updated(channel)) {
let link = channel.querySelector('a').href;
if (!Boolean(link.match(/\/videos($|\/$)/)))
link += '/videos'
window.open(link);
}
});
}
function checkout_updated_channels() {
let subscriptions = document.querySelector('div#content > tp-yt-app-drawer#guide div#sections > ytd-guide-section-renderer:nth-of-type(2) > div#items');//subject to change
if (subscriptions == null)
return false;
let channels = subscriptions.querySelectorAll(':scope > ytd-guide-entry-renderer');
if (is_channel_updated(channels[channels.length - 1])) {
let active_DOMNodeInsertion = 0;
subscriptions.addEventListener("DOMNodeInserted", () => {
active_DOMNodeInsertion++;
setTimeout(() => {
active_DOMNodeInsertion--;
if (active_DOMNodeInsertion == 0)
check_for_updates(subscriptions);
}, 100);
});
subscriptions.querySelector('ytd-guide-collapsible-entry-renderer > ytd-guide-entry-renderer#expander-item').click();
} else
check_for_updates(subscriptions);
return true;
}
function repeat_until_successful(function_ptr, interval) {
if (!function_ptr())
setTimeout(() => {
repeat_until_successful(function_ptr, interval);
}, interval);
}
function in_iframe() {
try {
return window.self !== window.top;
} catch (e) {
return true;
}
}
if (!in_iframe()) {
repeat_until_successful(checkout_updated_channels, 1000);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment