YouTube Video Duration Checker

Shows the duration of YouTube videos on mobile browsers.

目前為 2023-10-30 提交的版本,檢視 最新版本

// ==UserScript==
// @name         YouTube Video Duration Checker
// @namespace    https://greasyfork.org/en/users/670188-hacker09?sort=daily_installs
// @version      1
// @description  Shows the duration of YouTube videos on mobile browsers.
// @author       hacker09
// @match        https://*.youtube.com/embed/*
// @icon         https://www.youtube.com/s/desktop/03f86491/img/favicon.ico
// @run-at       document-end
// @grant        none
// ==/UserScript==

(function() {
  'use strict';
  if (document.querySelector(".ytp-time-current") !== '0:00') //If the timer does not exist
  { //Starts the if condition
    var timeDisplay = document.createElement('div'); //Create a new div element
    timeDisplay.className = 'ytp-time-display notranslate'; //Add a class to the div element
    document.querySelector(".ytp-volume-area").appendChild(timeDisplay); //Append the div element

    document.querySelector('.video-stream').addEventListener('timeupdate', function() { //When the YT video is playing
      var currentMins = Math.floor(document.querySelector('.video-stream').currentTime / 60); //Create a variable to hold the current mins
      var currentSecs = Math.floor(document.querySelector('.video-stream').currentTime % 60); //Create a variable to hold the current secs
      var totalMins = Math.floor(document.querySelector('.video-stream').duration / 60); //Create a variable to hold the total mins
      var totalSecs = Math.floor(document.querySelector('.video-stream').duration % 60); //Create a variable to hold the total mins

      currentSecs = currentSecs < 10 ? '0' + currentSecs : currentSecs; //Add leading zero if seconds are less than 10
      totalSecs = (/^\d$/.test(totalSecs)) ? totalSecs + '0' : totalSecs; //Add leading zero if minutes is a single digit

      timeDisplay.innerHTML = `<span>${currentMins}:${currentSecs} / ${totalMins}:${totalSecs}</span>`; //Show and update the timer
    }); //Update the content of the time display element
  } //Finishes the if condition
})();