Youtube Playlist Total Duration

Add the total duration of a YouTube Playlist

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Youtube Playlist Total Duration
// @namespace    http://tampermonkey.net/
// @version      0.3
// @description  Add the total duration of a YouTube Playlist
// @author       Benoit Durand
// @match        *://www.youtube.com/playlist?list=*
// @grant        none
// ==/UserScript==

function addPlaylistDuration() {

  var el = document.getElementsByClassName("style-scope ytd-thumbnail-overlay-time-status-renderer");
  var nb_video = document.getElementById("stats").getElementsByClassName("style-scope yt-formatted-string")[0].innerHTML;

  if (el.length == 2*nb_video) {

    var s = 0;
    var eq_sec = [3600, 60, 1];

    for (var i = 0; i < el.length; i++) {

      if (el[i].tagName == "SPAN") {

        var time = el[i].innerHTML.replace(/\n/gi, "").split(":");
        var selec = eq_sec.slice(-time.length);

        for (var j = 0; j < time.length; j++){
            s = s + Number(time[j]) * selec[j];
        }
      }
    }

    var nb_hour = Math.trunc(s / 3600);
    s = s%3600;
    var nb_min = Math.trunc(s / 60);
    s = s%60;
    var nb_sec = s;

    var final_string = "\n Total Duration : " + nb_hour + ":" + nb_min.toString().padStart(2, '0') + ":" + nb_sec.toString().padStart(2, '0') ;

    document.getElementById("description").appendChild(document.createTextNode(final_string));

  } 

}


var targetNode = document.querySelectorAll('div#overlays.style-scope.ytd-thumbnail')[0];
var config = {childList: true};

var callback = function(mutationsList){
     mutationsList.forEach(function(mutation) {
         addPlaylistDuration();
         observer.disconnect();
    });
};

var observer = new MutationObserver(callback);
observer.observe(targetNode, config);