add `git clone` prefix

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

当前为 2023-12-06 提交的版本,查看 最新版本

  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 2023/11/30 09:50:00
  5. // @namespace add-git-clone
  6. // @match *://*/*
  7. // @grant none
  8. // @version 1.2
  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. if (navigator.clipboard) {
  23. const originalWriteText = navigator.clipboard.writeText;
  24. navigator.clipboard.writeText = function(data) {
  25. if (shouldPrefix(data)) {
  26. data = 'git clone ' + data;
  27. }
  28. return originalWriteText.call(navigator.clipboard, data);
  29. };
  30. }
  31.  
  32. document.addEventListener('copy', function(e) {
  33. let selection = window.getSelection().toString();
  34. if (shouldPrefix(selection)) {
  35. e.clipboardData.setData('text/plain', 'git clone ' + selection);
  36. e.preventDefault();
  37. }
  38. });
  39. })();