Youtube OCD number replacer

Replaces a number in the end of youtube videos duration.

目前為 2024-08-27 提交的版本,檢視 最新版本

// ==UserScript==
// @name         Youtube OCD number replacer
// @namespace    https://www.nexusmods.com/palworld/mods/1134
// @version      2024-08-27
// @description  Replaces a number in the end of youtube videos duration.
// @author       Deli Almendra
// @match        https://www.youtube.com/*
// @icon         https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Fcdn.iconscout.com%2Ficon%2Ffree%2Fpng-256%2Focd-file-913037.png
// @grant        none
// @license MIT
// ==/UserScript==

let number_to_replace = '3';

function durationEndsWithThree(duration) {
  return duration.endsWith(number_to_replace);
}

function updateBadgeShapeWizElement(element) {
  const durationTextElement = element.querySelector('.badge-shape-wiz__text');
  if (durationTextElement) {
    const duration = durationTextElement.textContent.trim();
    if (durationEndsWithThree(duration)) {
      const modifiedDuration = duration.slice(0, -1) + '4'; // Replace the last character with '4'
      durationTextElement.textContent = modifiedDuration;

      // Update aria-label if it exists
      if (element.hasAttribute('aria-label')) {
        let ariaLabel = element.getAttribute('aria-label');
        ariaLabel = ariaLabel.replace(/\d+$/, (match) => match.slice(0, -1) + '4');
        element.setAttribute('aria-label', ariaLabel);
      }
    }
  }
}

function updateYtpTimeDurationElement() {
  const durationTextElement = document.querySelector('.ytp-time-duration');
  if (durationTextElement) {
    const duration = durationTextElement.textContent.trim();
    if (durationEndsWithThree(duration)) {
      const modifiedDuration = duration.slice(0, -1) + '4'; // Replace the last character with '4'
      durationTextElement.textContent = modifiedDuration;
    }
  }
}

function processDurationElements() {
  // Process badge-shape-wiz elements
  const badgeShapeWizElements = document.querySelectorAll('.badge-shape-wiz');
  badgeShapeWizElements.forEach(updateBadgeShapeWizElement);

  // Process ytp-time-duration element
  updateYtpTimeDurationElement();
}

const observer = new MutationObserver(processDurationElements);
observer.observe(document.body, { childList: true, subtree: true });

// Initial run
processDurationElements();