YouTube: Copy Comment Link

You can conveniently copy the link of any comment and send it to your friend

Od 01.09.2023.. Pogledajte najnovija verzija.

// ==UserScript==
// @name         YouTube: Copy Comment Link
// @namespace    http://tampermonkey.net/
// @version      1.4
// @description  You can conveniently copy the link of any comment and send it to your friend
// @author       Grihail
// @match        *://www.youtube.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=youtube.com
// @grant        none
// @license      CC-BY
// ==/UserScript==

function addCopyLinkToHeaderAuthor() {
    const headerAuthorElements = document.querySelectorAll('#header-author.ytd-comment-renderer');

    headerAuthorElements.forEach(element => {
        const copyLink = element.querySelector('.copy-comment-link');

        if (!copyLink) {
            const ytFormattedString = element.querySelector('yt-formatted-string a');

            if (ytFormattedString) {
                const originalHref = ytFormattedString.getAttribute('href');
                const fullHref = 'https://www.youtube.com' + originalHref;

                const copyLink = document.createElement('a');
                copyLink.classList.add('copy-comment-link');
                copyLink.href = fullHref;
                copyLink.style.marginLeft = '10px';
                copyLink.style.width = '36px';
                copyLink.style.height = '36px';
                copyLink.style.borderRadius = '54%';
                copyLink.style.display = 'flex'; // Make it a flex container
                copyLink.style.alignItems = 'center'; // Center content vertically
                copyLink.style.justifyContent = 'center'; // Center content horizontally
                copyLink.innerHTML = '<svg height="24px" viewBox="0 0 24 24" width="24px"><path d="M15 5.63 20.66 12 15 18.37V14h-1c-3.96 0-7.14 1-9.75 3.09 1.84-4.07 5.11-6.4 9.89-7.1l.86-.13V5.63M14 3v6C6.22 10.13 3.11 15.33 2 21c2.78-3.97 6.44-6 12-6v6l8-9-8-9z" fill="#000"></path></svg>';

                copyLink.addEventListener('click', event => {
                    event.preventDefault();
                    navigator.clipboard.writeText(fullHref);
                });

                // Add hover and active styles
                copyLink.style.transition = 'background-color 0.3s';
                copyLink.style.backgroundColor = 'transparent';
                copyLink.style.border = 'none';

                copyLink.addEventListener('mouseenter', () => {
                    copyLink.style.backgroundColor = 'rgb(0 0 0 / 10%)';
                });

                copyLink.addEventListener('mouseleave', () => {
                    copyLink.style.backgroundColor = 'transparent';
                });

                copyLink.addEventListener('mousedown', () => {
                    copyLink.style.backgroundColor = 'rgb(0 0 0 / 20%)';
                });

                copyLink.addEventListener('mouseup', () => {
                    copyLink.style.backgroundColor = 'rgb(0 0 0 / 10%)';
                });

                element.style.alignItems = 'center';
                element.appendChild(copyLink);
            }
        }
    });
}

setInterval(addCopyLinkToHeaderAuthor, 1000);