阿里云效伴侣&WorkTile

让工作更高效,让生活更美好!

目前为 2022-06-23 提交的版本。查看 最新版本

// ==UserScript==
// @name         阿里云效伴侣&WorkTile
// @version      0.1.4
// @description  让工作更高效,让生活更美好!
// @author       Jack.Chan ([email protected])
// @namespace    http://fulicat.com
// @homepage     https://greasyfork.org/zh-CN/scripts/444697
// @url          https://greasyfork.org/zh-CN/scripts/444697-%E9%98%BF%E9%87%8C%E4%BA%91%E6%95%88%E4%BC%B4%E4%BE%A3-worktile
// @license MIT
// @match        https://flow.aliyun.com/my*
// @match        https://flow.aliyun.com/all*
// @match        https://flow.aliyun.com/groups/*
// @match        https://flow.aliyun.com/pipelines/*/*
// @icon         https://flow.aliyun.com/favicon.ico
// @grant        none
// @run-at       document-end
// ==/UserScript==

(function() {
    'use strict';

    function initStyle() {
        var $style = document.createElement('style');
        $style.type = 'text/css';
        $style.innerHTML = `
.tb-navigator-container .tb-navigator-nav-inner{
    background-color: #e1f2ff !important;
    border-bottom: 1px solid #ddd;
}
.tb-common-sidebar-main-item.is-group-item .tb-common-sidebar-main-item-jsx{
    left: 10px;
    width: 185px;
}

.yx-collapse-panel.yx-collapse-panel-hidden>.yx-collapse-panel-content{
    display: block !important;
    height: auto !important;
    opacity: 1 !important;
}

.tb-navigation-avatar img{
    border: 2px solid #ffffff;
    box-shadow: 0 0 10px 8px #4caf50;
}

.yx-dialog-header.has-worktile{
    background-color: #e1f2ff !important;
    border-radius: 4px 4px 0 0 !important;
}
`;
        document.head.appendChild($style);
    }


    function doCopy(value, callback) {
        value = value === undefined || value === null ? '' : value;
        var $textarea = document.createElement('textarea');
        $textarea.style.cssText = "position:absolute;left:-20px;top: -20px;width:0;height:0;opacity: 0;";
        document.body.appendChild($textarea);
        $textarea.value = value;
        $textarea.select();
        setTimeout(() => {
            document.execCommand('Copy');
            document.body.removeChild($textarea);
            console.log('@copied:', value);
            if (typeof callback === 'function') {
                callback(value);
            }
        }, 150);
    }

    function run() {
        var pipelinesId = 0;
        var paths = window.location.pathname.split('/');
        if (paths.length > 3 && !isNaN(paths[2])) {
            pipelinesId = paths[2];

            var $yxdialog = document.querySelector('.yx-dialog');
            console.log('$yxdialog:', $yxdialog);
            var $yxdialogHeader = document.querySelector('.yx-dialog-header');
            var $Branch = document.querySelector('.yx-input>input[value="Branch"]');
            var $WorktileTask = document.querySelector('.yx-input>input[id="WorktileTask"]');
            var $BtnCancel = document.querySelector('.yx-dialog-footer>button:nth-child(1)');
            var $BtnRun = document.querySelector('.yx-dialog-footer>button:nth-child(2)');
            var isReady = $Branch && $WorktileTask && $BtnRun && $BtnRun?.innerText === '运行' && $BtnCancel;
            var $task;
            if (pipelinesId && isReady) {

                if ($yxdialogHeader) {
                    $yxdialogHeader.classList.add('has-worktile');
                }

                var hasBind = !!$BtnRun.hasBind;
                if (!hasBind) {
                    $task = document.createElement('div');
                    $task.id = 'WorktileTaskURL';
                    $Branch.parentNode.parentNode.appendChild($task);
                    $task.addEventListener('dblclick', function(event) {
                        event.preventDefault();
                        event.stopPropagation();
                        //doCopy($task.value);
                    }, false);

                    $WorktileTask.addEventListener('dblclick', function(event) {
                        $WorktileTask.select();
                    }, false);
                    $Branch.addEventListener('keydown', function(event) {
                        if ($Branch.timer) {
                            clearTimeout($Branch.timer);
                            $Branch.timer = undefined;
                        }
                    }, false);
                    $Branch.addEventListener('keyup', function(event) {
                        if ($Branch.timer) {
                            //clearTimeout($Branch.timer);
                            //$Branch.timer = undefind;
                        }

                        var branch = this.value.trim();
                        var key = 'pipelinesId:'+ pipelinesId +':'+ branch;
                        if (branch) {
                            var task = window.localStorage.getItem(key) || '';
                            console.log('@hasTask', key, task);
                            if (task) {
                                $task.innerHTML = '<p>'+ task + '</p>' + (task ? '<a id="link-do-copy" href="#" style="user-select: none;">点击复制</a>' : '');
                                var $linkDoCopy = $task.querySelector('#link-do-copy');
                                $linkDoCopy.onclick = function(evt) {
                                    evt.preventDefault();
                                    evt.stopPropagation();

                                    doCopy(task, () => {
                                        $linkDoCopy.innerText = '复制成功';
                                        setTimeout(() => {
                                            $linkDoCopy.innerText = '点击复制';
                                        }, 1500);
                                    });
                                    return false;
                                }
                            } else {
                                $task.innerHTML = '<p><small style="user-select: none;color: red;">未找到关联需求/任务,请去Worktile查找或联系产品经理</small></p>';
                            }
                            $task.value = task;
                        } else {
                            $task.value = '';
                            $task.innerHTML = '';
                        }
                    }, false);
                    $BtnCancel.addEventListener('click', function(event) {
                        var branch = $Branch.value.trim();
                        var key = 'pipelinesId:'+ pipelinesId +':'+ branch;
                        if (branch) {
                            window.localStorage.removeItem(key);
                            console.log('@removed', key);
                        }
                    }, false);
                    $BtnRun.addEventListener('click', function(event) {
                        var branch = $Branch.value.trim();
                        var task = $WorktileTask.value.trim();
                        var key = 'pipelinesId:'+ pipelinesId +':'+ branch;
                        if (branch && task) {
                            window.localStorage.setItem(key, task);
                            console.log('@saved', key, task);
                        }
                    }, false);
                    $BtnRun.hasBind = true;
                }
                console.log('hasBind:'+ hasBind);
            }
            console.log('isReady:pipelinesId:'+ pipelinesId);
        }

    }

    function init_pipelines() {

        document.body.addEventListener('mousedown', function(e) {

            var $yxdialog = document.querySelector('.yx-dialog');
            var delay = 2000;
            if ($yxdialog) {
                delay = 600;
            }
            setTimeout(() => {
                run();
            }, delay);
        }, false);
    }


    if (document.contentType.startsWith('text/html')) {

        initStyle();

        if (location.pathname.startsWith('/pipelines')) {
            init_pipelines();
        }

    }
})();