Claude Pasted Content Copier

Adds copy functionality to pasted content on Claude.ai

// ==UserScript==
// @name         Claude Pasted Content Copier
// @description  Adds copy functionality to pasted content on Claude.ai
// @icon         https://claude.ai/favicon.ico
// @version      1.2
// @author       afkarxyz
// @namespace    https://github.com/afkarxyz/misc-scripts/
// @supportURL   https://github.com/afkarxyz/misc-scripts/issues
// @license      MIT
// @match        *://claude.ai/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    function addCopyButton() {
        const targetHeading = document.querySelector('h2.font-styrene-display.flex-1.truncate.text-lg.font-medium');
        if (targetHeading && !targetHeading.querySelector('.copy-button')) {
            targetHeading.textContent = 'Pasted content';

            const copyButton = document.createElement('button');
            copyButton.className = `copy-button inline-flex items-center justify-center relative shrink-0 ring-offset-2 ring-offset-bg-300 ring-accent-main-100 focus-visible:outline-none focus-visible:ring-1 disabled:pointer-events-none disabled:opacity-50 disabled:shadow-none disabled:drop-shadow-none text-text-200 transition-all font-styrene active:bg-bg-400 hover:bg-bg-500/40 hover:text-text-100 h-8 w-8 rounded-md active:scale-95 ml-2`;
            copyButton.setAttribute('data-state', 'closed');
            copyButton.innerHTML = `
                <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" viewBox="0 0 256 256">
                    <path d="M200,32H163.74a47.92,47.92,0,0,0-71.48,0H56A16,16,0,0,0,40,48V216a16,16,0,0,0,16,16H200a16,16,0,0,0,16-16V48A16,16,0,0,0,200,32Zm-72,0a32,32,0,0,1,32,32H96A32,32,0,0,1,128,32Zm72,184H56V48H82.75A47.93,47.93,0,0,0,80,64v8a8,8,0,0,0,8,8h80a8,8,0,0,0,8-8V64a47.93,47.93,0,0,0-2.75-16H200Z"></path>
                </svg>
            `;
            copyButton.addEventListener('click', copyText);

            targetHeading.style.display = 'flex';
            targetHeading.style.alignItems = 'center';
            targetHeading.appendChild(copyButton);
        }
    }

    function copyText() {
        const textElement = document.querySelector('div.-m-1.mt-0.min-h-0.flex-1.whitespace-pre-wrap.break-all.text-xs.p-4.bg-bg-000.rounded-lg.border-0\\.5.border-border-300.overflow-y-auto.font-mono.shadow-sm');
        if (textElement) {
            navigator.clipboard.writeText(textElement.textContent).then(() => {
                const copyButton = document.querySelector('.copy-button');
                if (copyButton) {
                    const originalHTML = copyButton.innerHTML;
                    copyButton.innerHTML = `
                        <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" viewBox="0 0 256 256"><path d="M173.66,98.34a8,8,0,0,1,0,11.32l-56,56a8,8,0,0,1-11.32,0l-24-24a8,8,0,0,1,11.32-11.32L112,148.69l50.34-50.35A8,8,0,0,1,173.66,98.34ZM232,128A104,104,0,1,1,128,24,104.11,104.11,0,0,1,232,128Zm-16,0a88,88,0,1,0-88,88A88.1,88.1,0,0,0,216,128Z"></path></svg>
                    `;
                    setTimeout(() => {
                        copyButton.innerHTML = originalHTML;
                    }, 1000);
                }
            });
        }
    }

    function init() {
        addCopyButton();
        const observer = new MutationObserver((mutations) => {
            mutations.forEach((mutation) => {
                if (mutation.type === 'childList') {
                    addCopyButton();
                }
            });
        });
        observer.observe(document.body, { childList: true, subtree: true });
    }

    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', init);
    } else {
        init();
    }
})();