Youtube Get Playlist Time

Get Youtube Playlist Total Time

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Youtube Get Playlist Time
// @namespace    
// @version      1.0
// @description  Get Youtube Playlist Total Time
// @author       Ze
// @include      https://www.youtube.*/playlist*
// @grant        
// ==/UserScript==
function getPlaylistTime() {
    "use strict";
    var spanTitles = Array.from(document.querySelectorAll('div.timestamp > span')),
        spanLength = 0,
        spanContent = "",
        i = 0,
        j = 0,
        k = 0,
        hoursArray = [],
        minutesArray = [],
        secondsArray = [],
        totalHours = 0,
        totalMinutes = 0,
        totalSeconds = 0,
        elementAppend = document.querySelector('h1.pl-header-title'),
        getTimeButton = document.createElement('button');
    for (i; i < spanTitles.length; i += 1) {
        spanLength = spanTitles[i].textContent.length;
        spanContent = spanTitles[i].textContent;
        if (spanLength === 5) {
            hoursArray.push(0);
            minutesArray.push(parseInt(spanContent.substring(0, 2), 10));
            secondsArray.push(parseInt(spanContent.substring(3, 5), 10));
        } else if (spanLength === 4) {
            hoursArray.push(0);
            minutesArray.push(parseInt(spanContent.substring(0, 1), 10));
            secondsArray.push(parseInt(spanContent.substring(2, 4), 10));
        } else if (spanLength === 7) {
            hoursArray.push(parseInt(spanContent.substring(0, 1), 10));
            minutesArray.push(parseInt(spanContent.substring(2, 4), 10));
            secondsArray.push(parseInt(spanContent.substring(5, 7), 10));
        } else if (spanLength === 8) {
            hoursArray.push(parseInt(spanContent.substring(0, 2), 10));
            minutesArray.push(parseInt(spanContent.substring(3, 5), 10));
            secondsArray.push(parseInt(spanContent.substring(6, 8), 10));
        }
    }
    for (j; j < spanTitles.length; j += 1) {
        totalHours += hoursArray[j];
        totalMinutes += minutesArray[j];
        totalSeconds += secondsArray[j];
    }
    function pad(str, max) {
        str = str.toString();
        return str.length < max ? pad("0" + str, max) : str;
    }
    totalMinutes += parseInt((totalSeconds / 60), 10);
    totalHours += parseInt((totalMinutes / 60), 10);
    totalSeconds = pad((totalSeconds % 60), 2);
    totalMinutes = pad((totalMinutes % 60), 2);
    getTimeButton.textContent = totalHours + ":" + totalMinutes + ":" + totalSeconds;
    getTimeButton.style.fontSize = "22px";
    getTimeButton.style.color = "Red";
    elementAppend.appendChild(getTimeButton);
}
function loadAllVideos() {
    "use strict";
    if (document.querySelector('#pl-video-list > button') !== null) {
        document.querySelector('#pl-video-list > button').click();
        setTimeout(loadAllVideos, 2000);
    } else {
        setTimeout(getPlaylistTime, 100);
    }
}
loadAllVideos();