add `git clone` automatically when copying ssh repo link

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

目前为 2023-11-30 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name add `git clone` automatically when copying ssh repo link
  3. // @description a simple script to `add git clone` prefix when copying ssh repo link
  4. // @update 2023/11/30 09:50:00
  5. // @namespace add-git-clone
  6. // @match *://*/*
  7. // @grant none
  8. // @version 1.0
  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.  
  16. (function() {
  17. 'use strict';
  18. function shouldPrefix(text) {
  19. return text.startsWith('git@');
  20. }
  21.  
  22. const originalWriteText = navigator.clipboard.writeText;
  23. navigator.clipboard.writeText = function(data) {
  24. if (shouldPrefix(data)) {
  25. data = 'git clone ' + data;
  26. }
  27. return originalWriteText.call(navigator.clipboard, data);
  28. };
  29.  
  30. document.addEventListener('copy', function(e) {
  31. let selection = window.getSelection().toString();
  32. if (shouldPrefix(selection)) {
  33. e.clipboardData.setData('text/plain', 'git clone ' + selection);
  34. e.preventDefault(); // 阻止默认行为
  35. }
  36. });
  37. })();