复制 GitHub 仓库名

在 GitHub 页面上添加一个按钮,点击后可以复制仓库名(owner/repo)

  1. // ==UserScript==
  2. // @name Copy GitHub Repo Name
  3. // @name:en Copy GitHub Repo Name
  4. // @name:zh-CN 复制 GitHub 仓库名
  5. // @name:zh-TW 複製 GitHub 倉庫名
  6. // @name:zh 複製 GitHub 倉庫名
  7. // @namespace http://tampermonkey.net/
  8. // @version 2024-05-12
  9. // @description Create a button to copy the repo name (owner/repo) on GitHub
  10. // @description:en Create a button to copy the repo name (owner/repo) on GitHub
  11. // @description:zh-CN 在 GitHub 页面上添加一个按钮,点击后可以复制仓库名(owner/repo)
  12. // @description:zh-TW 在 GitHub 頁面上添加一個按鈕,點擊後可以複製倉庫名(owner/repo)
  13. // @description:zh 在 GitHub 頁面上添加一個按鈕,點擊後可以複製倉庫名(owner/repo)
  14. // @author Elvis Mao
  15. // @match https://github.com/*
  16. // @icon https://emtech.cc/icons/apple-touch-icon.png
  17. // @grant none
  18. // @license Apache-2.0
  19. // @homepageURL https://github.com/Edit-Mr/SSS/tree/main
  20. // @supportURL https://github.com/Edit-Mr/SSS/issues
  21. // ==/UserScript==
  22.  
  23. (function () {
  24. "use strict";
  25.  
  26. let button = document.createElement("button");
  27. button.style.cssText =
  28. "margin:0; padding: 0; font-size: 1em; border:0; outline:0;background:transparent;";
  29. button.textContent = "🔗";
  30. button.addEventListener("click", function () {
  31. try {
  32. button.textContent = "✅";
  33. navigator.clipboard.writeText(
  34. location.pathname.split("/").slice(1, 3).join("/")
  35. );
  36. setTimeout(() => {
  37. button.textContent = "🔗";
  38. }, 1000);
  39. } catch (e) {
  40. button.textContent = "❌";
  41. console.error("Failed to copy repo name");
  42. }
  43. });
  44. document.querySelector("#repo-title-component strong").appendChild(button);
  45. })();