SmartPP Helper

增强分发系统功能,检验分发系统异常值

Fra og med 04.11.2020. Se den nyeste version.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         SmartPP Helper
// @namespace    http://minhill.com
// @version      0.2
// @description  增强分发系统功能,检验分发系统异常值
// @author       Minhill
// @include      http://10.148.16.64:8080/*
// @include      http://10.148.16.63:8080/*
// @include      http://10.148.16.40:8080/*
// @grant        GM_addStyle
// @supportURL  https://greasyfork.org/scripts/415457
// @homepage    https://greasyfork.org/zh-CN/scripts/415457
// ==/UserScript==

(function () {
  'use strict';
  let config = {
    keyword: {
      CN: {
        tMax: '最高温度',
        tMin: '最低温度',
        fcTime: '预报时效',
        timeSplit: '至',
        hour08: '08时',
        hourReg: /(\d{2})时/,
      },
      EN: {
        tMax: 'Maximum Temperature',
        tMin: 'Minimum Temperature',
        fcTime: 'Forecast Period',
        timeSplit: ' - ',
        hour08: '08:00',
        hourReg: /(\d{2}):00/,
      }
    }
  }
  /**
   * 
   * @param {Object} table 表 Html DOM
   */
  function validateTemp(table) {

    let IntlWords;
    if (table.textContent.includes('预报时效')) {
      // 中文
      IntlWords = config.keyword.CN;
    }
    else if (table.textContent.includes('Forecast')) {// 英文
      IntlWords = config.keyword.EN;
    } else {
      console.log('未识别语言');
      return;
    }

    var tBody = table.children[0];
    let TimeRow, TmaxRow, TminRow;
    for (let tr of tBody.children) {// 获取高低温行元素
      if (tr.textContent.includes(IntlWords.fcTime)) TimeRow = tr;// Forecast Period
      if (tr.textContent.includes(IntlWords.tMax)) TmaxRow = tr;// Maximum Temperature
      if (tr.textContent.includes(IntlWords.tMin)) TminRow = tr;// Minimum Temperature
    }
    if (!TmaxRow) return;// 没有最高最低温直接返回
    let [tMaxList, tMinList, timeList] = [ [], [], [] ];
    for (let index = 1; index < TmaxRow.children.length; index++) {// 导入高低温数据
      tMinList.push(Number(TminRow.children[index].textContent));
      tMaxList.push(Number(TmaxRow.children[index].textContent));
      timeList.push(TimeRow.children[index].textContent);
    }

    let initHour = NaN,endHour = NaN;
    var timePeroidList = timeList[0].split(IntlWords.timeSplit);
    if (timePeroidList.length === 1) throw new Error('未识别的日期分隔符');
    let initHourMatched = timePeroidList[0].match(IntlWords.hourReg);
    if (initHourMatched) {
      initHour = Number(initHourMatched[1]);
    } else {
      throw new Error('未识别的日期格式' + timePeroidList[0]);
    }
    let endHourMatched = timePeroidList[1].match(IntlWords.hourReg);
    if (endHourMatched) {
      endHour = Number(endHourMatched[1]);
    } else {
      throw new Error('未识别的日期格式' + timePeroidList[1]);
    }

    let validTmaxList,validTminList;
    if ((initHour === 8 && endHour === 20) || (initHour === 20 && endHour === 8)) {
      validTmaxList = new Array(tMaxList.length / 2);
      validTminList = new Array(tMaxList.length / 2);
    } else {
      console.log(`非正点时次${initHour}, ${endHour}`);
      return;
    }
    for (let i = 0; i < validTmaxList.length; i++) {// 判断每一对温度
      if (initHour === 8 && endHour === 20) {
        validTmaxList[i] = tMaxList[i * 2] > tMaxList[i * 2 + 1];
        validTminList[i] = tMinList[i * 2] > tMinList[i * 2 + 1];
      } else if (initHour === 20 && endHour === 8) {
        validTmaxList[i] = tMaxList[i * 2] < tMaxList[i * 2 + 1];
        validTminList[i] = tMinList[i * 2] < tMinList[i * 2 + 1];
      } else {
        console.log(`非正点时次${initHour}, ${endHour}`);
        return;
      }
    }
    changeValidStatus(validTmaxList, TmaxRow);
    changeValidStatus(validTminList, TminRow);
  }

  /**
   * 
   * @param {Array} validList 判断列表
   * @param {Object} tempRow 温度行元素
   */
  function changeValidStatus(validList, tempRow) {
    validList.forEach((iValid, index) => {
      if (iValid) {
        tempRow.children[1 + index * 2].classList.add("valid-success");
        tempRow.children[1 + index * 2 + 1].classList.add("valid-success");
        tempRow.children[1 + index * 2].classList.remove("valid-error");
        tempRow.children[1 + index * 2 + 1].classList.remove("valid-error");
      } else {
        tempRow.children[1 + index * 2].classList.add("valid-error");
        tempRow.children[1 + index * 2 + 1].classList.add("valid-error");
        tempRow.children[1 + index * 2].classList.remove("valid-success");
        tempRow.children[1 + index * 2 + 1].classList.remove("valid-success");
      }
    });
  }

  /**
   * 获取表格
   */
  function tableSelector() {
    var tableList = document.querySelectorAll('table.text_table2');
    for (let iTable of tableList) {
      validateTemp(iTable);
    }
  }

  /**
   * 模板部分监听器
   */
  function templateObserver() {
    const targetNode = document.getElementById('templatePreContent');// 选择需要观察变动的节点
    const config = { childList: true, subtree: false };// 观察器的配置(需要观察什么变动)
    const callback = function (mutationsList, observer) {// 当观察到变动时执行的回调函数
      tableSelector();
    };
    const observer = new MutationObserver(callback);// 创建一个观察器实例并传入回调函数
    observer.observe(targetNode, config);// 以上述配置开始观察目标节点
  }

  /**
   * 主入口函数
   */
  function main() {
    var style = '<style>.valid-success{background-color:green;}.valid-error{background-color:orange;}</style>';
    var ele = document.createElement('div');
    ele.innerHTML = style;
    document.getElementsByTagName('head')[0].appendChild(ele.firstElementChild);
    lookUpTarget();
  }

  /**
   * 处理注入问题
   */
  function lookUpTarget() {
    const targetNode = document.getElementById('templatePreContent');
    if(targetNode){
      return templateObserver()
    }else{
      console.log('未找到目标元素,等待5秒重新查找');
      return setTimeout(lookUpTarget, 5000);
    }
  }

  main();

})();