Copy a Gerrit link for querying CR And add a button to browse branch code

快捷浏览分支代码以及快捷拷贝查询CR的链接

目前為 2023-07-07 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Copy a Gerrit link for querying CR And add a button to browse branch code
// @namespace    http://tampermonkey.net/
// @version      0.4
// @description  快捷浏览分支代码以及快捷拷贝查询CR的链接
// @author       Jackie
// @match        https://gerrit.mot.com*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=mot.com
// @run-at       document-end
// @grant        GM.addStyle
// ==/UserScript==

(function() {
    'use strict';

    let action = ()=>{
        sleep(100).then(()=>{
            addButtons();
        });
    }
    //action();
    sleep(1000).then(()=>{
        let c = document.getElementById("gerrit_header");
        observeDOM(c, /*onAdd*/ action, /*onRemove*/ action);
    });
})();

function addButtons() {
    console.log(`=======================addButtons`)
    let btnCopyCrQueryLink = document.getElementById("btnCopyCrQueryLink");
    if(!btnCopyCrQueryLink){
        btnCopyCrQueryLink = createButton("CopyQueryLink");
        btnCopyCrQueryLink.id = "btnCopyCrQueryLink";
        btnCopyCrQueryLink.onclick = () => {
            let msg = "CR not found!"
            let crDiv = document.getElementsByClassName("com-google-gerrit-client-change-CommitBox_BinderImpl_GenCss_style-text")[0];
            if(crDiv && crDiv.firstElementChild){
                msg = "https://gerrit.mot.com/#/q/" + crDiv.firstElementChild.text;
            }
            navigator.clipboard.writeText(msg);
            showSnackbar(msg);
        }
        let parent = document.getElementsByClassName("com-google-gerrit-client-change-ChangeScreen_BinderImpl_GenCss_style-infoLineHeaderButtons")[0];
        if(parent) {
            parent.appendChild(btnCopyCrQueryLink);
        }
    }

    let btnBrowseBranch = document.getElementById("btnBrowseBranch");
    if(!btnBrowseBranch){
        btnBrowseBranch = createElement({
            name:"a",
            class:"com-google-gerrit-client-change-ChangeScreen_BinderImpl_GenCss_style-projectSettings",
            innerHTML: "Browse Branch"
        });
        btnBrowseBranch.target = `_blank`;
        btnBrowseBranch.id = "btnBrowseBranch";

        let branchTd = document.querySelector('#change_infoTable > tbody > tr:nth-of-type(7) > td:nth-of-type(1)');
        let branchName = branchTd.firstChild.text;
        let project = document.querySelector('#change_infoTable > tbody > tr:nth-of-type(6) > td:nth-of-type(1)').firstChild.text;
        btnBrowseBranch.href = `https://gerrit.mot.com/plugins/gitiles/${project}/+/refs/heads/${branchName}`;
        branchTd.appendChild(btnBrowseBranch);
    }

}

function showSnackbar(msg) {
    let snackbar = getSnackbar();
    snackbar.className = "show";
    setTimeout(function(){
        snackbar.className = snackbar.className.replace("show", "");
    }, 1500);
}

function getSnackbar() {
    let snackbar = document.getElementById('snackbar');
    if(!snackbar) {
        snackbar = document.createElement("div");
        snackbar.id="snackbar";
        snackbar.innerHTML="Copied succesfully"
        document.getElementsByClassName('screen')[0].appendChild(snackbar);
        GM.addStyle(`
      #snackbar {
  visibility: hidden;
  min-width: 250px;
  margin-left: -125px;
  background-color: #333;
  color: #fff;
  text-align: center;
  border-radius: 2px;
  padding: 16px;
  position: fixed;
  z-index: 1;
  left: 50%;
  top: 50px;
  font-size: 17px;
}

#snackbar.show {
  visibility: visible;
  -webkit-animation: fadein 0.5s, fadeout 0.5s 2.5s;
  animation: fadein 0.5s, fadeout 0.5s 2.5s;
}

@-webkit-keyframes fadein {
  from {top: 0; opacity: 0;}
  to {top: 50px; opacity: 1;}
}

@keyframes fadein {
  from {top: 0; opacity: 0;}
  to {top: 50px; opacity: 1;}
}

@-webkit-keyframes fadeout {
  from {top: 50px; opacity: 1;}
  to {top: 0; opacity: 0;}
}

@keyframes fadeout {
  from {top: 50px; opacity: 1;}
  to {top: 0; opacity: 0;}
}
    `);
    }
    return snackbar;
}

function createButton(text) {
    let button = createElement(
        {
            name:"button",
            class:"com-google-gerrit-client-change-ChangeScreen_BinderImpl_GenCss_style-highlight",
        }
    );
    button.type = "button";
    button.title = text;
    let div = document.createElement('div');
    div.innerHTML=text;
    button.appendChild(div);
    return button;
}

function createElement(info) {
    if(!info.name) return undefined;
    let element = document.createElement(info.name);
    if(info.innerHTML) element.innerHTML = info.innerHTML;
    if(info.class) {
        /*if(info.class.__proto__ == String.prototype) {
            element.classList.add(info.class);
        } else {
            for (let cls of info.class) {
                element.classList.add(cls);
            }
        }*/
        element.classList.add(info.class);
    }
    return element;
}

function sleep (time) {
    return new Promise((resolve) => setTimeout(resolve, time));
}

const observeDOM = (function () {
    let MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
    let eventListenerSupported = window.addEventListener;

    return function (obj, onAddCallback, onRemoveCallback) {
        if (MutationObserver) {
            // define a new observer
            let mutationObserver = new MutationObserver(function (mutations, observer) {
                if (mutations[0].addedNodes.length && onAddCallback != undefined) {
                    onAddCallback();
                }
            });
            // have the observer observe foo for changes in children
            mutationObserver.observe(obj, { attributes: true, childList: true, subtree: true });
        } else if (eventListenerSupported) {
            obj.addEventListener('DOMNodeInserted', onAddCallback, false);
        }
    };
})();