Auto show more for YouTube

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

目前为 2018-02-23 提交的版本。查看 最新版本

// ==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",
    }
];