SmartPP Helper

检验分发系统异常值

Version au 04/11/2020. Voir la dernière version.

// ==UserScript==
// @name         SmartPP Helper
// @namespace    http://minhill.com
// @version      0.1
// @description  检验分发系统异常值
// @author       Hanchy Hill 
// @include      http://10.148.16.64:8080/*
// @include      http://10.148.16.63:8080/*
// @include      http://10.148.16.40:8080/*
// @grant        GM_addStyle
// ==/UserScript==

(function () {
  'use strict';

  function validateTemp(table) {
    //TODO 14时至14时 返回 中船龙穴
    //TODO 20时至20时 返回
    // var table = document.querySelector('table.text_table2');
    // console.log(table);
    // TODO remove class 再添加 class
    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/,
        }
      }
    }
    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];
    var TimeRow = null;
    var TmaxRow = null;
    var TminRow = null;
    for (let tr of tBody.children) {// 获取高低温行元素
      //console.log(tr.textContent);
      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;// 没有最高最低温直接返回
    //console.log(TimeRow);
    //console.log(TmaxRow);
    //console.log(TminRow);
    var tMaxList = [];
    var tMinList = [];
    var 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);
    }
    // console.log(tMinList);
    // console.log(tMaxList);
    // console.log(timeList);
    var initHour = NaN;
    var 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]);
    }

    // timeList[0].split(IntlWords.timeSplit)[0].includes(IntlWords.hour08) ?
    //   initHour = 8 :
    //   initHour = 20;
    // console.log(timeList[0].split(IntlWords.timeSplit));
    let validTmaxList;
    let 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;
      }
    }
    // console.log(validTmaxList);
    // console.log(validTminList);
    changeValidStatus(validTmaxList, TmaxRow);
    changeValidStatus(validTminList, TminRow);
  }

  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();

})();