Greasy Fork镜像 还支持 简体中文。

GitHub Repo Markdown Link Copier

Add a copy button to GitHub repos to copy the repo link in markdown format

  1. // ==UserScript==
  2. // @name GitHub Repo Markdown Link Copier
  3. // @namespace https://dvel.me/github-repo-markdown-link-copier
  4. // @version 1.0
  5. // @description Add a copy button to GitHub repos to copy the repo link in markdown format
  6. // @author Dvel
  7. // @match https://github.com/*/*
  8. // @grant GM_setClipboard
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. 'use strict';
  13.  
  14. // SVG图标
  15. const copyIconSVG = `<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-clipboard" viewBox="0 0 16 16"><path d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 0 1 0 1.5h-1.5a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-1.5a.75.75 0 0 1 1.5 0v1.5A1.75 1.75 0 0 1 9.25 16h-7.5A1.75 1.75 0 0 1 0 14.25Z"></path><path d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0 1 14.25 11h-7.5A1.75 1.75 0 0 1 5 9.25Zm1.75-.25a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-7.5a.25.25 0 0 0-.25-.25Z"></path></svg>`;
  16. const checkedIconSVG = `<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-check2" viewBox="0 0 16 16"><path d="M13.854 3.646a.5.5 0 0 0-.708 0L7 9.793 3.854 6.646a.5.5 0 1 0-.708.708l3.5 3.5a.5.5 0 0 0 .708 0l6.5-6.5a.5.5 0 0 0 0-.708z"/></svg>`;
  17.  
  18. // 使用MutationObserver监听DOM变化
  19. const observer = new MutationObserver(mutations => {
  20. mutations.forEach(mutation => {
  21. if (mutation.addedNodes.length > 0 || mutation.type === 'childList') {
  22. const navList = document.querySelector('nav[role="navigation"][aria-label="Page context"] ul');
  23. const existingButton = document.querySelector('#custom-copy-button');
  24. if (navList && !existingButton) {
  25. addCopyButton();
  26. }
  27. }
  28. });
  29. });
  30.  
  31. // 观察器配置:观察子节点的变化
  32. const config = { childList: true, subtree: true };
  33.  
  34. // 监听页面加载事件,然后开始观察DOM变化
  35. window.addEventListener('load', () => {
  36. observer.observe(document.body, config);
  37. });
  38.  
  39. function addCopyButton() {
  40. // 定位到GitHub页面导航栏的ul元素
  41. const navList = document.querySelector('nav[role="navigation"][aria-label="Page context"] ul');
  42. if (!navList) return;
  43.  
  44. // 获取仓库的用户名和名称
  45. const pathParts = document.location.pathname.split('/').filter(Boolean);
  46. const repoFullName = document.location.pathname.substring(1);
  47. const repoUrl = window.location.href;
  48.  
  49.  
  50. // 创建按钮和设置样式
  51. const listItem = document.createElement('li');
  52. listItem.id = 'custom-copy-button'; // 确保按钮唯一性
  53. const copyButton = document.createElement('button');
  54. copyButton.innerHTML = `${copyIconSVG} Copy`;
  55. styleButton(copyButton); // 应用样式
  56.  
  57. // 点击按钮复制Markdown链接
  58. copyButton.onclick = function() {
  59. const markdownLink = `[${pathParts[0]}/${pathParts[1]}](${repoUrl})`;
  60. GM_setClipboard(markdownLink);
  61. copyButton.innerHTML = `${checkedIconSVG} Copied!`;
  62. setTimeout(() => { copyButton.innerHTML = `${copyIconSVG} copy`; }, 10000);
  63. };
  64.  
  65. listItem.appendChild(copyButton);
  66. navList.appendChild(listItem);
  67. }
  68.  
  69. // 应用按钮样式,模仿GitHub风格
  70. function styleButton(button) {
  71. button.style.padding = '5px 10px';
  72. button.style.fontSize = '12px';
  73. button.style.fontWeight = '600';
  74. button.style.lineHeight = '20px';
  75. button.style.color = '#24292e';
  76. button.style.backgroundColor = '#eff3f6';
  77. button.style.border = '1px solid rgba(27,31,35,.15)';
  78. button.style.borderRadius = '6px';
  79. button.style.cursor = 'pointer';
  80. button.style.marginLeft = '8px';
  81. button.style.display = 'flex';
  82. button.style.alignItems = 'center';
  83. button.style.gap = '5px';
  84.  
  85. button.onmouseover = function() {
  86. this.style.backgroundColor = '#e1e4e8';
  87. };
  88. button.onmouseout = function() {
  89. this.style.backgroundColor = '#eff3f6';
  90. };
  91. }
  92. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址