// ==UserScript==
// @name 审核任务书
// @namespace http://tampermonkey.net/
// @version 0.7
// @description 完善了比对的信息!
// @author You
// @match http://gkg.kjt.gxzf.gov.cn/egrantweb/ctrapprove/*
// @grant none
// @require http://incaseofstairs.com/jsdiff/diff.js
// ==/UserScript==
(function() {
'use strict';
// 生成按钮
var cmd = document.querySelector("#approve").parentNode;
cmd.innerHTML = ' ' + cmd.innerHTML;
cmd.children[0].addEventListener("click", myFunction);
function myFunction(){
//发送请求待审核任务书清单
var httpRequest = new XMLHttpRequest();
httpRequest.open('POST', 'http://gkg.kjt.gxzf.gov.cn/egrantweb/ctrapprove/list-grid?_search=false&rows=20&page=1&sidx=zhTitle&sord=asc&searchString=', true); //第二步:打开连接
//httpRequest.setRequestHeader("Content-type","application/x-www-form-urlencoded");
httpRequest.send('');
httpRequest.onreadystatechange = function () {
if (httpRequest.readyState == 4 && httpRequest.status == 200) {
var domParser = new DOMParser();
var xmlDoc = domParser.parseFromString(httpRequest.responseText, 'text/xml');
//根据任务书窗口链接地址中的grantCode在任务书清单中查找申报书代码
var row = xmlDoc.getElementById(window.location.href.slice(73,window.location.href.indexOf("&grantCode=")));
var codes = row.children[6].textContent.match(/\'([^\']*)\'/g);//申报书代码
//下载申报书
var httpRequest1 = new XMLHttpRequest();
var url = 'http://gkg.kjt.gxzf.gov.cn/egrantweb/proposal/view-prp?posCodeStr=';
url = url + codes[0].slice(1,-1) + '&grantCodeStr=';
url = url + codes[1].slice(1,-1) + '&subGrantCodeStr=';
url = url + codes[2].slice(1,-1) + '&';
httpRequest1.open('POST', url, true); //第二步:打开连接
httpRequest1.send('');//发送请求
httpRequest1.onreadystatechange = function () {
if (httpRequest1.readyState == 4 && httpRequest1.status == 200) {
//提取任务书类型,bMajor为true时为重大专项任务书
var bMajor = document.querySelector("body > div > div:nth-child(3) > table:nth-child(89) > tbody > tr:nth-child(3) > td > font > strong").innerText.indexOf("重大专项") > 0;
//解析申报书页面
var htmlPrp = domParser.parseFromString(httpRequest1.responseText, 'text/html');
//比对项目时间
//读取申报书中开始与结束时间(XXXX-XX-XX)按照任务书格式(XXXX年XX月XX日)生成。
var timePrp = htmlPrp.querySelector("#baseInfo > tbody:nth-child(2) > tr:nth-child(1) > td:nth-child(2)").textContent.trim() + "至" +
htmlPrp.querySelector("#baseInfo > tbody:nth-child(2) > tr:nth-child(1) > td:nth-child(4)").textContent.trim();
timePrp = timePrp.replace("-","年").replace("-","月").replace("-","日").replace("-","年").replace("-","月").replace("-","日");
var timeCtr = document.querySelector("table.table01:nth-child(117) > tbody:nth-child(2) > tr:nth-child(7) > td:nth-child(2)");
if (timePrp != timeCtr.textContent.trim()){//红色为申报书内容,绿色为任务书内容
timeCtr.innerHTML = '' + timePrp + '' + timeCtr.textContent.trim() +'';
}
//比对项目摘要
checkText(htmlPrp.querySelector("#baseInfo > tbody:nth-child(2) > tr:nth-child(7) > td:nth-child(2)"),
document.querySelector("table.table01:nth-child(117) > tbody:nth-child(2) > tr:nth-child(8) > td:nth-child(2)"));
//比对人数
if (bMajor){
checkText(htmlPrp.querySelector("#fragment-tab9 > table:nth-child(7) > tbody:nth-child(2) > tr:nth-child(1) > td:nth-child(2)"),
document.querySelector("table.table01:nth-child(124) > tbody:nth-child(2) > tr:nth-child(2) > td:nth-child(1)"));
}
else{
checkText(htmlPrp.querySelector("#fragment-tab7 > table:nth-child(7) > tbody:nth-child(2) > tr:nth-child(1) > td:nth-child(2)"),
document.querySelector("table.table01:nth-child(124) > tbody:nth-child(2) > tr:nth-child(2) > td:nth-child(1)"));
}
//比对负责人
var zh_persons = document.querySelectorAll("table#zh_persons");
var xs_persons = zh_persons[0].nextElementSibling;//人员列表标题,用于显示删除的人员
zh_persons = Array.from(zh_persons[2].querySelectorAll("tbody > tr > td:nth-child(2)"));
for(var i = 1;i < 12;i++){//比对人员各列信息
checkText(htmlPrp.querySelector("#tab > tr > td:nth-child(3) > a > span").parentNode.parentNode.parentNode.children[i + 1],
zh_persons[1].parentNode.children[i]);
}
//比对项目组成员
var prpPersons = Array.from(htmlPrp.querySelectorAll("#kfpsn > tbody > tr > td:nth-child(3) > a > span"));
zh_persons.splice(0, 2);
for(i = 0; i < prpPersons.length; i++){
for(var j = 0; j < zh_persons.length; j++){
if (isEquality(prpPersons[i].innerText,zh_persons[j].innerText)){
checkText2((parseInt(prpPersons[i].parentNode.parentNode.parentNode.children[1].innerText.trim()) + 1).toString(),
zh_persons[j].parentNode.children[0].innerText.trim(),
zh_persons[j].parentNode.children[0]);
for(var p = 1;p < 12;p++){
checkText(prpPersons[i].parentNode.parentNode.parentNode.children[p + 1],
zh_persons[j].parentNode.children[p]);
}
prpPersons.splice(i, 1);
zh_persons.splice(j, 1);
i--;
break;
}
}
}
if(prpPersons.length > 0){
var delPerson = "";
for(i = 0; i < prpPersons.length; i++){
delPerson = delPerson + prpPersons[i].innerText.trim() + ";";
}
xs_persons.innerHTML = xs_persons.innerText + '删除了' + delPerson + '';
}
for(i = 0; i < zh_persons.length; i++){
zh_persons[i].innerHTML = '' + zh_persons[i].innerText.trim() +'';
}
var ztmb = document.querySelectorAll("#baseInfo01 > tbody > tr > td")
//比对总体目标
checkText(htmlPrp.querySelector("#baseInfo > tbody > tr:nth-child(8) > td:nth-child(2)"), ztmb[0]);
//比对主要研究内容
if (bMajor){//重大专项和重点研发格式不同
checkText(htmlPrp.querySelector("#baseInfo > tbody > tr:nth-child(9) > td:nth-child(2)"), ztmb[2]);
}
else{
checkText(htmlPrp.querySelector("#baseInfo > tbody > tr:nth-child(9) > td:nth-child(2)"), ztmb[1]);
}
//比对研究解决的关键技术问题
if (bMajor){//重大专项和重点研发格式不同
checkText(htmlPrp.querySelector("#baseInfo > tbody:nth-child(2) > tr:nth-child(10) > td:nth-child(2)"), ztmb[4]);
}
//比对考核指标
var zbPrp = htmlPrp.querySelectorAll("#ndmb_list > tbody:nth-child(2) td");
var zbCtr = document.querySelectorAll("#ndmb_list > tbody:nth-child(2) td");
for (var row = 0; row < 4; row++){
for(var col = 0; col < 2; col++){
checkText(zbPrp[row * 4 + col + 2], zbCtr[row * 3 + col + 1]);
}
}
//技术指标
//checkText(htmlPrp.querySelector("#ndmb_list > tbody > tr:nth-child(2) > td:nth-child(3) > pre"), document.querySelector("#ndmb_list > tbody > tr:nth-child(2) > td:nth-child(2)"));
//经济指标
//checkText(htmlPrp.querySelector("#ndmb_list > tbody > tr:nth-child(3) > td:nth-child(3) > pre"), document.querySelector("#ndmb_list > tbody > tr:nth-child(3) > td:nth-child(2)"));
//人才指标
//checkText(htmlPrp.querySelector("#ndmb_list > tbody > tr:nth-child(4) > td:nth-child(3) > pre"), document.querySelector("#ndmb_list > tbody > tr:nth-child(4) > td:nth-child(2)"));
//其他指标
//checkText(htmlPrp.querySelector("#ndmb_list > tbody > tr:nth-child(5) > td:nth-child(3) > pre"), document.querySelector("#ndmb_list > tbody > tr:nth-child(5) > td:nth-child(2)"));
//比对进度考核指标
var jdPrp = htmlPrp.querySelectorAll("#milestones > tbody:nth-child(2) td");
var jbCtr = document.querySelectorAll("#milestones > tbody:nth-child(2) td");
for (i = 0; i < Math.min(jdPrp.length, jbCtr.length); i++){
checkText(jdPrp[i], jbCtr[i]);
}
//比对支出进度
var zcPrp = htmlPrp.querySelectorAll("#expends > tbody:nth-child(3) td");
var zcCtr = document.querySelectorAll("#expends > tbody:nth-child(4) td");
for (i = 0; i < Math.min(zcPrp.length, zcCtr.length); i++){
checkText(zcPrp[i], zcCtr[i]);
}
if (bMajor){
//比对课题任务设置
var ktPrp = htmlPrp.querySelectorAll("#ktrw_table > tbody:nth-child(3) td");
var ktCtr = document.querySelectorAll("#tasks > tbody:nth-child(3) td");
for (row = 0; row < Math.min(ktPrp.length / 8, ktCtr.length / 9); row++){
for(col = 0; col < 8; col++){
if (col < 2){
checkText(ktPrp[row * 8 + col], ktCtr[row * 9 + col]);
}
else{
checkText(ktPrp[row * 8 + col], ktCtr[row * 9 + col + 1]);
}
}
}
}
//比对经费分配
var fpPrp = htmlPrp.querySelectorAll("#fundTab3 > tbody:nth-child(2) td");
var fpCtr = document.querySelectorAll("#fund_org_list > tbody:nth-child(2) td");
for (i = 0; i < Math.min(fpPrp.length, fpCtr.length); i++){
checkText(fpPrp[i], fpCtr[i + 6]);
}
var tdPrp = htmlPrp.querySelectorAll("#cgxy > tbody td");
var tdCtr = document.querySelectorAll("#cgxy > tbody td");
for(i = 0; i < tdPrp.length; i++){
checkText(tdPrp[i], tdCtr[i]);
}
tdPrp = htmlPrp.querySelectorAll("#fundTab2 > tbody td");
tdCtr = document.querySelectorAll("#fundTab2 > tbody td");
for(i = 0; i < tdPrp.length; i++){
checkText(tdPrp[i], tdCtr[i + 6]);
}
alert("检查完毕!");
}
}
}
};
}
//忽略空白比较字符串
function isEquality(text1, text2){
return text1.replace(/\s*/g,"") == text2.replace(/\s*/g,"");
}
//比对申报书和任务书控件内容,并在任务书中显示不同
function checkText(textPrp, textCtr){
var tPrp = textPrp.innerText.trim();
var tCtr= textCtr.innerText.trim();
checkText2(tPrp, tCtr, textCtr);
}
function checkText2(tPrp, tCtr, textCtr){
//如果太短不用修订模式
if (tPrp.length < 25 && tCtr.length < 25){
if (!isEquality(tPrp,tCtr)){//红色为申报书内容,绿色为任务书内容
textCtr.innerHTML = '' + tPrp + '' + tCtr +'';
}
return;
}
//修订形式
var diff = JsDiff["diffChars"](tPrp, tCtr);
var fragment = document.createDocumentFragment();
for (var i=0; i < diff.length; i++) {
if (diff[i].added && diff[i + 1] && diff[i + 1].removed) {
var swap = diff[i];
diff[i] = diff[i + 1];
diff[i + 1] = swap;
}
var node;
if (diff[i].removed) {
node = document.createElement('del');
node.style.color = "red"
node.appendChild(document.createTextNode(diff[i].value));
} else if (diff[i].added) {
node = document.createElement('add');
node.style.color = "green"
node.appendChild(document.createTextNode(diff[i].value));
} else {
node = document.createTextNode(diff[i].value);
}
fragment.appendChild(node);
}
textCtr.textContent = '';
textCtr.appendChild(fragment);
}
})();