copy clicked text

Hold command and click to copy text to clipboard.

目前为 2024-03-22 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name copy clicked text
  3. // @namespace http://tampermonkey.net/
  4. // @version 2024-02-08
  5. // @description Hold command and click to copy text to clipboard.
  6. // @author You
  7. // @match *://*/*
  8. // @icon https://www.google.com/s2/favicons?sz=64&domain=stackoverflow.com
  9. // @grant none
  10. // @require https://code.jquery.com/jquery-3.6.0.min.js
  11. // @license MIT
  12. // ==/UserScript==
  13. var $ = window.jQuery;
  14.  
  15. (function() {
  16. 'use strict';
  17. $(document).ready(function() {
  18. document.addEventListener('click', (e) => {
  19. const {
  20. clientX: x,
  21. clientY: y
  22. } = e
  23.  
  24. if (e.metaKey){
  25. addBorder(e.target);
  26. copyText(e.target);
  27. e.preventDefault();
  28. }
  29. });
  30.  
  31. function copyText(element_clicked){
  32. var text = element_clicked.innerText || element_clicked.textContent;
  33. // Copy the text that was clicked
  34. navigator.clipboard.writeText(text);
  35. }
  36.  
  37. function addBorder(element_clicked){
  38. //addborder copied wholesale from: https://github.com/DanKaplanSES/copy-link-with-click/blob/master/menu.js
  39. var rect = element_clicked.getBoundingClientRect();
  40. var frame = document.createElement("div");
  41. Object.assign(frame.style, {
  42. position: "absolute",
  43. top: (rect.top + window.scrollY) + "px",
  44. left: (rect.left + window.scrollX) + "px",
  45. width: (rect.width - 4) + "px",
  46. height: (rect.height - 4) + "px",
  47. border: "solid 2px gold",
  48. borderRadius: "5px",
  49. zIndex: "99999",
  50. pointerEvents: "none"
  51. });
  52.  
  53. document.body.appendChild(frame);
  54.  
  55. $(frame).fadeIn(300, "swing").delay(500).fadeOut(500, "swing");
  56. }
  57.  
  58. });
  59. })();