Here's a Tampermonkey script that converts abbreviated YouTube subscriber counts (such as "6,34 mln") into full numbers ("6 340 000"). This script is compatible with the latest versions of major web browsers like Chrome, Firefox, and Edge. It adheres to best coding practices and includes detailed comments for readability and maintainability.
// ==UserScript==
// @name YouTube Subscriber Count Expander
// @namespace http://tampermonkey.net/
// @version 1.0
// @description Convert abbreviated YouTube subscriber counts to full numbers (e.g., "6,34 mln" to "6 340 000").
// @author
// @match https://www.youtube.com/*
// @grant none
// ==/UserScript==
(function () {
'use strict';
/**
* Updates the subscriber counts on the page by converting abbreviated numbers into full numbers.
*/
function updateSubscriberCounts() {
// Select all subscriber count elements with the specified class names.
const elements = document.querySelectorAll(
'span.yt-subscription-button-subscriber-count-branded-horizontal.yt-subscriber-count'
);
// Iterate over each element to process and update its content.
elements.forEach((element) => {
// Get the text content of the element and trim any leading/trailing whitespace.
const text = element.textContent.trim();
// Regular expression to match numbers with units (e.g., "6,34 mln" or "1,2 tys.").
const match = text.match(/^([\d,]+)\s*(tys\.|mln)$/i);
if (match) {
const numberStr = match[1]; // The numeric part as a string.
const unit = match[2]; // The unit ("mln" or "tys.").
// Replace comma with dot for decimal parsing (if comma is used as decimal separator).
let number = parseFloat(numberStr.replace(',', '.'));
// Multiply the number by the appropriate factor based on the unit.
if (unit.toLowerCase() === 'mln') {
number *= 1_000_000; // Multiply by one million.
} else if (unit.toLowerCase() === 'tys.') {
number *= 1_000; // Multiply by one thousand.
} else {
// If the unit is unrecognized, skip processing this element.
return;
}
// Format the number with non-breaking spaces as thousand separators.
// Using 'pl-PL' locale for formatting; adjust as needed for different locales.
const formattedNumber = number
.toLocaleString('pl-PL')
.replace(/\s/g, '\u00A0'); // Replace spaces with non-breaking spaces.
// Update the text content of the element with the formatted full number.
element.textContent = formattedNumber;
// Update the 'title' and 'aria-label' attributes with the formatted full number.
element.setAttribute('title', formattedNumber);
element.setAttribute('aria-label', formattedNumber);
}
});
}
// Run the update function on initial page load.
updateSubscriberCounts();
// Create a MutationObserver to watch for changes in the document.
// This handles dynamically loaded content (e.g., when navigating YouTube without full page reloads).
const observer = new MutationObserver(() => {
// Run the update function to process any new subscriber count elements.
updateSubscriberCounts();
});
// Start observing the document body for added child elements and subtree modifications.
observer.observe(document.body, {
childList: true,
subtree: true,
});
})();
Explanation of the Code:
Tampermonkey Metadata Block:
// ==UserScript==
// @name YouTube Subscriber Count Expander
// @namespace http://tampermonkey.net/
// @version 1.0
// @description Convert abbreviated YouTube subscriber counts to full numbers (e.g., "6,34 mln" to "6 340 000").
// @match https://www.youtube.com/*
// @grant none
// ==/UserScript==
- @name: Name of the script.
- @namespace: A unique identifier to avoid conflicts with other scripts.
- @version: Version of the script.
- @description: A brief description of what the script does.
- @match: Specifies which pages the script will run on (all YouTube pages in this case).
- @grant: Indicates that no special permissions are required.
Main Functionality:
(function () {
'use strict';
// Function to update subscriber counts.
function updateSubscriberCounts() {
// Select all subscriber count elements with the specified classes.
const elements = document.querySelectorAll(
'span.yt-subscription-button-subscriber-count-branded-horizontal.yt-subscriber-count'
);
elements.forEach((element) => {
const text = element.textContent.trim();
const match = text.match(/^([\d,]+)\s*(tys\.|mln)$/i);
if (match) {
const numberStr = match[1]; // Numeric part as a string.
const unit = match[2]; // Unit (e.g., "mln" or "tys.")
// Parse the number, handling comma as decimal separator.
let number = parseFloat(numberStr.replace(',', '.'));
// Multiply by appropriate factor based on unit.
if (unit.toLowerCase() === 'mln') {
number *= 1_000_000;
} else if (unit.toLowerCase() === 'tys.') {
number *= 1_000;
} else {
return; // Unrecognized unit; skip.
}
// Format the full number with non-breaking spaces.
const formattedNumber = number
.toLocaleString('pl-PL')
.replace(/\s/g, '\u00A0');
// Update the element's content and attributes.
element.textContent = formattedNumber;
element.setAttribute('title', formattedNumber);
element.setAttribute('aria-label', formattedNumber);
}
});
}
// Run the update function on page load.
updateSubscriberCounts();
// Observe the document for changes to handle dynamic content.
const observer = new MutationObserver(() => {
updateSubscriberCounts();
});
observer.observe(document.body, {
childList: true,
subtree: true,
});
})();
Detailed Breakdown:
Notes on Compatibility and Best Practices:
Customization:
-
Localization:
- The script uses
'pl-PL' locale in toLocaleString() for Polish formatting (e.g., using commas as decimal separators and spaces as thousand separators).
- Adjust the locale in
toLocaleString('pl-PL') to match your preferred number formatting conventions.
-
Units Handling:
- The regular expression and unit processing handle "mln" (million) and "tys." (thousand).
- If there are other units or abbreviations used, you can extend the regular expression and unit conversion logic accordingly.
Installation Instructions:
-
Install Tampermonkey Extension:
- For your browser (Chrome, Firefox, Edge), download and install the Tampermonkey extension from the respective web store.
-
Create a New Script:
- Click on the Tampermonkey icon in your browser and select "Create a new script".
-
Paste the Script Code:
- Remove any pre-filled content in the editor and paste the entire script code provided above.
-
Save the Script:
- Click on "File" and then "Save" (or use the shortcut
Ctrl+S / Cmd+S).
-
Refresh YouTube:
- Open YouTube or refresh the page if it's already open. The subscriber counts should now display the full numbers.
Testing and Verification:
- Navigate to various YouTube channels and verify that the subscriber counts displayed are the full numbers.
- Ensure that the script works both on initial page load and when navigating within YouTube (e.g., clicking on different videos or channels without a full page refresh).
By following the above script and explanations, you should have a fully functional Tampermonkey script that converts abbreviated YouTube subscriber counts into full numbers, enhancing readability and providing accurate information. The detailed comments and adherence to modern coding standards ensure the script is maintainable and compatible with current web technologies.