Skip to content

Instantly share code, notes, and snippets.

@Tancred423
Last active December 8, 2023 16:35
Show Gist options
  • Save Tancred423/775a5ba7877dd0987a4e7274e8b5562d to your computer and use it in GitHub Desktop.
Save Tancred423/775a5ba7877dd0987a4e7274e8b5562d to your computer and use it in GitHub Desktop.
Tapermonkey script to clean up YouTube titles - Formats fully capitalised words and removes spaces in front of question marks and exclamation marks.
// ==UserScript==
// @name Clean YouTube Titles
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Cleans up YouTube titles by formatting fully capitalised words and removing spaces in front of question marks and exclamation marks.
// @author Tancred and ChatGPT :)
// @match https://www.youtube.com/*
// @grant none
// @require https://cdn.jsdelivr.net/gh/CoeJoder/waitForKeyElements.js@v1.2/waitForKeyElements.js
// ==/UserScript==
(function() {
'use strict'
/////////////// SETTINGS (FEEL FREE TO ADJUST THEM TO YOUR NEEDS) ///////////////
// Set to "true" to print all converted words in your browser's console. You can filter the console by "Clean YouTube Titles"
const isDebug = false // Default: false
// How many milliseconds to wait until the next check. Increase the amount for better performance. Decrease it for faster formats.
const interval = 300 // Default: 300
// The whitelist of words that will not be formatted.
const whitelist = [
// Roman Numerals
'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X', 'XI', 'XII', 'XIII', 'XIV', 'XV', 'XVI', 'XVII', 'XVIII', 'XIX', 'XX',
// Often used abbreviations in YouTube titles
'HD', 'HQ',
// Content specific abbreviations
'ASMR', 'SMP', 'PVP', 'DIY', 'MMO', 'MMORPG', 'MOBA', 'AI', 'IRL', 'GCI',
// PC abbreviations
'PC', 'LAN', 'WIFI', 'WLAN', 'RAM', 'CPU', 'GPU', 'RTX', 'GTX', 'XP',
// Video game initals and terms
'GTA', 'FFXIV', 'WOW', 'EO', 'TESO', 'ESO',
// Company and channel initials or related terms
'WBS', 'CEO', 'YOASOBI', 'NNO',
]
/////////////// DO NOT TOUCH UNLESS YOU KNOW WHAT YOU ARE DOING ///////////////
// Format titles on front page, subscription page, recommended videos panel, etc.
waitForKeyElements("#video-title", formatVideoTitles, false, interval)
// Format titles when video is opened
waitForKeyElements("h1.ytd-watch-metadata", formatVideoTitles, false, interval)
// Format the video title in the browser tab
setInterval(() => { document.title = format(document.title) }, interval)
function formatVideoTitles(jNode) {
jNode.textContent = format(jNode.textContent)
}
function format(str) {
// Capitalize all words in CAPS, except whitelisted ones.
const words = str.match(/\b[A-Z]{2,}\b/g) ?? []
for (const word of words) {
if (whitelist.includes(word)) {
continue
}
const newWord = word.substring(0, 1).toUpperCase() + word.substring(1).toLowerCase()
str = str.replaceAll(word, newWord)
if (isDebug) {
console.debug(`%c[Clean YouTube Titles] %c[DEBUG] %c${word} → ${newWord}`, 'color: orange', 'color: aqua', 'color: inherit')
}
}
// Remove spaces before question marks and exclamation marks
str = str.replace(/\s+([?!])/g, '$1')
return str
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment