Youtube: expand description and replies

Video description and long comments are expanded automatically

目前為 2022-07-22 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name          Youtube: expand description and replies
// @description   Video description and long comments are expanded automatically
// @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.0.1
// @license       MIT
// @grant         none
// @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 = $( "#columns #primary 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
      }
    }

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

})();