Greasy Fork is available in English.

复制粘贴填写成绩 - zknu.edu.cn

2022.7.1

// ==UserScript==
// @name        复制粘贴填写成绩 - zknu.edu.cn
// @namespace   zknu.edu.cn
// @match       *://*.zknu.edu.cn/*
// @grant       none
// @version     2022.7.1
// @run-at      document-idle
// @author      -
// @description 2022.7.1
// @license MIT
// ==/UserScript==
(function() {
  const msg = "粘贴学号和成绩";
  const hit = "经济与管理学院周利斌15690820302";
  const chengjiinput = "input[name*=_zhcj_],input[name*=_mkcj_]";
  const re = /(\d{12})\D*[0-9\.]*\D*[0-9\.]*\D*([0-9]*)/;
  const uid = hit;

  const getwin = win => {
    if (win) {
      if (win.document.querySelector(chengjiinput)) {
        return win;
      }
      for (let i = 0; i < win.frames.length; i++) {
        const frame = getwin(win.frames[i]);
        if (frame) {
          return frame;
        }
      }
    }
  };
  const win = getwin(window);
  if (!win) return;
  const pdoc = win.parent.document;
  if (pdoc.querySelector("#" + uid)) {
    pdoc.querySelector("#" + uid).remove();
    pdoc.querySelector("#b" + uid).remove();
  }

  const dc = {};
  win.document.querySelectorAll(".datalist table tr").forEach(function(e) {
    e.querySelector("td[name=yhxh]") && (dc["xs" + e.querySelector("td[name=yhxh]").innerText] = e.querySelector(chengjiinput));
  });
  const createElement = (type, attrs, events) => {
    const ele = document.createElement(type);
    for (const attr in attrs) ele[attr] = attrs[attr];
    for (const event in events) ele.addEventListener(event, e => events[event](e));
    return ele;
  };
  const ta = createElement(
    "textarea",
    { id: uid, innerText: msg, style: "width:100%;height:125px" },
    {
      change: () => {
        const txt = ta.value;
        const dds = txt.split("\n");
        for (const eachd in dds) {
          const matchs = re.exec(dds[eachd]);
          if (matchs && matchs.length == 3 && dc["xs" + matchs[1]]) {
            dc["xs" + matchs[1]].focus();
            dc["xs" + matchs[1]].value = matchs[2];
            dc["xs" + matchs[1]].blur();
          }
        }
      }
    }
  );
  pdoc.body.firstChild.before(ta);
  pdoc.getElementById("divButton").firstChild.before(
    createElement(
      "span",
      { id: "b" + uid, style: "color:red", innerText: hit },
      {
        click: () => {
          ta.style.display = ta.style.display !== "none" ? "none" : "block";
        }
      }
    )
  );
})();