Quick Copy and Auto Working/Ready

Add buttons for copying the id/summary/link and for auto working/ready

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Quick Copy and Auto Working/Ready
// @name:zh-CN   快速复制,自动working/ready
// @namespace    http://tampermonkey.net/
// @description  Add buttons for copying the id/summary/link and for auto working/ready
// @description:zh-cn 快速复制id/summary/link,自动working/ready
// @author       Jackie
// @version      0.2
// @match        https://idart.mot.com/browse/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=mot.com
// @run-at       document-start
// @grant        GM.addStyle
// @grant        GM.log
// ==/UserScript==

(function () {
    'use strict';

    let 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, {
                    childList: true
                });
            } else if (eventListenerSupported) {
                obj.addEventListener('DOMNodeInserted', onAddCallback, false);
            }
        };
    })();
    detectElementBySelector('.issue-container', ()=>{
        addButtons();
        observeDOM(document.getElementsByClassName('issue-container')[0], /*onAdd*/ addButtons, /*onRemove*/ addButtons);
    }, 100)
})();


function addButtons() {
    addWorkingButton();
    addReadyButton();
    if(document.getElementById("copy_id")) return;
    console.log(`=======================addCopyBtn`);
    const container = document.getElementById('stalker');
    const issueKey = document.getElementById("key-val");
    const issueName = document.getElementById("summary-val");

    if(!container) return;

    const divE = document.createElement("div");
    divE.id="snackbar";
    divE.innerHTML="Copied succesfully"
    container.appendChild(divE);

    const newElement = document.createElement("li");
    const idE = document.createElement("a");
    idE.innerHTML="Copy id";
    idE.className="aui-button aui-button-primary aui-style";
    idE.id="copy_id";
    idE.onclick= (e) => {
        var snackbar = document.getElementById("snackbar");
        snackbar.className = "show";

        navigator.clipboard.writeText(issueKey.childNodes[0].data);

        setTimeout(function(){
            snackbar.className = snackbar.className.replace("show", "");
        }, 1500);
    };
    newElement.appendChild(idE);
    issueKey.parentNode.parentNode.appendChild(newElement);

    const newElement2 = document.createElement("li");
    const summaryE = document.createElement("a");
    summaryE.className="aui-button aui-button-primary aui-style";
    summaryE.innerHTML="Copy summary";
    summaryE.id="copy_summary";
    summaryE.onclick= (e) => {
        var snackbar = document.getElementById("snackbar");
        snackbar.className = "show";

        navigator.clipboard.writeText(issueName.childNodes[0].data);

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

    newElement2.appendChild(summaryE);
    issueKey.parentNode.parentNode.appendChild(newElement2);

    const newElement3 = document.createElement("li");
    const linkE = document.createElement("a");
    linkE.className="aui-button aui-button-primary aui-style";
    linkE.innerHTML="Copy link";
    linkE.id="copy_link";
    linkE.onclick= (e) => {
        var snackbar = document.getElementById("snackbar");
        snackbar.className = "show";

        navigator.clipboard.writeText("https://idart.mot.com/browse/" + issueKey.childNodes[0].data);

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

    newElement3.appendChild(linkE);
    issueKey.parentNode.parentNode.appendChild(newElement3);

    const newElement4 = document.createElement("li");
    const idSummaryE = document.createElement("a");
    idSummaryE.className="aui-button aui-button-primary aui-style";
    idSummaryE.innerHTML="Copy as git title";
    idSummaryE.id="copy_id_summary";
    idSummaryE.onclick= (e) => {
        var snackbar = document.getElementById("snackbar");
        snackbar.className = "show";

        navigator.clipboard.writeText(issueKey.childNodes[0].data + " " + issueName.childNodes[0].data);

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

    newElement4.appendChild(idSummaryE);
    issueKey.parentNode.parentNode.appendChild(newElement4);

    addStyles();
}

function addWorkingButton() {
    let oriWorkingBtn = document.querySelector('#action_id_131');
    let autoWorkingBtn = document.querySelector('#auto_working');
    if(!oriWorkingBtn || autoWorkingBtn) return;
    oriWorkingBtn.style.display = 'none';
    autoWorkingBtn = createWorkFlowButton('auto_working', 'Auto Working');
    autoWorkingBtn.onclick = ()=>{
        oriWorkingBtn.click();
        detectElementBySelector("#customfield_10572", ()=>{
            let targetDate = document.querySelector('#customfield_10572');
            targetDate.value = getLastDayOfCurrentMonth();
            let workingSubmit = document.querySelector('#issue-workflow-transition-submit');
            workingSubmit.click();
        });
    }
    oriWorkingBtn.parentElement.prepend(autoWorkingBtn);
}

function getLastDayOfCurrentMonth() {
    let date = new Date();
    let lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);
    let [_,month,day,year] = lastDay.toDateString().split(' ');
    return day + '/'+month+'/'+year.substr(2,2);
}

function addReadyButton() {
    let oriReadyBtn = document.getElementById('action_id_541');
    if(!oriReadyBtn) return;
    let parent = document.getElementById('opsbar-opsbar-transitions');
    if(!parent || document.getElementById('btn_ready')) return;
    let btnReady = createWorkFlowButton('btn_ready', 'Auto Ready');
    btnReady.onclick = ()=>{
        oriReadyBtn.click();
        // observer ready dialog
        detectElementBySelector("#customfield_10867", ()=>{
            let testsExecuted = document.querySelector('#customfield_10867');
            testsExecuted.textContent = "Test good";
            let dependentCRs = document.querySelector('#customfield_10127');
            dependentCRs.value = "NA";
            let readySubmit = document.querySelector('#issue-workflow-transition-submit');
            readySubmit.click();
        });
    }

    parent.appendChild(btnReady);
}

function createWorkFlowButton(id, text){
    let btn = document.createElement('a');
    btn.id = id;
    btn.className = "aui-button toolbar-trigger issueaction-workflow-transition";
    let span = document.createElement('span');
    span.className = "trigger-label";
    span.innerHTML = text;
    btn.appendChild(span);
    return btn;
}

function detectElementBySelector(selector, action, delay) {
    let queryAction = ()=>{
        return document.querySelector(selector);
    }

    if(queryAction()) {
        action();
    } else {
        setTimeout(()=>{
            detectElementBySelector(selector, action)
        }, delay ? delay : 200);
    }

}

function addStyles() {


    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;}
}
    `);
}

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, {
                childList: true
            });
        } else if (eventListenerSupported) {
            obj.addEventListener('DOMNodeInserted', onAddCallback, false);
        }
    };
})();