Copy Link and Button Text on Drag

Copy the text of a link or button to the clipboard when dragged slightly

נכון ליום 11-06-2024. ראה הגרסה האחרונה.

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

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.

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           Copy Link and Button Text on Drag
// @description    Copy the text of a link or button to the clipboard when dragged slightly
// @description:de Kopiere den Text eines Links oder Buttons in die Zwischenablage, wenn er leicht gezogen wird
// @description:ru Копировать текст ссылки или кнопки в буфер обмена при небольшом перетаскивании
// @description:uk Скопіювати текст посилання або кнопки в буфер обміну при невеликому перетягуванні
// @description:zh 在轻微拖动时将链接或按钮文本复制到剪贴板
// @description:ja リンクやボタンを少しドラッグすると、テキストをクリップボードにコピーします
// @description:nl Kopieer de tekst van een link of knop naar het klembord wanneer deze licht wordt gesleept
// @description:pt Copiar o texto de um link ou botão para a área de transferência quando arrastado ligeiramente
// @description:es Copiar el texto de un enlace o botón al portapapeles cuando se arrastra ligeramente
// @description:it Copia il testo di un collegamento o pulsante negli appunti quando viene trascinato leggermente
// @description:ar نسخ نص الرابط أو الزر إلى الحافظة عند سحبه قليلاً
// @description:fr Copier le texte d'un lien ou d'un bouton dans le presse-papiers lorsqu'il est légèrement glissé
// @description:pl Skopiuj tekst linku lub przycisku do schowka po lekkim przeciągnięciu
// @description:hi लिंक या बटन के टेक्स्ट को थोड़ा खींचने पर क्लिपबोर्ड पर कॉपी करें
// @description:bn লিঙ্ক বা বোতামের পাঠ্য সামান্য টেনে ক্লিপবোর্ডে কপি করুন
// @description:ko 링크나 버튼을 약간 드래그하면 텍스트를 클립보드에 복사합니다
// @description:vi Sao chép văn bản của liên kết hoặc nút vào bảng tạm khi kéo nhẹ
// @description:tr Bir bağlantının veya düğmenin metnini hafifçe sürüklediğinizde panoya kopyalayın
// @description:th คัดลอกข้อความของลิงก์หรือปุ่มไปยังคลิปบอร์ดเมื่อถูกลากเบา ๆ
// @icon           https://ide.onl/img/script/copylinkdrag.png
// @namespace      https://ide.onl/scripts/30-kopirovanie-teksta-ssylok-s-pomoschju-tampermonkey.html
// @version        1.2
// @match          *://*/*
// @grant          none
// @author         Sitego
// @license        MIT
// ==/UserScript==

(function() {
    'use strict';

    let startX, startY, dragging = false, targetElement = null;

    document.addEventListener('mousedown', function(event) {
        if (event.target.tagName.toLowerCase() === 'a' || event.target.tagName.toLowerCase() === 'button') {
            startX = event.clientX;
            startY = event.clientY;
            dragging = true;
            targetElement = event.target;
        }
    });

    document.addEventListener('mousemove', function(event) {
        if (dragging) {
            const distance = Math.sqrt(Math.pow(event.clientX - startX, 2) + Math.pow(event.clientY - startY, 2));
            if (distance > 5) { // Consider it a drag if moved more than 5 pixels
                if (targetElement) {
                    const elementText = targetElement.textContent.trim();
                    navigator.clipboard.writeText(elementText).then(() => {
                        console.log('Text copied to clipboard:', elementText);
                    }).catch(err => {
                        console.error('Could not copy text: ', err);
                    });
                    dragging = false;
                    targetElement = null;
                }
            }
        }
    });

    document.addEventListener('mouseup', function() {
        dragging = false;
        targetElement = null;
    });

    document.addEventListener('mouseleave', function() {
        dragging = false;
        targetElement = null;
    });
})();