GitHub PR Title Copier (Improved)

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

คุณจะต้องติดตั้งส่วนขยาย เช่น Tampermonkey, Greasemonkey หรือ Violentmonkey เพื่อติดตั้งสคริปต์นี้

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

คุณจะต้องติดตั้งส่วนขยาย เช่น Tampermonkey หรือ Violentmonkey เพื่อติดตั้งสคริปต์นี้

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.

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

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

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