// ==UserScript== // @name YouTube Link Title Converter // @namespace http://tampermonkey.net/ // @version 1.0 // @description Replace YouTube links with their titles // @include *://soyjak.party/* // @include *://soyjak.st/* // @grant GM_xmlhttpRequest // @grant GM_addStyle // ==/UserScript== (function() { 'use strict'; // Updated Regex to match YouTube URLs including shorts, mobile, and live formats const youtubeRegExp = /^(?:https?:\/\/)?(?:www\.)?(?:m\.)?(?:youtube\.com\/(?:shorts\/|live\/|(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|.*[?&]v=))|youtu\.be\/)([a-zA-Z0-9_-]{11})/; // Function to fetch video title using YouTube oEmbed API function fetchTitle(videoId) { const apiUrl = `https://www.youtube.com/oembed?url=https%3A//www.youtube.com/watch?v=${videoId}&format=json`; return new Promise((resolve, reject) => { GM_xmlhttpRequest({ method: "GET", url: apiUrl, onload: function(response) { if (response.status === 200) { const data = JSON.parse(response.responseText); resolve(data.title); } else { reject('Error fetching title'); } }, onerror: function() { reject('Network error'); } }); }); } // Function to update the link display function updateLink(link, title) { link.innerHTML = `[YouTube] ${title}`; link.title = link.href; // Show original link on hover } // Add custom CSS for YouTube link styling GM_addStyle(` a[href*="youtube.com"]::before, a[href*="youtu.be"]::before { content: ""; background: transparent url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAB4AAAAUCAYAAACaq43EAAAAo0lEQVR4AWNABv8ZjBWAuACIG4B4PRDvx4LvA/F/NHwem1qoGQ1QMxUYsAGo5H8a4wJ0Sx1AEnTCDsgWz6ejxfORLd5PR4v3wy0mSaOCL6UWvyfP4v1n/v9fv58iB5BtMRi8//T/f8PM//8FHOhqMQLcf/b/f0AxnS1GAIi4QeRwtxgR1MM6cVGenQa6AKF/kTnQlQT9q8UBbQhQq+lzn9SmDwBt39YrPgtlogAAAABJRU5ErkJggg==') center left no-repeat; background-size: contain; /* Prevents stretching */ padding-left: 18px; height: 11px; /* Adjust height as needed */ display: inline-block; vertical-align: middle; margin-top: -1.2px; /* Adjust to position */ } `); // On page load, process YouTube links window.addEventListener('load', () => { const links = document.querySelectorAll('a[href*="youtube.com/watch"], a[href*="youtube.com/shorts"], a[href*="youtu.be"], a[href*="youtube.com/live"]'); links.forEach(link => { const match = link.href.match(youtubeRegExp); if (match) { const videoId = match[1]; fetchTitle(videoId) .then(title => updateLink(link, title)) .catch(error => console.error(error)); } }); }); })();