Auto show more for YouTube

This plugin press "Show more" button when display it on your browser by scroll.

目前為 2018-02-23 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name           Auto show more for YouTube
// @name:ja        YouTubeの「もっと見る」ボタンを自動で押す
// @description    This plugin press "Show more" button when display it on your browser by scroll.
// @description:ja YouTube.comの「もっと見る」ボタンが、スクロールによって画面に表示された瞬間、自動で押されます。
// @namespace      https://twitter.com/sititou70
// @license     MIT
// @include        /https*:\/\/www\.youtube\.com\/.*/
// @version        2.0.1
// @grant          none
// ==/UserScript==

const user_agent = window.navigator.userAgent.toLowerCase();

const adjust_scroll_px = 0;

let clicked_buttons = [];

const fireClickEvent = (elem) => {
    if( user_agent.match(/(msie|MSIE)/) || user_agent.match(/(T|t)rident/) ) {
        elem.fireEvent("onclick");
    } else {
        const event = document.createEvent("MouseEvents");
        event.initEvent("click", true, true);
        elem.dispatchEvent(event);
    }
};

document.addEventListener("scroll", function(){
    buttons.forEach(function(obj){
        document.querySelectorAll(obj.selector).forEach((elem) => {
            if(clicked_buttons.includes(elem))return;

            if(elem.getBoundingClientRect().y - window.innerHeight + adjust_scroll_px < 0){
                clicked_buttons.push(elem);
                fireClickEvent(elem);
            }
        });
    });
});

document.addEventListener("click", function(e){
    let target = e.target;
    while(target !== document.body){
        if(target.tagName == "A"){
            clicked_buttons = [];
            return;
        }
        target = target.parentElement;
    }
});

const buttons = [
    {
        name: "Comment show more button",
        selector: ".more-button > paper-button.ytd-comment-replies-renderer",
    },
    {
        name: "Watch more related button",
        selector: "paper-button.yt-next-continuation",
    },
    {
        name: "Video info show more button",
        selector: "paper-button.ytd-expander",
    }
];