生成上线周知

try to take over the node server!

  1. // ==UserScript==
  2. // @name 生成上线周知
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.7
  5. // @description try to take over the node server!
  6. // @author pujiaxun
  7. // @match https://km.sankuai.com/*
  8. // @grant GM_setClipboard
  9. // ==/UserScript==
  10.  
  11. const BUTTON_ID = "SXZZ-GenDeployNoticeButton";
  12. const PLUGIN_NAME = "[生成上线周知插件]";
  13.  
  14. (function() {
  15. "use strict";
  16. window.addEventListener(
  17. "load",
  18. function(e) {
  19. setTimeout(init, 2000);
  20. },
  21. true
  22. );
  23. })();
  24.  
  25. /**
  26. * 监视内容DOM子元素的变化,主要用来判断是否切换页面
  27. */
  28. function observeIt() {
  29. const targetNode = document.getElementById("page-wrapper");
  30. const config = { childList: true, characterData: true };
  31. const observer = new MutationObserver(mutationList => {
  32. refresh();
  33. });
  34.  
  35. observer.observe(targetNode, config);
  36. }
  37.  
  38. /**
  39. * 初始化,添加页面切换的observer,并refresh按钮
  40. */
  41. function init() {
  42. observeIt();
  43. refresh();
  44. }
  45.  
  46. /**
  47. * 清除按钮,并判断是否需要添加按钮
  48. */
  49. function refresh() {
  50. flush();
  51. if (hasDeployPlan()) {
  52. create();
  53. }
  54. }
  55.  
  56. /**
  57. * 清除按钮
  58. */
  59. function flush() {
  60. const existBtn = document.getElementById(BUTTON_ID);
  61. if (existBtn) {
  62. existBtn.parentNode.removeChild(existBtn);
  63. }
  64. }
  65.  
  66. /**
  67. * 插入一个按钮,用来一键生成上线周知
  68. */
  69. function create() {
  70. const d = document.createElement("div");
  71. d.innerHTML = `<button style="position: fixed; right: 40px; bottom: 150px" class="ant-btn ant-btn-primary" id="${BUTTON_ID}" title="点击自动复制"><span>生成上线周知</span></button>`;
  72.  
  73. d.addEventListener("click", genDeployNotice);
  74. document.body.appendChild(d);
  75. }
  76.  
  77. /**
  78. * 生成上线周知,并复制到系统剪贴板
  79. */
  80. function genDeployNotice() {
  81. try {
  82. const textList = getValidLineDatas();
  83. const noticeItems = [
  84. `【上线项目】:${textList[1]}`,
  85. `【上线内容】:${textList[0]}`,
  86. `【上线时间】:${textList[6]}`,
  87. `【影响范围】:${textList[2]}`,
  88. `【上线计划】:${location.href}`
  89. ];
  90. const result = noticeItems.join("\n");
  91. GM_setClipboard(result, { type: "text" });
  92. message("上线周知已成功复制到剪贴板", "success");
  93. } catch (e) {
  94. message("Something Wrong", "error");
  95. console.info(PLUGIN_NAME, "报错了,我也不知道咋处理,欢迎帮忙debug");
  96. console.error(PLUGIN_NAME, e);
  97. }
  98. }
  99.  
  100. /**
  101. * 判断是否为上线方案页面
  102. */
  103. function hasDeployPlan() {
  104. return !!document.querySelector('a[title="境外度假终端上线流程规范"]');
  105. }
  106.  
  107. /**
  108. * 解析HTML,获取表格最后n行的所有单元格
  109. */
  110. function parseHtml(lastIndex = 0) {
  111. const trs = document.querySelectorAll("table tbody tr");
  112. const tds = trs[trs.length - 1 - lastIndex].querySelectorAll("td");
  113. const tdContentList = Array.map(tds, td => td.textContent);
  114. return tdContentList;
  115. }
  116.  
  117. /**
  118. * 循环遍历列表,找到最后一条有效数据
  119. */
  120. function getValidLineDatas() {
  121. const trs = document.querySelectorAll("table tbody tr");
  122. let textList = [];
  123. let lastIndex = 0;
  124. do {
  125. textList = parseHtml(lastIndex);
  126. lastIndex += 1;
  127. // 数据列表未定义或者仅包含空格则寻找上一条
  128. } while ((!textList || !textList.join('').trim()) && lastIndex < trs.length);
  129.  
  130. return textList;
  131. }
  132.  
  133. /**
  134. * 弹出消息提示框
  135. * @param {string} msg 提示信息内容
  136. * @param {string} type 提示样式类型:success|warning|error
  137. */
  138. function message(msg = "", type = "success") {
  139. const msgEl = document.createElement("div");
  140. msgEl.innerHTML = `<div data-reactroot="" class="ant-message"><span><div class="ant-message-notice"><div class="ant-message-notice-content"><div class="ant-message-custom-content ant-message-${type}"><i class="anticon anticon-check-circle"></i><span>${msg}</span></div></div></div></span></div>`;
  141. document.body.appendChild(msgEl);
  142. setTimeout(() => {
  143. msgEl.remove();
  144. }, 3000);
  145. }

QingJ © 2025

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