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

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

当前为 2023-08-08 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name          Youtube: expand description and long comments; show all the replies
// @description   Video description, long comments and list of subscriptions are expanded automatically; all the replies are shown after pressing "View all N replies" button
// @author        MK
// @namespace     max44
// @homepage      https://greasyfork.org/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.2.3
// @license       MIT
// @grant         none
// @require       https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
// @run-at        document-idle
// ==/UserScript==

(function () {
  'use strict';

  var videoIdAtLastCheck = "";
  var btnClick = null;
  var waitVideo;
  var i;

  var observerBody = null;
  var observerSubs = null;
  var flgSubsDone = false;
  var observerDesc = null;
  var observerComments = null;
  var observerCommentsNotif = null;
  var observerComPost = null;

  //---
  //--- Listen to global page changes
  //---
  const callbackBody = function (mutationsList, observer) {

    var pathArray = window.location.pathname.split('/');
    var firstPath = pathArray[1];
    var lastPath = pathArray[pathArray.length - 1];

    //Check whether video is new to expand description
    if (firstPath === "watch") {
      var player = $( "div#content ytd-watch-flexy" );
      if (player != null && player.length > 0) {
        clearInterval(waitVideo); //Stop waiting for video
        var videoId = player[0].getAttribute("video-id");
        player = null;

        if (videoIdAtLastCheck != videoId) {
          videoIdAtLastCheck = videoId;
          //console.log("new video");
          expandDesc();
        }
      }
    }

/*    //---
    //--- Listen to description and expand it
    //---
    if (observerDesc == null) {
      const callbackDesc = function (mutationsList, observer) {
        //Check whether video is new to expand description
        if (firstPath === "watch") {
          var player = $( "div#content ytd-watch-flexy" );
          if (player != null && player.length > 0) {
            clearInterval(waitVideo); //Stop waiting for video
            var videoId = player[0].getAttribute("video-id");
            player = null;

            if (videoIdAtLastCheck != videoId) {
              videoIdAtLastCheck = videoId;
              console.log("new video");
              expandDesc();
            }
          }
        }
      }
      let nodeDesc = document.querySelector("#player");//document.querySelector(".watch-active-metadata");
      if (nodeDesc != null) {
        observerDesc = new MutationObserver(callbackDesc);
        observerDesc.observe(nodeDesc, {childList: true, subtree: true, attributes: false, characterData: false});
        console.log("desc observer added");
      }
    }*/

    //Remove subscriptions observer after subscriptions have been expanded
    if (flgSubsDone && observerSubs != null) {
      observerSubs.disconnect(); //Expand subscriptions only once
      observerSubs = null;
      //console.log("subs observer removed");
    }

    //---
    //--- Listen to community posts and expand them
    //---
    if (observerComPost == null && lastPath === "community") {
      const callbackComPost = function (mutationsList, observer) {
        expandComPost();
        //console.log("post expanded");
      }
      let nodeComPost = document.querySelector("div#content");
      if (nodeComPost != null) {
        observerComPost = new MutationObserver(callbackComPost);
        observerComPost.observe(nodeComPost, {childList: true, subtree: true, attributes: false, characterData: false});
        //console.log("post observer added");
      }
    }
    //Remove community post observer on non-community pages
    if (observerComPost != null && lastPath != "community") {
      observerComPost.disconnect();
      observerComPost = null;
      //console.log("post observer removed");
    }

    //---
    //--- Listen to comments and expand them
    //---
    if (observerComments == null && (firstPath === "watch" || firstPath === "post" || lastPath === "community")) {
      const callbackComments = function (mutationsList, observer) {
        expandComments();
        //console.log("comments expanded");
      }
      let nodeComments = null;
      if (firstPath === "watch") {
        nodeComments = document.querySelector("ytd-comments");
        if (nodeComments != null) {
          observerComments = new MutationObserver(callbackComments);
          observerComments.observe(nodeComments, {childList: true, subtree: true, attributes: false, characterData: false});
          //console.log("comments observer added");
        }
      } else {
        nodeComments = document.querySelector("body");
        if (nodeComments != null) {
          observerComments = new MutationObserver(callbackComments);
          observerComments.observe(nodeComments, {childList: true, subtree: true, attributes: false, characterData: false});
          //console.log("comments observer added");
        }
      }
    }
    //Remove comments observer
    if (observerComments != null && firstPath != "watch" && firstPath != "post" && lastPath != "community") {
      observerComments.disconnect();
      observerComments = null;
      //console.log("comments observer removed");
    }

    //---
    //--- Listen to comments in notification submenu and expand them
    //---
    if (observerCommentsNotif == null) {
      const callbackCommentsNotif = function (mutationsList, observer) {
        expandCommentsNotif();
        //console.log("notif comments expanded");
      }
      let nodeCommentsNotif = null;
      nodeCommentsNotif = document.querySelector("ytd-popup-container #contentWrapper");
      if (nodeCommentsNotif != null) {
        observerCommentsNotif = new MutationObserver(callbackCommentsNotif);
        observerCommentsNotif.observe(nodeCommentsNotif, {childList: true, subtree: true, attributes: false, characterData: false});
        //console.log("notif comments observer added");
      }
    }
  }

  let nodeBody = document.querySelector("body");
  if (nodeBody != null) {
    const observerBody = new MutationObserver(callbackBody);
    observerBody.observe(nodeBody, {childList: true, subtree: true, attributes: false, characterData: false});
    //console.log("body observer added");
  }

  //---
  //--- Listen to subscriptions and expand them
  //---
  const callbackSubs = function (mutationsList, observer) {
    expandSubs();
  }
  let nodeSubs = document.querySelector("div#guide-wrapper");
  if (nodeSubs != null) {
    observerSubs = new MutationObserver(callbackSubs);
    observerSubs.observe(nodeSubs, {childList: true, subtree: true, attributes: false, characterData: false});
    //console.log("subs observer added");
  }

  /*//Check what buttons to click after each scroll of main window
  $( window ).scroll(function() {
    //expandComPost();
    expandComments();
  });*/


  //---------------------------------------
  // Expand description
  //---------------------------------------
  function expandDesc() {
    //Expand description
    /*btnClick = $( "div#description ytd-text-inline-expander tp-yt-paper-button#expand[role='button']:not([hidden=''])" );
    if (btnClick != null && btnClick.length > 0) {// && isVisible(btnClick)) {
      btnClick[0].click();
    }*/

    //Expand description - suggested by gcobc12632
    btnClick = $( "yt-interaction#description-interaction" );
    if (btnClick != null) {// && isVisible(btnClick)) {
      btnClick.click();
    }

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

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

  //---------------------------------------
  // Expand comments
  //---------------------------------------
  function expandComments() {
    //Expand long comments and hide "show less" button in comments section
    btnClick = $( "#primary ytd-comment-renderer #comment-content tp-yt-paper-button#more:not([hidden='']) > span.more-button[slot='more-button']" );
    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
      }
    }

    //Show all replies upon pressing "Show more replies" button
    btnClick = $( "#primary div#replies div#expander div#expander-contents:not([hidden]) div#button.ytd-continuation-item-renderer ytd-button-renderer.ytd-continuation-item-renderer button.yt-spec-button-shape-next: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
      }
    }

    //Show all replies upon pressing "View all N replies" button (7ktTube | 2016 REDUX script)
    btnClick = $( "#primary div#replies div#expander div#expander-contents:not([hidden]) 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
      }
    }
  }

  //---------------------------------------
  // Expand comments in notification submenu
  //---------------------------------------
  function expandCommentsNotif() {
    //Expand long comments and hide "show less" button in notification submenu
    btnClick = $( "#submenu 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
      }
    }
  }

  //---------------------------------------
  // Show all subscriptions
  //---------------------------------------
  function expandSubs() {
    btnClick = $( "#guide div#sections div#items ytd-guide-collapsible-entry-renderer.ytd-guide-section-renderer[can-show-more=''] #expander-item" );
    if (btnClick != null) {
      for (i = 0; i < btnClick.length; i++) {
        if (isVisible(btnClick[i])) {
          btnClick[i].click();
          flgSubsDone = true;
        }
      }
    }
  }

  //---------------------------------------
  // Check all the parents of element to find whether it is visible or not
  //---------------------------------------
  function isVisible(pObj) {
    if (pObj != null) {
      var checkNext = true;
      var vObj = pObj;

      while (checkNext) {
        checkNext = false;
        //console.log("checking element " + vObj.tagName + "#" + vObj.id + ": '" + document.defaultView.getComputedStyle(vObj,null)['display'] + "'");
        if (document.defaultView.getComputedStyle(vObj,null)['display'] != "none") {
          if (vObj.parentElement != null) {
            vObj = vObj.parentElement;
            checkNext = true;
          }
        } else {
          return false;
        }
      }
      return true;
    }
    return false;
  }

})();

    /*//Detect spinner at main comments
    var spinnerMain = $( "#primary div#replies div#expander tp-yt-paper-spinner#spinner[active]" );
    if (spinnerMain != null && spinnerMain.length > 0) {
      console.log("main active spinner detected");
      spinnerActive = true;

      //Listen to spinner changes
      const spinnerCallback = function (mutationsList, observer) {
        expandComments();

        //spinnerMain = $( "#primary div#replies div#expander tp-yt-paper-spinner#spinner[active]" );
        if (spinnerMain[0].getAttribute("active") == null || spinnerMain[0].getAttribute("active") == "") {
          console.log("main spinner deactivated");
          spinnerObserver.disconnet();
        }
      }

      var spinnerNode = document.querySelector("#primary div#replies div#expander tp-yt-paper-spinner#spinner[active]");
      if (spinnerNode != null) {
        const spinnerObserver = new MutationObserver(spinnerCallback);
        spinnerObserver.observe(spinnerNode, {childList: true, subtree: true, attributes: true, characterData: true});
      }

    } else if (spinnerActive) {
      console.log("spinner stopped");
      spinnerActive = false;
      expandComments();
    }*/