Copy Link and Button Text on Drag

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

目前为 2024-06-11 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Copy Link and Button Text on Drag
  3. // @description Copy the text of a link or button to the clipboard when dragged slightly
  4. // @description:de Kopiere den Text eines Links oder Buttons in die Zwischenablage, wenn er leicht gezogen wird
  5. // @description:ru Копировать текст ссылки или кнопки в буфер обмена при небольшом перетаскивании
  6. // @description:uk Скопіювати текст посилання або кнопки в буфер обміну при невеликому перетягуванні
  7. // @description:zh 在轻微拖动时将链接或按钮文本复制到剪贴板
  8. // @description:ja リンクやボタンを少しドラッグすると、テキストをクリップボードにコピーします
  9. // @description:nl Kopieer de tekst van een link of knop naar het klembord wanneer deze licht wordt gesleept
  10. // @description:pt Copiar o texto de um link ou botão para a área de transferência quando arrastado ligeiramente
  11. // @description:es Copiar el texto de un enlace o botón al portapapeles cuando se arrastra ligeramente
  12. // @description:it Copia il testo di un collegamento o pulsante negli appunti quando viene trascinato leggermente
  13. // @description:ar نسخ نص الرابط أو الزر إلى الحافظة عند سحبه قليلاً
  14. // @description:fr Copier le texte d'un lien ou d'un bouton dans le presse-papiers lorsqu'il est légèrement glissé
  15. // @description:pl Skopiuj tekst linku lub przycisku do schowka po lekkim przeciągnięciu
  16. // @description:hi लिंक या बटन के टेक्स्ट को थोड़ा खींचने पर क्लिपबोर्ड पर कॉपी करें
  17. // @description:bn লিঙ্ক বা বোতামের পাঠ্য সামান্য টেনে ক্লিপবোর্ডে কপি করুন
  18. // @description:ko 링크나 버튼을 약간 드래그하면 텍스트를 클립보드에 복사합니다
  19. // @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ẹ
  20. // @description:tr Bir bağlantının veya düğmenin metnini hafifçe sürüklediğinizde panoya kopyalayın
  21. // @description:th คัดลอกข้อความของลิงก์หรือปุ่มไปยังคลิปบอร์ดเมื่อถูกลากเบา ๆ
  22. // @icon https://ide.onl/img/script/copylinkdrag.png
  23. // @namespace https://ide.onl/scripts/30-kopirovanie-teksta-ssylok-s-pomoschju-tampermonkey.html
  24. // @version 1.2
  25. // @match *://*/*
  26. // @grant none
  27. // @author Sitego
  28. // @license MIT
  29. // ==/UserScript==
  30.  
  31. (function() {
  32. 'use strict';
  33.  
  34. let startX, startY, dragging = false, targetElement = null;
  35.  
  36. document.addEventListener('mousedown', function(event) {
  37. if (event.target.tagName.toLowerCase() === 'a' || event.target.tagName.toLowerCase() === 'button') {
  38. startX = event.clientX;
  39. startY = event.clientY;
  40. dragging = true;
  41. targetElement = event.target;
  42. }
  43. });
  44.  
  45. document.addEventListener('mousemove', function(event) {
  46. if (dragging) {
  47. const distance = Math.sqrt(Math.pow(event.clientX - startX, 2) + Math.pow(event.clientY - startY, 2));
  48. if (distance > 5) { // Consider it a drag if moved more than 5 pixels
  49. if (targetElement) {
  50. const elementText = targetElement.textContent.trim();
  51. navigator.clipboard.writeText(elementText).then(() => {
  52. console.log('Text copied to clipboard:', elementText);
  53. }).catch(err => {
  54. console.error('Could not copy text: ', err);
  55. });
  56. dragging = false;
  57. targetElement = null;
  58. }
  59. }
  60. }
  61. });
  62.  
  63. document.addEventListener('mouseup', function() {
  64. dragging = false;
  65. targetElement = null;
  66. });
  67.  
  68. document.addEventListener('mouseleave', function() {
  69. dragging = false;
  70. targetElement = null;
  71. });
  72. })();