Please write a stable and fully functional Tampermonkey script that accomplishes the…
- User
Please write a stable and fully functional Tampermonkey script that accomplishes the following task: Convert this: 44.2K subscribers and 44.2K subscribers into this: 44,200 subscribers and 44,200 subscribers. Ensure that the script is compatible with the latest versions of major web browsers (e.g., Chrome, Firefox, Edge) and follows best coding practices for Tampermonkey scripts. Include detailed comments explaining each part of the code to enhance readability and maintainability. Avoid using deprecated features or methods that may lead to errors or unintended behavior.
- Assistant
// ==UserScript== // @name YouTube Subscriber Count Expander // @namespace http://tampermonkey.net/ // @version 1.0 // @description Expand abbreviated subscriber counts on YouTube to full numbers (e.g., from 44.2K to 44,200) // @match *://*.youtube.com/* // @grant none // ==/UserScript== (function() { 'use strict'; /** * Converts an abbreviated subscriber count (e.g., '44.2K') to a full number with commas (e.g., '44,200'). * @param {string} text - The text containing the abbreviated number. * @return {string} - The text with the expanded number. */ function expandAbbreviatedNumber(text) { // Regular expression to match the abbreviated number and units. var regex = /([\d\.]+)\s*([KM])\s*(.*)/i; var match = text.match(regex); if (match) { var number = parseFloat(match[1]); var unit = match[2].toUpperCase(); var suffix = match[3]; // The rest of the text (e.g., 'subscribers'). var multiplier = 1; // Determine the multiplier based on the unit. if (unit === 'K') { multiplier = 1000; } else if (unit === 'M') { multiplier = 1000000; } // Extend with 'B' for billions if needed. var expandedNumber = Math.round(number * multiplier); // Format the number with commas for readability. var expandedNumberWithCommas = expandedNumber.toLocaleString(); return expandedNumberWithCommas + ' ' + suffix; } else { // If no abbreviation is found, return the original text. return text; } } /** * Processes the subscriber count elements on the page and replaces abbreviated numbers with full numbers. */ function processSubscriberCounts() { // Process the element with id 'owner-sub-count' (channel subscriber count below the video). var ownerSubCount = document.getElementById('owner-sub-count'); if (ownerSubCount) { ownerSubCount.textContent = expandAbbreviatedNumber(ownerSubCount.textContent); } // Process the subscriber count in the channel header (e.g., on 'About' tab). // Select all <span> elements with the specific class indicating subscriber count. var subCountSpans = document.querySelectorAll('span.yt-core-attributed-string.yt-content-metadata-view-model-wiz__metadata-text'); subCountSpans.forEach(function(span) { span.textContent = expandAbbreviatedNumber(span.textContent); }); } /** * Sets up a MutationObserver to monitor changes to the DOM and re-process subscriber counts when necessary. */ function observeMutations() { // Select the node to observe (we'll observe the entire body). var targetNode = document.body; // Create an observer instance linked to the callback function. var observer = new MutationObserver(function(mutationsList, observer) { for(var mutation of mutationsList) { if (mutation.type === 'childList') { // When new nodes are added, process the subscriber counts. processSubscriberCounts(); } } }); // Configuration of the observer: // - childList: Set to true if mutations to target's children are to be observed. // - subtree: Set to true to observe all descendants (not just immediate children). var config = { childList: true, subtree: true }; // Start observing the target node with the given configuration. observer.observe(targetNode, config); } // Run the processSubscriberCounts function once on initial load. processSubscriberCounts(); // Set up the MutationObserver to monitor changes and process subscriber counts dynamically. observeMutations(); })();This Tampermonkey script expands abbreviated YouTube subscriber counts (like "44.2K") to their full numerical representation with commas (like "44,200"). It automates the following steps:
-
Function
expandAbbreviatedNumber:- Uses a regular expression to detect numbers followed by "K" (thousand) or "M" (million).
- Converts the abbreviated number to a full number by applying the appropriate multiplier.
- Formats the number with commas for readability.
-
Function
processSubscriberCounts:- Targets the subscriber count element below the video (
id="owner-sub-count"). - Targets subscriber count spans in the channel header using specific class selectors.
- Updates their text content with the expanded number.
- Targets the subscriber count element below the video (
-
Function
observeMutations:- Sets up a
MutationObserverto monitor the page for dynamic content changes (common on YouTube). - Reapplies
processSubscriberCountswhenever new nodes are added to the DOM.
- Sets up a
-
Initialization:
- Invokes
processSubscriberCountson script load to handle the initial content. - Starts observing mutations to keep the subscriber counts updated in real-time.
- Invokes
Compatibility and Best Practices:
- Uses modern JavaScript features compatible with the latest versions of major browsers (Chrome, Firefox, Edge).
- Avoids deprecated methods and global namespace pollution by encapsulating code within an IIFE (Immediately Invoked Function Expression).
- Provides detailed comments explaining each part of the code for better readability and maintainability.
- Utilizes efficient DOM selectors and event listeners to minimize performance impact.
Note: This script specifically targets YouTube's current DOM structure as of the knowledge cutoff in October 2023. If YouTube updates its HTML structure, the selectors may need adjustment.
-