计算已批阅日志数量(广轻)

4/21/2024, 12:10:37 PM

// ==UserScript==
// @name        计算已批阅日志数量(广轻)
// @license     MIT License
// @namespace   11ze
// @match       https://my.gdip.edu.cn/*
// @grant       none
// @icon        https://zhyccommon.oss-cn-shenzhen.aliyuncs.com/commonUpload/20210715/4cac334b887d3af9/img_logo.png
// @version     1.2.0
// @author      11ze
// @description 4/21/2024, 12:10:37 PM
// ==/UserScript==

function getEol() {
  return navigator.platform.indexOf('Win') > -1 ? '\r\n' : '\n';
}

function getReviewed() {
  const table = document.querySelector('.el-table__body');

  // 获取 class="el-table_row" 的所有行
  const rows = table.getElementsByClassName('el-table__row');

  const eol = getEol();

  const records = {};
  const names = [];

  for (let i = 0; i < rows.length; i++) {
    const row = rows[i];
    const name = row
      .querySelector('td.el-table_1_column_4.el-table__cell > div > span')
      .textContent.trim();
    const total = row
      .querySelector('td.el-table_1_column_10.el-table__cell > div > span')
      .textContent.trim();
    const unReviewed = row
      .querySelector('td.el-table_1_column_12.el-table__cell > div > span')
      .textContent.trim();
    const reviewed = total - unReviewed;

    records[name] = reviewed;
    names.push(name);
  }

  const sorted = [];
  // 将 records 按名字排序
  const sortedNames = getSortColumnArray();
  for (let i = 0; i < sortedNames.length; i++) {
    const name = sortedNames[i];
    if (records[name]) {
      sorted.push({
        name: name,
        reviewed: records[name],
      });
      names.splice(names.indexOf(name), 1);
    }
  }

  for (let i = 0; i < names.length; i++) {
    const name = names[i];
    sorted.push({
      name: name,
      reviewed: records[name],
    });
  }

  const display = sorted.map((item) => {
    return item.name + ':' + item.reviewed;
  });

  const nameMatched = names.length === 0;
  const warn = nameMatched ? '(已排序)' : '(排序错误,请检查顺序)';
  const displayStr = warn + eol + '结果已放在剪切板,可在表格选中单元格后粘贴:' + eol + display.join(eol);

  // 把结果放到剪切板
  const clipboardContent = sorted.map((item) => item.reviewed).join(eol);
  navigator.clipboard.writeText(clipboardContent);
  alert(displayStr);
}

function getSortColumnInputText() {
  return localStorage.getItem('columnSort');
}

function setSortColumnInputText(text) {
  return localStorage.setItem('columnSort', text);
}

function getSortColumnArray() {
  const text = getSortColumnInputText();
  if (!text) {
    return [];
  }
  return text.split(',');
}

function showSetSortInput() {
  const lastText = localStorage.getItem('columnSort');

  // 创建一个包含多行文本输入框和提交按钮的弹窗
  const popup = document.createElement('div');
  popup.className = 'popup';

  const textarea = document.createElement('textarea');
  textarea.placeholder = '请输入学生名字,用英文逗号隔开,如:张三,李四,王五';
  textarea.id = 'set-sort-textarea'; // 设置 dom id

  textarea.rows = 5;
  textarea.cols = 100;

  if (lastText) {
    textarea.value = lastText;
  }

  popup.appendChild(textarea); // 将文本输入框添加到弹窗中

  const submitButton = document.createElement('button');
  submitButton.innerHTML = '提交';
  submitButton.onclick = function () {
    const text = textarea.value;

    setSortColumnInputText(text);

    popup.remove();

    alert('排序设置已保存');
  };
  popup.appendChild(submitButton);

  document.body.appendChild(popup);
}

// <button data-v-2349296e="" type="button" class="el-button search-sss el-button--primary" style="margin-right: 0px;"><!----><!----><span>查询</span></button>
const getReviewedButton = `<button type="button" class="el-button search-sss el-button--primary" style="margin-right: 0px;" onclick="getReviewed()"><!----><!----><span>获取已批阅日志数量</span></button>`;
const setSort = `<button type="button" class="el-button search-sss el-button--primary" style="margin-right: 0px;" onclick="showSetSortInput()"><!----><!----><span>到页面底部设置顺序</span></button>`;

window.getReviewed = getReviewed;
window.getReviewedButton = getReviewedButton;
window.showSetSortInput = showSetSortInput;

window.columnSort = []; // [name1, name2, ...]

// 定时检查按钮有没有,没有则加上
setInterval(function () {
  const targetDom = document.getElementsByClassName('el-tabs__item is-top is-active is-closable');
  if (!targetDom) {
    return;
  }
  const domName = targetDom[0].textContent;
  if (domName.indexOf('学生日志批阅') === -1) {
    return;
  }

  const buttons = document.getElementsByClassName('action-row el-row el-row--flex');
  if (!buttons) {
    return;
  }

  if (buttons[buttons.length - 1].innerHTML.indexOf(setSort) === -1) {
    buttons[buttons.length - 1].innerHTML += setSort;
  }

  if (buttons[buttons.length - 1].innerHTML.indexOf(getReviewedButton) === -1) {
    buttons[buttons.length - 1].innerHTML += getReviewedButton;
  }
}, 1000);