// ==UserScript==
// @name YouTube Subscriber Count Expander
// @namespace http://tampermonkey.net/
// @version 1.0
// @description Converts abbreviated YouTube subscriber counts into full numbers (e.g., "44.2K subscribers" -> "44,200 subscribers").
// @author
// @match https://www.youtube.com/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
/**
* Parses an abbreviated number string and converts it into a full number string with commas.
* For example: '44.2K' -> '44,200'
* @param {string} s - The abbreviated number string (e.g., '44.2K subscribers')
* @returns {string} - The full number string (e.g., '44,200')
*/
function parseAbbreviatedNumber(s) {
// Remove 'subscribers' text and any extra whitespace
s = s.replace(/subscribers/i, '').trim();
// Remove any commas from the string
s = s.replace(/,/g, '');
// Regular expression to capture the number and optional unit (K, M, B)
const match = s.match(/^([\d\.]+)([KMB]?)$/i);
if (!match) {
// If the string doesn't match the pattern, return the original string
return s;
}
// Parse the numeric part of the string
let num = parseFloat(match[1]);
// Get the unit (if any) and convert it to uppercase
const unit = match[2].toUpperCase();
// Multiply the number based on the unit
switch (unit) {
case 'K':
num = num * 1000;
break;
case 'M':
num = num * 1000000;
break;
case 'B':
num = num * 1000000000;
break;
default:
// No unit, number remains the same
break;
}
// Format the number with commas as thousands separators
const fullNum = num.toLocaleString();
return fullNum;
}
/**
* Updates the subscriber count element by converting the abbreviated number to full number.
* @param {HTMLElement} element - The subscriber count element to update
*/
function updateSubscriberCount(element) {
// Get the original text content of the element
const originalText = element.textContent;
// Check if the element has already been processed
if (element.getAttribute('data-expanded') === 'true') {
// If so, do nothing
return;
}
// Parse and expand the abbreviated number
const fullNumber = parseAbbreviatedNumber(originalText);
// Update the element's text content with the full number and 'subscribers' text
element.textContent = fullNumber + ' subscribers';
// Update the 'aria-label' attribute for accessibility
element.setAttribute('aria-label', fullNumber + ' subscribers');
// Mark the element as processed to avoid duplicate processing
element.setAttribute('data-expanded', 'true');
}
/**
* Creates a MutationObserver to watch for changes in the DOM and update subscriber counts when necessary.
*/
function observeMutations() {
// Create a new MutationObserver instance and pass in the callback function
const observer = new MutationObserver(function(mutations) {
// Loop through each mutation record
for(const mutation of mutations) {
// If the mutation is of type 'childList' (nodes added or removed)
if(mutation.type === 'childList') {
// Loop through all added nodes
mutation.addedNodes.forEach(function(node) {
// Only process element nodes (type 1)
if(node.nodeType === Node.ELEMENT_NODE) {
// Check if the subscriber count element is within the added node or its descendants
const subCountElement = node.querySelector('#owner-sub-count');
if (subCountElement) {
// Update the subscriber count element
updateSubscriberCount(subCountElement);
}
}
});
}
}
});
// Start observing the document body for changes in the child elements and their descendants
observer.observe(document.body, {
childList: true, // Look for added or removed child nodes
subtree: true // Extend the observation to the entire subtree
});
}
/**
* Initializes the script by updating the subscriber count (if present) and setting up mutation observers.
*/
function init() {
// Try to find the subscriber count element on the page
const subCountElement = document.querySelector('#owner-sub-count');
if (subCountElement) {
// Update the subscriber count element if found
updateSubscriberCount(subCountElement);
}
// Start observing for future changes in the DOM
observeMutations();
}
// Run the init function when the DOM is fully loaded
if (document.readyState === 'complete' || document.readyState === 'interactive') {
init();
} else {
// Add event listener for DOMContentLoaded if the DOM is not yet ready
document.addEventListener('DOMContentLoaded', init);
}
})();
Explanation:
This Tampermonkey script expands abbreviated YouTube subscriber counts (like "44.2K subscribers") into their full numerical form ("44,200 subscribers"). Here's how the script works:
-
Metadata Block: The metadata at the top provides Tampermonkey with information about the script, including where it should run (https://www.youtube.com/*).
-
IIFE (Immediately Invoked Function Expression): The entire script is wrapped in an IIFE to prevent variable leakage into the global scope.
-
parseAbbreviatedNumber Function:
- Purpose: Converts an abbreviated number string into a full number string with commas.
- Process:
- Removes the word "subscribers" and any commas.
- Uses a regular expression to split the number and its unit (K, M, B).
- Multiplies the base number by the appropriate value based on the unit.
- Formats the full number with commas.
-
updateSubscriberCount Function:
- Purpose: Updates the subscriber count element on the page.
- Process:
- Checks if the element has already been processed to avoid redundant work.
- Parses the abbreviated number using
parseAbbreviatedNumber.
- Updates the text content and
aria-label attribute of the element.
- Marks the element as processed by adding a
data-expanded attribute.
-
observeMutations Function:
- Purpose: Observes changes to the DOM to catch dynamic content changes (important for YouTube's SPA behavior).
- Process:
- Creates a
MutationObserver to watch for added nodes.
- When new nodes are added, checks if the subscriber count element is among them.
- If found, calls
updateSubscriberCount on the new element.
-
init Function:
- Purpose: Initializes the script.
- Process:
- Tries to immediately find and update the subscriber count element.
- Starts observing mutations for future updates.
-
Event Listener for DOMContentLoaded:
- Ensures the
init function runs as soon as the DOM is ready.
Compatibility and Best Practices:
- Browser Compatibility: The script uses modern JavaScript features (like
const, let, and arrow functions) supported by the latest versions of major browsers (Chrome, Firefox, Edge).
- Coding Practices:
- Uses strict mode for safer JavaScript execution.
- Variables are properly scoped using
let and const.
- Functions are documented with comments explaining their purpose and how they work.
- Avoids deprecated features and relies on standard, well-supported web APIs.
Usage:
- Install the Tampermonkey extension in your browser if you haven't already.
- Create a new script in Tampermonkey and paste the entire code above.
- Save the script.
- Navigate to any YouTube video page, and you should see the subscriber count expanded to its full numerical form.
Note: Ensure that no other extensions or scripts interfere with YouTube's page content, as they might conflict with this script's operation.