YouTube Check Out Updated Channels

Open channels with updates in new tabs

当前为 2020-09-23 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         YouTube Check Out Updated Channels
// @namespace    http://tampermonkey.net/
// @version      0.3
// @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
// @grant        GM_getValue
// @grant        GM_setValue
// @run-at       document-idle
// ==/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 > 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);
                }, 20);
            });
            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()) {
        setTimeout(() => {
            let timestamp_now = (new Date()).getTime();
            let timestamp_last_check = GM_getValue('timestampLastYouTubeUpdateCheck', Number.MIN_SAFE_INTEGER);
            if (timestamp_last_check < 0 || timestamp_now - timestamp_last_check > 120000/* 2 minutes */)
                GM_setValue('timestampLastYouTubeUpdateCheck', timestamp_now);
            else
                return;
            repeat_until_successful(checkout_updated_channels, 100);
        }, 5000);// 5 seconds delay to avoid possible conflict with other scripts
    }
})();