auto oa fill

maybe you dont want to fill this form manually

  1. // ==UserScript==
  2. // @name auto oa fill
  3. // @license MIT
  4. // @namespace linghao.su
  5. // @version 0.3
  6. // @description maybe you dont want to fill this form manually
  7. // @author slh001@live.cn
  8. // @match https://oa.daocloud.io/spa/workflow/static4form/index.html?*
  9. // @icon data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
  10. // ==/UserScript==
  11.  
  12. const DayWorkHour = 9;
  13.  
  14. const parseDate = (date) => {
  15. const originDateStr = `${date}`;
  16. const dateStr = `${originDateStr.slice(0, 4)}-${originDateStr.slice(
  17. 4,
  18. 6
  19. )}-${originDateStr.slice(6, 8)}`;
  20.  
  21. return dateStr;
  22. };
  23.  
  24. function promisify(fn, timeout) {
  25. return () =>
  26. new Promise((resolve) => {
  27. setTimeout(async () => {
  28. await fn();
  29. resolve();
  30. }, timeout);
  31. });
  32. }
  33.  
  34. function sleep(timeout) {
  35. return new Promise((resolve) => {
  36. setTimeout(async () => {
  37. resolve();
  38. }, timeout);
  39. });
  40. }
  41.  
  42. const project = [
  43. "应用工作台",
  44. "微服务引擎",
  45. "全局管理",
  46. "可观测性",
  47. "微服务管理",
  48. "服务网格",
  49. ];
  50.  
  51. const comp = [
  52. "镜像仓库",
  53. " Helm 应用",
  54. "服务与路由",
  55. "拓扑图",
  56. "流量监控",
  57. "灰度发布",
  58. ];
  59.  
  60. const action = ["开发", " bugfix", "性能优化", "重构"];
  61.  
  62. function getRandom(arr) {
  63. const count = arr.length;
  64. const idx = Math.floor(Math.random() * count);
  65.  
  66. return arr[idx];
  67. }
  68.  
  69. function getRandomJob() {
  70. return `${getRandom(project)}${getRandom(comp)}${getRandom(action)}`;
  71. }
  72.  
  73. let startPosition = 0;
  74.  
  75. (async function () {
  76.  
  77.  
  78. const $ = document.querySelectorAll.bind(document);
  79.  
  80.  
  81.  
  82. function getTime(offset) {
  83. const date = new Date(Date.now() - 60 * 1000 * 60 * 24 * offset);
  84. const month = date.getMonth() + 1;
  85. const day = date.getDate();
  86. const year = date.getFullYear();
  87.  
  88. return `${year}-${month}-${day}`;
  89. }
  90.  
  91. function parseSingleDate(date) {
  92. return `${Number(date.slice(0, 4))}-${Number(date.slice(4, 6))}-${Number(
  93. date.slice(6, 8)
  94. )}`;
  95. }
  96.  
  97. let isInit = false;
  98.  
  99. const init = async () => {
  100. await sleep(1000);
  101. if (isInit) {
  102. return;
  103. }
  104.  
  105. if ($(".etype_1_swapDiv")[0].children[0].innerText !== "项目周报") {
  106. return;
  107. }
  108. isInit = true;
  109. const baseMonth = window.prompt(
  110. "请输入希望填写的月份",
  111. new Date().getMonth() + 1
  112. );
  113. const baseYear = new Date().getFullYear();
  114.  
  115. startPosition = $(".icon-coms-New-schedule").length > 0 ? 1 : 0;
  116.  
  117. let targetMonth = `${baseYear}`;
  118.  
  119. if (baseMonth.length === 1) {
  120. targetMonth += "0";
  121. }
  122.  
  123. targetMonth += baseMonth;
  124.  
  125. const response = await (
  126. await fetch(
  127. `https://api.apihubs.cn/holiday/get?size=500&year=${baseYear}&month=${targetMonth}&workday=1`
  128. )
  129. ).json();
  130.  
  131. const {
  132. data: { list: dataList },
  133. } = response;
  134.  
  135. const lastDate = dataList[0];
  136.  
  137. const data = [...dataList].reverse();
  138.  
  139. const lastDateStr = parseDate(lastDate.date);
  140.  
  141. const clickCurrentDate = promisify(async () => {
  142. $(".icon-coms-New-schedule")[0].click();
  143. const key = `td[title="${lastDateStr}"]`;
  144.  
  145. await sleep(1000);
  146. $(key)[0].click();
  147. }, 1000);
  148.  
  149. // await clickCurrentDate();
  150.  
  151. const addBtn = document.getElementById("addbutton2");
  152.  
  153. const weekGroup = {};
  154.  
  155. for (let i = 0; i < data.length; i++) {
  156. const singleDay = data[i];
  157. const week = weekGroup[singleDay.yearweek] || [];
  158.  
  159. week.push(singleDay);
  160. weekGroup[singleDay.yearweek] = week;
  161. }
  162.  
  163. const weekCount = Object.keys(weekGroup).length;
  164.  
  165. for (let i = 0; i < weekCount; i++) {
  166. addBtn.click();
  167. }
  168. const weekGroupArr = Object.values(weekGroup);
  169. await sleep(1000);
  170. for (const week of weekGroupArr) {
  171. const index = weekGroupArr.indexOf(week);
  172. const weekDayCount = week.length;
  173. const weekWorkHour = weekDayCount * DayWorkHour;
  174.  
  175. $(".ant-btn-icon-only")[index].click();
  176. await sleep(2000);
  177. $(".ant-table-row-level-0")[0].click();
  178.  
  179. const timeInput = $(".wf-input-detail")[index];
  180.  
  181. timeInput.focus();
  182.  
  183. timeInput.setAttribute("value", 8);
  184. timeInput.value = weekWorkHour;
  185.  
  186. const firstDay = String(week.at(0).date);
  187. const lastDay = String(week.at(-1).date);
  188.  
  189. const firstDayParsed = parseSingleDate(firstDay);
  190. const lastDayParsed = parseSingleDate(lastDay);
  191. let timeOffset = firstDayParsed;
  192.  
  193. if (firstDayParsed !== lastDayParsed) {
  194. timeOffset += `~${lastDayParsed}`;
  195. }
  196. timeOffset += `\n${getRandomJob()}`;
  197.  
  198. $(".ant-input")[index].focus();
  199. $(".ant-input")[index].value = timeOffset;
  200.  
  201. $(".icon-coms-New-schedule")[index + startPosition].click();
  202. await sleep(500);
  203. const key = `td[title="${lastDayParsed}"]`;
  204.  
  205. $(key)[0].click();
  206. await sleep(500);
  207. }
  208. };
  209.  
  210. window.onload = async function () {
  211.  
  212. await init();
  213. };
  214.  
  215. window.autoReportInit = () => {
  216. init();
  217. };
  218.  
  219. await sleep(5000);
  220. init();
  221. // Your code here...
  222. })();
  223.