add `git clone` prefix

a simple script to `add git clone` prefix when copying ssh repo link

  1. // ==UserScript==
  2. // @name add `git clone` prefix
  3. // @description a simple script to `add git clone` prefix when copying ssh repo link
  4. // @update 2025/01/14 15:00:00
  5. // @namespace add-git-clone
  6. // @match *://*/*
  7. // @grant none
  8. // @version 1.4
  9. // @author Viki <hi@viki.moe>
  10. // @feedback-url https://github.com/vikiboss/add-git-clone/issues
  11. // @github https://github.com/vikiboss/add-git-clone
  12. // @license MIT
  13. // ==/UserScript==
  14.  
  15. ;(function () {
  16. 'use strict'
  17.  
  18. function shouldPrefix(text) {
  19. return text.startsWith('git@')
  20. }
  21.  
  22. window.addEventListener('load', function () {
  23. document.addEventListener('copy', function (e) {
  24. let selection = window.getSelection().toString()
  25.  
  26. if (shouldPrefix(selection)) {
  27. e.clipboardData.setData('text/plain', 'git clone --depth 1 ' + selection)
  28. e.preventDefault()
  29. }
  30. })
  31.  
  32. if (navigator.clipboard) {
  33. const originalWriteText = navigator.clipboard.writeText
  34.  
  35. navigator.clipboard.writeText = function (data) {
  36. if (shouldPrefix(data)) {
  37. data = 'git clone ' + data
  38. }
  39. return originalWriteText.call(navigator.clipboard, data)
  40. }
  41. }
  42. })
  43. })()