GitHub PR Title Copier (Improved)

Adds a "Copy" button next to PR title on GitHub (works on navigation)

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

You will need to install an extension such as Tampermonkey to install this script.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

You will need to install an extension such as Tampermonkey to install this script.

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

// ==UserScript==
// @name         GitHub PR Title Copier (Improved)
// @namespace    http://tampermonkey.net/
// @version      0.5
// @description  Adds a "Copy" button next to PR title on GitHub (works on navigation)
// @match        https://github.com/*/pull/*
// @grant        window.onurlchange
// @license MIT
// ==/UserScript==

(function () {
    'use strict';

    function addCopyButton() {
        const titleEl = document.querySelector('h1[data-component="PH_Title"]');
        if (!titleEl || document.querySelector('#pr-title-copy-btn')) return;

        const titleTextEl = titleEl.querySelector('span');
        if (!titleTextEl) return;

        const copyBtn = document.createElement('button');
        copyBtn.id = 'pr-title-copy-btn';
        copyBtn.textContent = '📋 Copy';
        copyBtn.className = 'btn btn-sm';
        copyBtn.style.marginLeft = '10px';
        copyBtn.style.verticalAlign = 'middle';

        copyBtn.addEventListener('click', async (e) => {
            e.preventDefault();
            try {
                const textToCopy = titleTextEl.textContent.trim();
                await navigator.clipboard.writeText(textToCopy);

                copyBtn.textContent = '✅';
                setTimeout(() => {
                    copyBtn.textContent = '📋 Copy';
                }, 1500);
            } catch (err) {
                console.error('Copy failed:', err);
            }
        });

        titleEl.appendChild(copyBtn);
    }

    document.addEventListener('turbo:load', addCopyButton);

    if (window.onurlchange === null) {
        window.addEventListener('urlchange', () => {
            setTimeout(addCopyButton, 500);
        });
    }

    addCopyButton();

    const observer = new MutationObserver(() => {
        if (!document.querySelector('#pr-title-copy-btn')) {
            addCopyButton();
        }
    });

    observer.observe(document.body, { childList: true, subtree: true });
})();