Copy SSH URL Button for github.com

Adds a button to copy SSH clone URL on GitHub repositories when logged out.

As of 2024-06-05. See the latest version.

  1. // ==UserScript==
  2. // @name Copy SSH URL Button for github.com
  3. // @namespace Violentmonkey Scripts
  4. // @match https://github.com/*/*
  5. // @grant none
  6. // @version 1.0
  7. // @author Matthew Hugley
  8. // @description Adds a button to copy SSH clone URL on GitHub repositories when logged out.
  9. // @license BSD 3-Clause
  10. // ==/UserScript==
  11.  
  12. function addCopySSHButton() {
  13. const button = document.createElement("button");
  14. button.textContent = "Copy SSH URL";
  15. button.style.cssText = `
  16. position: absolute;
  17. left: 50%;
  18. top: 2%;
  19. transform: translateX(-50%);
  20. z-index: 1000;
  21. padding: 10px 20px;
  22. border: none;
  23. border-radius: 5px;
  24. background-color: #007bff;
  25. color: white;
  26. cursor: pointer;
  27. box-shadow: 0 2px 5px rgba(0,0,0,0.2);
  28. `;
  29.  
  30. button.onclick = function () {
  31. const repoUrl = window.location.href;
  32. const sshUrl = repoUrl.replace("https://github.com/", "git@github.com:").replace(/\/$/, "") + ".git";
  33. navigator.clipboard.writeText(sshUrl).then(
  34. function () {
  35. alert("SSH URL copied to clipboard:\n" + sshUrl);
  36. },
  37. function (err) {
  38. console.error("Could not copy SSH URL:", err);
  39. }
  40. );
  41. };
  42.  
  43. document.body.appendChild(button);
  44. }
  45.  
  46. addCopySSHButton();