GitHub Copy Ticket Ref

Adds a button next to issue actions to copy issue ref (e.g. org/repo#123)

目前为 2025-04-09 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name GitHub Copy Ticket Ref
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.0
  5. // @description Adds a button next to issue actions to copy issue ref (e.g. org/repo#123)
  6. // @author Ryan Allison
  7. // @match https://github.com/*/*/issues/*
  8. // @grant GM_setClipboard
  9. // @license MIT
  10. // ==/UserScript==
  11.  
  12. (function () {
  13. 'use strict';
  14.  
  15. const insertButton = () => {
  16. if (document.getElementById('copy-ticket-ref-btn')) return;
  17.  
  18. const pathMatch = window.location.pathname.match(/^\/([^\/]+)\/([^\/]+)\/issues\/(\d+)/);
  19. if (!pathMatch) return;
  20.  
  21. const [_, owner, repo, issueNum] = pathMatch;
  22. const issueRef = `${owner}/${repo}#${issueNum}`;
  23.  
  24. // Find the GitHub issue action button container
  25. const actionContainer = document.querySelector('[data-component="PH_Actions"] .Box-sc-g0xbh4-0');
  26. if (!actionContainer) return;
  27.  
  28. const button = document.createElement('button');
  29. button.id = 'copy-ticket-ref-btn';
  30. button.textContent = '📋 Copy Ticket Ref';
  31. button.className = 'prc-Button-ButtonBase-c50BI'; // Match GitHub style
  32. button.setAttribute('data-size', 'medium');
  33. button.setAttribute('data-variant', 'invisible');
  34. button.style.marginLeft = '8px';
  35.  
  36. button.onclick = () => {
  37. navigator.clipboard.writeText(issueRef);
  38. button.textContent = '✅ Copied!';
  39. setTimeout(() => {
  40. button.textContent = '📋 Copy Ticket Ref';
  41. }, 2000);
  42. };
  43.  
  44. actionContainer.appendChild(button);
  45. };
  46.  
  47. // Observe DOM for GitHub SPA navigation
  48. const observer = new MutationObserver(() => {
  49. insertButton();
  50. });
  51.  
  52. observer.observe(document.body, {
  53. childList: true,
  54. subtree: true,
  55. });
  56.  
  57. insertButton();
  58. })();