diff --git a/main.js b/main.js new file mode 100644 index 0000000..5f7fb99 --- /dev/null +++ b/main.js @@ -0,0 +1,130 @@ +// ==UserScript== +// @name 复旦微加班时间统计 +// @namespace http://start.fengbohan.com/ +// @version 2024-08-1 +// @description 统计加班总时间, 修复了周末加班时间统计的BUG +// @author bhfeng +// @match http://192.168.36.67:7888/shr/dynamic.do?uipk=com.kingdee.eas.hr.ats.app.WorkCalendarItem* +// @icon data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw== +// @grant none +// ==/UserScript== + +(function() { + 'use strict'; + function calculateTimeDifference(timeString, weekday) { + // 判断字符串是否为 "--" + if (timeString === '--') { + return 0; + } + + // 按逗号分割字符串 + const times = timeString.split(','); + + // 解析时间并转换为毫秒 + const parsedTimes = times.map(timeStr => { + const [hours, minutes, seconds] = timeStr.trim().split(':').map(Number); + return isNaN(hours) || isNaN(minutes) || isNaN(seconds) ? null : (hours * 3600 + minutes * 60 + seconds) * 1000; + }); + + // 过滤掉无效的时间 + const validTimes = parsedTimes.filter(time => time !== null); + + if (validTimes.length === 0) { + return 'No valid times provided'; + } + + // 计算最大值和最小值 + const maxTime = Math.max(...validTimes); + const minTime = Math.min(...validTimes); + + // 下午5点20分(17:20:00)的时间戳 + const fiveTwentyPM = (17 * 3600 + 20 * 60) * 1000; + let timeDifferenceInHours; + + if (weekday === "周六" || weekday == "周日") { + // 计算时间差(以小时为单位) + timeDifferenceInHours = (maxTime - minTime) / (1000 * 60 * 60); + } else { + // 计算时间差(以小时为单位) + if (maxTime >= fiveTwentyPM) { + timeDifferenceInHours = (maxTime - fiveTwentyPM) / (1000 * 60 * 60); + } else { + //timeDifferenceInHours = (fiveTwentyPM - maxTime) / (1000 * 60 * 60); + timeDifferenceInHours = 0; + } + } + + return timeDifferenceInHours; + } + + function sumOfSecondColumn(tableId) { + // 获取表格元素 + var table = document.getElementById(tableId); + + // 初始化总和 + var totalSum = 0; + + // 获取所有的行 + var rows = table.getElementsByTagName('tr'); + + // 遍历每行 + for (var i = 1; i < rows.length; i++) { // 从1开始跳过表头 + var cells = rows[i].getElementsByTagName('td'); + if (cells.length > 1) { // 确保该行至少有两列 + var cellValue = cells[3].textContent; + var weekday = cells[2].textContent; + var difference = calculateTimeDifference(cellValue, weekday); + console.log(`第${i}行:${difference} 小时`); + totalSum += difference; + + // 创建新的单元格 + var newCell2 = cells[4]; + newCell2.style="width:110px;"; + newCell2.textContent = `${difference.toFixed(2)}H`; + rows[i].appendChild(newCell2); + } + } + + return totalSum; + } + + // 创建一个新的按钮元素 + var button = document.createElement('button'); + button.style.width = "90px"; //按钮宽度 + button.style.height = "60px"; //按钮高度 + button.style.align = "center"; //文本居中 + button.style.color = "#e5e5e5"; //按钮文字颜色 + button.style.background = "#b46300"; //按钮底色 + button.style.borderRadius = "4px"; //按钮四个角弧度 + + + // 设置按钮的文字 + button.textContent = '点击我'; + + // 添加点击事件监听器 + button.addEventListener('click', function() { + var message = sumOfSecondColumn('grid').toFixed(2); + + // 使用 alert() 函数弹出提示框 + alert(`你加班了${message}小时,幸苦了!`); + + var table = document.querySelector("#gview_grid > div.ui-state-default.ui-jqgrid-hdiv > div > table > thead > tr") + var rows = table.getElementsByTagName('th'); + + // 添加表头 + var newCell = rows[4] + newCell.textContent = '加班时间'; + table.appendChild(newCell); + }); + + // 获取要将按钮添加到其中的容器 + var container = document.getElementById('seclevelmenu'); // 假设页面上有id为'container'的元素 + + // 如果没有找到容器,则添加到body元素 + if (!container) { + container = document.body; + } + + // 将按钮添加到容器中 + container.appendChild(button); +})(); \ No newline at end of file