Gitlab Username Replicator

Conveniently copy username in gitlab page

  1. // ==UserScript==
  2. // @name Gitlab Username Replicator
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.0
  5. // @description Conveniently copy username in gitlab page
  6. // @author luzhiyuan.deer
  7. // @match *gitlab.com/*
  8. // @match *jihulab.com/*
  9. // @icon https://www.google.com/s2/favicons?sz=64&domain=jihulab.com
  10. // @grant none
  11. // @license MIT
  12. // ==/UserScript==
  13. var copyBtnClass = "copy-text-btn";
  14.  
  15. (function() {
  16. 'use strict';
  17. setInterval(function() {initCopyBtn()}, 1000);
  18. addCssStyle(`.${copyBtnClass} {cursor: pointer; color: #666}`);
  19. })();
  20.  
  21. function initCopyBtn() {
  22. [...document.getElementsByClassName("author-username")].forEach(function(userNameElement){
  23. var btnAlreadyExist = userNameElement.getElementsByClassName(copyBtnClass).length > 0;
  24. if (btnAlreadyExist) { return; }
  25.  
  26. var copyBtn = createCopyBtn();
  27. userNameElement.appendChild(copyBtn);
  28.  
  29. copyBtn.addEventListener("click", function() {
  30. var username = userNameElement.getElementsByClassName("author-username-link")[0].textContent;
  31. doCopy(username);
  32. copyBtn.textContent = "[done]";
  33. setTimeout(function() {
  34. copyBtn.textContent = "[copy]";
  35. }, 300);
  36. })
  37. });
  38.  
  39. [...document.getElementsByClassName("author-name-link")].forEach(function(userNameElement){
  40. var btnAlreadyExist = userNameElement.parentNode.getElementsByClassName(copyBtnClass).length > 0;
  41. if (btnAlreadyExist) { return; }
  42.  
  43. var copyBtn = createCopyBtn();
  44. userNameElement.appendChild(copyBtn);
  45. appendBrother(copyBtn, userNameElement);
  46.  
  47. copyBtn.addEventListener("click", function() {
  48. var username = "@" + userNameElement.getAttribute("data-username");
  49. doCopy(username);
  50. copyBtn.textContent = "[done]";
  51. setTimeout(function() {
  52. copyBtn.textContent = "[copy]";
  53. }, 300);
  54. })
  55. });
  56.  
  57. [...document.querySelectorAll(".is-merge-request .js-user-link")].forEach(function(userNameElement){
  58. var btnAlreadyExist = userNameElement.parentNode.getElementsByClassName(copyBtnClass).length > 0;
  59. if (btnAlreadyExist) { return; }
  60.  
  61. var copyBtn = createCopyBtn(" [copy]");
  62. userNameElement.appendChild(copyBtn);
  63. appendBrother(copyBtn, userNameElement);
  64.  
  65. copyBtn.addEventListener("click", function() {
  66. var hrefSplited = userNameElement.getAttribute("href").split("/");
  67. var username = "@" + hrefSplited[hrefSplited.length - 1];
  68. doCopy(username);
  69. copyBtn.textContent = " [done]";
  70. setTimeout(function() {
  71. copyBtn.textContent = " [copy]";
  72. }, 300);
  73. })
  74. });
  75. }
  76.  
  77. function createCopyBtn(text="[copy]") {
  78. var copyBtn = document.createElement("span");
  79. copyBtn.textContent = text;
  80. copyBtn.setAttribute("class", copyBtnClass);
  81. return copyBtn;
  82. }
  83.  
  84. function doCopy(text) {
  85. var textareaForCopy = document.createElement("textarea");
  86. textareaForCopy.textContent = text;
  87. textareaForCopy.style.position = "fixed";
  88. document.body.appendChild(textareaForCopy);
  89. textareaForCopy.select();
  90. document.execCommand("copy");
  91. document.body.removeChild(textareaForCopy);
  92. }
  93.  
  94. function addCssStyle(newStyle) {
  95. var styleElement = document.getElementById('styles_js');
  96.  
  97. if (!styleElement) {
  98. styleElement = document.createElement('style');
  99. styleElement.type = 'text/css';
  100. styleElement.id = 'styles_js';
  101. document.getElementsByTagName('head')[0].appendChild(styleElement);
  102. }
  103.  
  104. styleElement.appendChild(document.createTextNode(newStyle));
  105. }
  106.  
  107. function appendBrother(newElement, targetElement) {
  108. if(targetElement.nextSibling){
  109. targetElement.parentNode.insertBefore(newElement, targetElement.nextSibling);
  110. }else{
  111. targetElement.parentNode.appendChild(newElement);
  112. }
  113. }