Duração Restante da Playlist no YouTube

Mostra automaticamente a duração restante da playlist no título, ao assistir um vídeo do YouTube com playlist.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Duração Restante da Playlist no YouTube
// @namespace    http://tampermonkey.net/
// @version      0.0.2
// @description  Mostra automaticamente a duração restante da playlist no título, ao assistir um vídeo do YouTube com playlist.
// @author       JPedro-M
// @match        http://*.youtube.com/*
// @match        https://*.youtube.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=youtube.com
// @grant        none
// @license      MIT
// ==/UserScript==

(function () {
  'use strict';

  setInterval(() => {
      init();
  }, 5000);

  function init() {
      const containers = document.querySelectorAll("#playlist");
      const titlesContainers = document.querySelectorAll("#header-description > h3:nth-child(1) > yt-formatted-string");
      if (containers.length === 0 || titlesContainers.length === 0) {
          return;
      }

      const itens = containers[containers.length - 1].querySelectorAll(".badge-shape-wiz__text");
      const title = titlesContainers[titlesContainers.length - 1];

      let totalTime = 0;
      let viewed = false;

      if (itens.length === 0) {
          return;
      }

      itens.forEach(element => {
          const time = element.textContent.trim();
          const timeParts = time.split(":").map(Number);

          const minutes = timeParts.length === 2 ? timeParts[0] : 0;
          const seconds = timeParts.length === 2 ? timeParts[1] : 0;

          if (element.closest("#playlist-items").hasAttribute("selected")) {
              viewed = true;
          }
          if (!viewed) {
             return;
          }

          const totalSeconds = (minutes * 60) + seconds;

          totalTime += totalSeconds;
      })

      const totalHours = Math.floor(totalTime / 3600);
      const totalMinutes = Math.floor((totalTime % 3600) / 60);
      const totalSeconds = totalTime % 60;
      const totalTimeFormatted = `${totalHours > 0 ? String(totalHours).padStart(2, '0') + ":" : ""}${String(totalMinutes).padStart(2, '0')}:${String(totalSeconds).padStart(2, '0')}`;

      title.textContent = `${title.title} - ${totalTimeFormatted}`;
  }
})();