Easy Text Selection Copier

Adds a floating button to copy selected text (Scroll-Friendly)

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला 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.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==UserScript==
// @name         Easy Text Selection Copier
// @namespace    http://tampermonkey.net/
// @version      1.3
// @description  Adds a floating button to copy selected text (Scroll-Friendly)
// @author       You
// @match        *://*/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    let capturedText = "";

    const btn = document.createElement('button');
    btn.textContent = '📋 Copy';
    // Using cssText for cleaner styling
    btn.style.cssText = `
        position: fixed;
        padding: 8px 12px;
        background: #333;
        color: #fff;
        border: none;
        border-radius: 5px;
        cursor: pointer;
        z-index: 999999;
        display: none;
        box-shadow: 0 2px 5px rgba(0,0,0,0.3);
        font-family: sans-serif;
        pointer-events: auto;
    `;
    document.body.appendChild(btn);

    // Track selection on mouseup
    document.addEventListener('mouseup', function(e) {
        // Tiny delay to allow the selection to finalize
        setTimeout(() => {
            const selection = window.getSelection();
            const text = selection.toString().trim();
            
            if (text.length > 0) {
                capturedText = text;
                btn.style.display = 'block';
                
                // Position button relative to the viewport (fixed)
                // This ensures it stays near the cursor even if you scrolled during selection
                btn.style.left = e.clientX + 10 + 'px';
                btn.style.top = e.clientY + 10 + 'px';
            } else if (e.target !== btn) {
                btn.style.display = 'none';
            }
        }, 10);
    });

    // Prevent button from stealing focus (which unselects text)
    btn.addEventListener('mousedown', function(e) {
        e.preventDefault();
        e.stopPropagation();
    });

    btn.addEventListener('click', function(e) {
        e.stopPropagation();
        if (capturedText.length > 0) {
            navigator.clipboard.writeText(capturedText).then(() => {
                btn.textContent = '✅ Copied!';
                setTimeout(() => {
                    btn.style.display = 'none';
                    btn.textContent = '📋 Copy';
                    capturedText = "";
                }, 1000);
            });
        }
    });

    // Hide when clicking elsewhere, but NOT when scrolling
    document.addEventListener('mousedown', function(e) {
        if (e.target !== btn) {
            btn.style.display = 'none';
        }
    });

    // Optional: Update button position on scroll if you want it to "follow" the text
    // Otherwise, 'position: fixed' keeps it at the mouse-up coordinates relative to the screen.
    window.addEventListener('scroll', function() {
        if (btn.style.display === 'block') {
            // We keep it visible; user can still click it after scrolling.
        }
    }, { passive: true });

})();