Greasy Fork

Greasy Fork is available in English.

生成上线周知

try to take over the world!

当前为 2018-11-09 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         生成上线周知
// @namespace    http://tampermonkey.net/
// @version      0.4
// @description  try to take over the world!
// @author       pujiaxun
// @match        https://km.sankuai.com/*
// @grant        GM_setClipboard
// ==/UserScript==

const BUTTON_ID = "SXZZ-GenDeployNoticeButton";

(function() {
  "use strict";
  window.addEventListener(
    "load",
    function(e) {
      setTimeout(init, 2000);
    },
    true
  );
})();

function observeIt() {
  const targetNode = document.getElementById("page-wrapper");
  const config = { childList: true, characterData: true };
  const observer = new MutationObserver(mutationList => {
    refresh();
  });

  observer.observe(targetNode, config);
}

/**
 * 初始化,添加页面切换的observer,并refresh按钮
 */
function init() {
  observeIt();
  refresh();
}

/**
 * 清除按钮,并判断是否需要添加按钮
 */
function refresh() {
  flush();
  if (hasDeployPlan()) {
    create();
  }
}

/**
 * 清除按钮
 */
function flush() {
  const existBtn = document.getElementById(BUTTON_ID);
  if (existBtn) {
    existBtn.parentNode.removeChild(existBtn);
  }
}

/**
 * 插入一个按钮,用来一键生成上线周知
 */
function create() {
  const d = document.createElement("div");
  d.innerHTML = `<button style="position: fixed; right: 40px; bottom: 150px" class="ant-btn ant-btn-danger" id="${BUTTON_ID}"><span>生成前端上线周知</span></button>`;

  d.addEventListener("click", genDeployNotice);
  document.body.appendChild(d);
}

/**
 * 生成上线周知,并复制到系统剪贴板
 */
function genDeployNotice() {
  const textList = parseHtml();
  const noticeItems = [
    `【上线项目】:${textList[1]}`,
    `【上线内容】:${textList[0]}`,
    `【上线时间】:${textList[6]}`,
    `【影响范围】:${textList[2]}`,
    `【上线计划】:${location.href}`
  ];
  const result = noticeItems.join("\n");
  GM_setClipboard(result, { type: "text" });
}

/**
 * 判断是否为上线方案页面
 */
function hasDeployPlan() {
  return !!document.querySelector('a[title="境外度假终端上线流程规范"]');
}

/**
 * 解析HTML,获取表格最后一行的所有单元格
 */
function parseHtml() {
  const trs = document.querySelectorAll("table tbody tr");
  const tds = trs[trs.length - 1].querySelectorAll("td");
  const tdContentList = Array.map(tds, td => td.textContent);
  return tdContentList;
}