monkey-yunxiao

A userscript that adds a progress bar to the task list.

当前为 2024-08-05 提交的版本,查看 最新版本

// ==UserScript==
// @name         monkey-yunxiao
// @namespace    npm/vite-plugin-monkey
// @version      0.0.1
// @author       monkey
// @description  A userscript that adds a progress bar to the task list.
// @license      MIT
// @icon         https://vitejs.dev/logo.svg
// @match        https://devops.aliyun.com/projex/project/*/task*
// @grant        GM_addStyle
// ==/UserScript==

(t=>{if(typeof GM_addStyle=="function"){GM_addStyle(t);return}const e=document.createElement("style");e.textContent=t,document.head.append(e)})(" ._status-button-progress_1wcb5_1{position:absolute;bottom:0;left:0;width:100%;height:2px;background:red;transition:width .5s} ");

(function () {
  'use strict';

  const style = {
    "status-button-progress": "_status-button-progress_1wcb5_1"
  };
  initInterceptor();
  const urlMaps = /* @__PURE__ */ new Map([
    [
      "/projex/api/workitem/workitem/list",
      (data) => {
        initInterval(".next-table-inner", (target) => {
          if (target.getAttribute("init-progress")) return;
          target.setAttribute("init-progress", "true");
          const col = findColsNum(target, "状态");
          if (col) {
            const elList = target.querySelectorAll(
              `.next-table-body tr td[data-next-table-col="${col}"] button`
            );
            elList.forEach((el, i) => {
              el.style.position = "relative";
              const div = document.createElement("div");
              div.classList.add(style["status-button-progress"]);
              el.appendChild(div);
              const dataItem = data.result[i];
              const parentIdentifier = dataItem == null ? void 0 : dataItem.parentIdentifier;
              parentIdentifier && queryParentAndUpdateEl(parentIdentifier, div, dataItem);
            });
          }
        });
      }
    ]
  ]);
  function initInterval(selector, cb) {
    const intervalId = setInterval(() => {
      const target = document.querySelector(selector);
      if (target) {
        cb(target);
        clearInterval(intervalId);
      }
    }, 1e3);
    return intervalId;
  }
  function initInterceptor() {
    const xhrOpen = window.XMLHttpRequest.prototype.open;
    window.XMLHttpRequest.prototype.open = function(...args) {
      xhrOpen.apply(this, args);
      this.addEventListener("readystatechange", function() {
        if (this.readyState === 4) {
          const path = new URL(this.responseURL).pathname;
          const handler = urlMaps.get(path);
          if (handler) {
            handler(JSON.parse(this.responseText));
          }
        }
      });
    };
  }
  function findColsNum(target, title) {
    const thList = target.querySelectorAll(
      ".next-table-header th"
    );
    for (let i = 0; i < thList.length; i++) {
      if (thList[i].innerText === title) {
        return i;
      }
    }
  }
  async function queryParentAndUpdateEl(identifier, dom, dataItem) {
    var _a;
    const res = await fetch(
      `https://devops.aliyun.com/projex/api/workitem/v2/workitem/${identifier}/relation/workitem/list/by-relation-category?category=PARENT_SUB&isForward=true&_input_charset=utf-8`,
      {
        headers: {
          "Content-Type": "application/json"
        },
        method: "GET",
        credentials: "include"
      }
    ).then((res2) => res2.json());
    const taskList = (_a = res.result) == null ? void 0 : _a.filter((item) => item.workitemTypeName === "任务").filter((item) => item.identifier !== dataItem.identifier);
    function setStyle(n) {
      dom.style.width = `${n}%`;
    }
    if (!(taskList == null ? void 0 : taskList.length)) {
      setStyle(100);
      return;
    }
    const progress = taskList.length * 100;
    let completed = 0;
    taskList.forEach((task) => {
      var _a2;
      const progressStr = (_a2 = task.fieldValueVOList.find(
        (field) => field.fieldIdentifier === "progress"
      )) == null ? void 0 : _a2.value;
      if (progressStr === "0.1") return;
      const n = Number(progressStr);
      if (n) {
        completed += n;
      }
    });
    const percent = completed / progress * 100;
    setStyle(percent);
  }

})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址