GitHub PR Title Copier (Improved)

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

Du musst eine Erweiterung wie Tampermonkey, Greasemonkey oder Violentmonkey installieren, um dieses Skript zu installieren.

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

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

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

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

Sie müssten eine Skript Manager Erweiterung installieren damit sie dieses Skript installieren können

(Ich habe schon ein Skript Manager, Lass mich es installieren!)

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.

(I already have a user style manager, let me install it!)

// ==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 });
})();