Copy URL Link

一個用來複製連結網址的userscript

  1. // ==UserScript==
  2. // @name Copy URL Link
  3. // @namespace https://github.com/nickburrows/userscript-copy-link
  4. // @description 一個用來複製連結網址的userscript
  5. // @match *://*/*
  6. // @inject-into content
  7. // @version 0.0.44
  8. // @author Nick Lin
  9. // @icon https://raw.githubusercontent.com/nickburrows/userscript-copy-link/e8f248af59bea72aeb08ded7743765ac1d6801ef/static/icon_32.png
  10. // @grant GM.setClipboard
  11. // @grant GM_setClipboard
  12. // ==/UserScript==
  13.  
  14. (function () {
  15. 'use strict';
  16.  
  17. window.addEventListener('load', () => {
  18. const evOpts = {
  19. capture: true,
  20. passive: true
  21. };
  22. let hoveredLink = null;
  23. const linkElements = document.getElementsByTagName('a');
  24. for (const link of linkElements) {
  25. link.addEventListener('mouseenter', () => {
  26. hoveredLink = link;
  27. }, evOpts);
  28. link.addEventListener('mouseleave', () => {
  29. hoveredLink = null;
  30. }, evOpts);
  31. }
  32. function eventKeyDown(ev) {
  33. if (hoveredLink && (ev.metaKey || ev.ctrlKey) && ev.key === 'c') {
  34. const linkUrl = hoveredLink.href;
  35. if (linkUrl !== null) {
  36. GM_setClipboard(linkUrl);
  37. }
  38. }
  39. }
  40. window.addEventListener('keydown', eventKeyDown, evOpts);
  41. });
  42.  
  43. })();