YouTube Video Duration Checker

Shows the duration of YouTube videos on mobile browsers.

当前为 2023-10-30 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==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
})();