Gitlab Username Replicator

Conveniently copy username in gitlab page

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Gitlab Username Replicator
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Conveniently copy username in gitlab page
// @author       luzhiyuan.deer
// @match        *gitlab.com/*
// @match        *jihulab.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=jihulab.com
// @grant        none
// @license MIT
// ==/UserScript==
var copyBtnClass = "copy-text-btn";

(function() {
    'use strict';
    setInterval(function() {initCopyBtn()}, 1000);
    addCssStyle(`.${copyBtnClass} {cursor: pointer; color: #666}`);
})();

function initCopyBtn() {
    [...document.getElementsByClassName("author-username")].forEach(function(userNameElement){
        var btnAlreadyExist = userNameElement.getElementsByClassName(copyBtnClass).length > 0;
        if (btnAlreadyExist) { return; }

        var copyBtn = createCopyBtn();
        userNameElement.appendChild(copyBtn);

        copyBtn.addEventListener("click", function() {
            var username = userNameElement.getElementsByClassName("author-username-link")[0].textContent;
            doCopy(username);
            copyBtn.textContent = "[done]";
            setTimeout(function() {
                copyBtn.textContent = "[copy]";
            }, 300);
        })
    });

    [...document.getElementsByClassName("author-name-link")].forEach(function(userNameElement){
        var btnAlreadyExist = userNameElement.parentNode.getElementsByClassName(copyBtnClass).length > 0;
        if (btnAlreadyExist) { return; }

        var copyBtn = createCopyBtn();
        userNameElement.appendChild(copyBtn);
        appendBrother(copyBtn, userNameElement);

        copyBtn.addEventListener("click", function() {
            var username = "@" + userNameElement.getAttribute("data-username");
            doCopy(username);
            copyBtn.textContent = "[done]";
            setTimeout(function() {
                copyBtn.textContent = "[copy]";
            }, 300);
        })
    });

    [...document.querySelectorAll(".is-merge-request .js-user-link")].forEach(function(userNameElement){
        var btnAlreadyExist = userNameElement.parentNode.getElementsByClassName(copyBtnClass).length > 0;
        if (btnAlreadyExist) { return; }

        var copyBtn = createCopyBtn(" [copy]");
        userNameElement.appendChild(copyBtn);
        appendBrother(copyBtn, userNameElement);

        copyBtn.addEventListener("click", function() {
            var hrefSplited = userNameElement.getAttribute("href").split("/");
            var username = "@" + hrefSplited[hrefSplited.length - 1];
            doCopy(username);
            copyBtn.textContent = " [done]";
            setTimeout(function() {
                copyBtn.textContent = " [copy]";
            }, 300);
        })
    });
}

function createCopyBtn(text="[copy]") {
    var copyBtn = document.createElement("span");
    copyBtn.textContent = text;
    copyBtn.setAttribute("class", copyBtnClass);
    return copyBtn;
}

function doCopy(text) {
    var textareaForCopy = document.createElement("textarea");
    textareaForCopy.textContent = text;
    textareaForCopy.style.position = "fixed";
    document.body.appendChild(textareaForCopy);
    textareaForCopy.select();
    document.execCommand("copy");
    document.body.removeChild(textareaForCopy);
}

function addCssStyle(newStyle) {
    var styleElement = document.getElementById('styles_js');

    if (!styleElement) {
        styleElement = document.createElement('style');
        styleElement.type = 'text/css';
        styleElement.id = 'styles_js';
        document.getElementsByTagName('head')[0].appendChild(styleElement);
    }

    styleElement.appendChild(document.createTextNode(newStyle));
}

function appendBrother(newElement, targetElement) {
    if(targetElement.nextSibling){
        targetElement.parentNode.insertBefore(newElement, targetElement.nextSibling);
    }else{
        targetElement.parentNode.appendChild(newElement);
    }
}