auto oa fill

maybe you dont want to fill this form manually

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         auto oa fill
// @license      MIT
// @namespace    linghao.su
// @version      0.3
// @description  maybe you dont want to fill this form manually
// @author       [email protected]
// @match        https://oa.daocloud.io/spa/workflow/static4form/index.html?*
// @icon         data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// ==/UserScript==

const DayWorkHour = 9;

const parseDate = (date) => {
  const originDateStr = `${date}`;
  const dateStr = `${originDateStr.slice(0, 4)}-${originDateStr.slice(
    4,
    6
  )}-${originDateStr.slice(6, 8)}`;

  return dateStr;
};

function promisify(fn, timeout) {
  return () =>
    new Promise((resolve) => {
      setTimeout(async () => {
        await fn();
        resolve();
      }, timeout);
    });
}

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

const project = [
  "应用工作台",
  "微服务引擎",
  "全局管理",
  "可观测性",
  "微服务管理",
  "服务网格",
];

const comp = [
  "镜像仓库",
  " Helm 应用",
  "服务与路由",
  "拓扑图",
  "流量监控",
  "灰度发布",
];

const action = ["开发", " bugfix", "性能优化", "重构"];

function getRandom(arr) {
  const count = arr.length;
  const idx = Math.floor(Math.random() * count);

  return arr[idx];
}

function getRandomJob() {
  return `${getRandom(project)}${getRandom(comp)}${getRandom(action)}`;
}

let startPosition = 0;

(async function () {


  const $ = document.querySelectorAll.bind(document);



  function getTime(offset) {
    const date = new Date(Date.now() - 60 * 1000 * 60 * 24 * offset);
    const month = date.getMonth() + 1;
    const day = date.getDate();
    const year = date.getFullYear();

    return `${year}-${month}-${day}`;
  }

  function parseSingleDate(date) {
    return `${Number(date.slice(0, 4))}-${Number(date.slice(4, 6))}-${Number(
      date.slice(6, 8)
    )}`;
  }

  let isInit = false;

  const init = async () => {
    await sleep(1000);
    if (isInit) {
      return;
    }

    if ($(".etype_1_swapDiv")[0].children[0].innerText !== "项目周报") {
      return;
    }
    isInit = true;
    const baseMonth = window.prompt(
      "请输入希望填写的月份",
      new Date().getMonth() + 1
    );
    const baseYear = new Date().getFullYear();

    startPosition = $(".icon-coms-New-schedule").length > 0 ? 1 : 0;

    let targetMonth = `${baseYear}`;

    if (baseMonth.length === 1) {
      targetMonth += "0";
    }

    targetMonth += baseMonth;

    const response = await (
      await fetch(
        `https://api.apihubs.cn/holiday/get?size=500&year=${baseYear}&month=${targetMonth}&workday=1`
      )
    ).json();

    const {
      data: { list: dataList },
    } = response;

    const lastDate = dataList[0];

    const data = [...dataList].reverse();

    const lastDateStr = parseDate(lastDate.date);

    const clickCurrentDate = promisify(async () => {
      $(".icon-coms-New-schedule")[0].click();
      const key = `td[title="${lastDateStr}"]`;

      await sleep(1000);
      $(key)[0].click();
    }, 1000);

    // await clickCurrentDate();

    const addBtn = document.getElementById("addbutton2");

    const weekGroup = {};

    for (let i = 0; i < data.length; i++) {
      const singleDay = data[i];
      const week = weekGroup[singleDay.yearweek] || [];

      week.push(singleDay);
      weekGroup[singleDay.yearweek] = week;
    }

    const weekCount = Object.keys(weekGroup).length;

    for (let i = 0; i < weekCount; i++) {
      addBtn.click();
    }
    const weekGroupArr = Object.values(weekGroup);
    await sleep(1000);
    for (const week of weekGroupArr) {
      const index = weekGroupArr.indexOf(week);
      const weekDayCount = week.length;
      const weekWorkHour = weekDayCount * DayWorkHour;

      $(".ant-btn-icon-only")[index].click();
      await sleep(2000);
      $(".ant-table-row-level-0")[0].click();

      const timeInput = $(".wf-input-detail")[index];

      timeInput.focus();

      timeInput.setAttribute("value", 8);
      timeInput.value = weekWorkHour;

      const firstDay = String(week.at(0).date);
      const lastDay = String(week.at(-1).date);

      const firstDayParsed = parseSingleDate(firstDay);
      const lastDayParsed = parseSingleDate(lastDay);
      let timeOffset = firstDayParsed;

      if (firstDayParsed !== lastDayParsed) {
        timeOffset += `~${lastDayParsed}`;
      }
      timeOffset += `\n${getRandomJob()}`;

      $(".ant-input")[index].focus();
      $(".ant-input")[index].value = timeOffset;

      $(".icon-coms-New-schedule")[index + startPosition].click();
      await sleep(500);
      const key = `td[title="${lastDayParsed}"]`;

      $(key)[0].click();
      await sleep(500);
    }
  };

  window.onload = async function () {

    await init();
  };

  window.autoReportInit = () => {
    init();
  };

    await sleep(5000);
    init();
  // Your code here...
})();