Youtube: expand description and long comments; show all the replies

Video description and long comments are expanded automatically; all the replies are shown after pressing "View all N replies" button

目前为 2022-10-08 提交的版本。查看 最新版本

// ==UserScript==
// @name          Youtube: expand description and long comments; show all the replies
// @description   Video description and long comments are expanded automatically; all the replies are shown after pressing "View all N replies" button
// @author        MK
// @namespace     max44
// @homepage      https://gf.qytechs.cn/en/users/309172-max44
// @match         *://*.youtube.com/*
// @match         *://*.youtu.be/*
// @icon          https://cdn.icon-icons.com/icons2/1488/PNG/512/5295-youtube-i_102568.png
// @version       1.0.5
// @license       MIT
// @grant         none
// @require       https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
// @run-at        document-idle
// ==/UserScript==

//TO DO: when you select "View all N replies" you will get all of them without necessity to click "view more comments" all the time

(function () {
  'use strict';

  var urlAtLastCheck = "";
  var nameAtLastCheck = "";
  var btnClick = null;
  var i;

  setInterval(function() { //Check page content constantly
    //Check whether URL and channel name have changed. If yes, clear the attribute clicked-by-script, because it survives change of the page
    if (urlAtLastCheck != window.location.href && nameAtLastCheck != document.title) {
      urlAtLastCheck = window.location.href;
      nameAtLastCheck = document.title;
      btnClick = $( "div#description ytd-text-inline-expander tp-yt-paper-button#expand[role='button'][clicked-by-script='true']" );
      if (btnClick != null && btnClick.length > 0) btnClick[0].removeAttribute("clicked-by-script");
      btnClick = $( "div#meta-contents ytd-video-secondary-info-renderer div ytd-expander tp-yt-paper-button#more[clicked-by-script='true']" );
      if (btnClick != null && btnClick.length > 0) btnClick[0].removeAttribute("clicked-by-script");
    }

    //Check whether description has been expanded by another script or addon. If yes, mark it as if it was expanded by the script to avoid double processing
    btnClick = $( "div#description ytd-text-inline-expander tp-yt-paper-button#expand[role='button']:not([clicked-by-script='true'])[hidden='']" ); //Description was expanded by another script or addon
    if (btnClick != null && btnClick.length > 0) {
      btnClick[0].setAttribute("clicked-by-script", "true"); //Do not click it again
    }

    //Expand description
    btnClick = $( "div#description ytd-text-inline-expander tp-yt-paper-button#expand[role='button']:not([clicked-by-script='true']):not([hidden=''])" );
    if (btnClick != null && btnClick.length > 0) {
      btnClick[0].click();
      btnClick[0].setAttribute("clicked-by-script", "true"); //Do not click it again
    }

    //Expand description for 7ktTube | 2016 REDUX script
    btnClick = $( "div#meta-contents ytd-video-secondary-info-renderer div ytd-expander tp-yt-paper-button#more:not([clicked-by-script='true'])" );
    if (btnClick != null && btnClick.length > 0) {
      btnClick[0].click();
      btnClick[0].setAttribute("clicked-by-script", "true"); //Do not click it again
    }

    //Expand replies and hide "show less" button
    btnClick = $( "#primary ytd-comment-renderer #comment-content tp-yt-paper-button#more:not([hidden='']) > span.more-button[slot='more-button']:not([clicked-by-script='true'])" );
    if (btnClick != null) {
      for (i = 0; i < btnClick.length; i++) {
        btnClick[i].click();
        btnClick[i].setAttribute("clicked-by-script", "true"); //Do not click it again
        btnClick[i].parentNode.previousElementSibling.setAttribute("style", "display:none;"); //Hide "Show less" button
      }
    }

    //Expand post in community section
    btnClick = $( "div#post ytd-expander#contentTextExpander tp-yt-paper-button#more[role='button']:not([clicked-by-script='true']):not([hidden=''])" );
    if (btnClick != null && btnClick.length > 0) {
      btnClick[0].click();
      btnClick[0].setAttribute("clicked-by-script", "true"); //Do not click it again
    }

    //Show all replies upon pressing "View all N replies" button
    btnClick = $( "div#replies div#expander div#button.ytd-continuation-item-renderer ytd-button-renderer tp-yt-paper-button#button[role='button']:not([clicked-by-script='true'])" );
    if (btnClick != null) {
      for (i = 0; i < btnClick.length; i++) {
        btnClick[i].click();
        btnClick[i].setAttribute("clicked-by-script", "true"); //Do not click it again
      }
    }

  }, 250); //Interval to check page content

})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址