bb-check-in

bb werp 打卡計算機

  1. // ==UserScript==
  2. // @name bb-check-in
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.2
  5. // @description bb werp 打卡計算機
  6. // @author You
  7. // @match https://cy.iwerp.net/portal/page/new_home.xhtml
  8. // @icon https://www.google.com/s2/favicons?sz=64&domain=iwerp.net
  9. // @grant none
  10. // @license MIT
  11. // ==/UserScript==
  12.  
  13. function isInCurrentWeek(dateString, firstDayOfWeek) {
  14. var currentDate = parseDateString(dateString);
  15. var weekStart = firstDayOfWeek;
  16. console.log(weekStart)
  17. var weekEnd = new Date(weekStart.getFullYear(), weekStart.getMonth(), weekStart.getDate() + 6);
  18.  
  19. return currentDate >= weekStart && currentDate <= weekEnd;
  20. }
  21.  
  22. function getcurrentDayOfWeek(dateString) {
  23. var currentDate = parseDateString(dateString);
  24. return currentDate.getDay();
  25. }
  26.  
  27. function parseDateString(dateString) {
  28. var parts = dateString.split('/');
  29. var month = parseInt(parts[0], 10) - 1;
  30. // 月份需減1,因為 JavaScript 的月份是從 0 開始
  31. var day = parseInt(parts[1].split(' ')[0], 10);
  32.  
  33. var currentDate = new Date();
  34. currentDate.setMonth(month);
  35. currentDate.setDate(day);
  36.  
  37. return currentDate;
  38. }
  39.  
  40. function addMinutes(time, minutes) {
  41. var newTime = new Date(time.getTime() + minutes * 60000);
  42. return newTime;
  43. }
  44.  
  45. function formatTime(time) {
  46. var hours = time.getHours().toString().padStart(2, '0');
  47. var minutes = time.getMinutes().toString().padStart(2, '0');
  48. return hours + ':' + minutes;
  49. }
  50.  
  51. function addColor(val,text){
  52. var retxt = "";
  53. if(val<0){
  54. retxt = '<font color="#FF0000">'+ text +'少: '+val+'</font>'
  55. }else{
  56. retxt = '<font color="blue">'+ text +'多: '+val+'</font>'
  57. }
  58. return retxt;
  59. }
  60.  
  61. function testA(){
  62. setTimeout(function() {
  63. 'use strict';
  64. var today = new Date();
  65. var currentDayOfWeek = today.getDay(); // 获取今天是星期几,0表示星期日,1表示星期一,以此类推
  66.  
  67. // 将日期设置为本周第一天(星期日)
  68. today.setDate(today.getDate() - currentDayOfWeek);
  69. // 取得指定 ID 的元素
  70. var container = document.getElementById('formTemplate:attend_rec_panel_content');
  71. // 取得所有的 <tr> 元素
  72. var rows = container.getElementsByTagName('tr');
  73. // 初始化一個空陣列來儲存資料
  74. var data = [];
  75. var sumEstimateMaintain = 0;
  76. var sumEstimate = 0;
  77. var todayStart = '';
  78. // 迭代所有的 <tr> 元素
  79. for (var i = 0; i < rows.length; i++) {
  80. var row = rows[i];
  81. // 取得每一列中的 <td> 元素
  82. var cells = row.getElementsByTagName('td');
  83. // 檢查 <td> 元素是否存在
  84. if (cells.length > 0) {
  85. var rowData = {};
  86. // 存放每個 <td> 的內容到對應的鍵值
  87. rowData.strDate = cells[0].textContent.trim();
  88. rowData.strStart = cells[1].textContent.trim().split('(')[0].trim();
  89. rowData.strEnd = cells[2].textContent.trim().split('(')[0].trim();
  90. var cell1 = row.insertCell();
  91. if(isInCurrentWeek(rowData.strDate,today)&&rowData.strStart.length>0){
  92. console.log("日期:"+i +"/"+ rowData.strStart.length);
  93. var strStart = rowData.strStart;
  94. var strEnd = rowData.strEnd;
  95. var dateStart = new Date('1970-01-01 ' + strStart);
  96. var dateEnd = new Date('1970-01-01 ' + strEnd);
  97. var diffMinutes = (dateEnd - dateStart) / (1000 * 60);
  98. if (!strEnd) {
  99. rowData.diffMinutes = 0;
  100. rowData.estimate = 0;
  101. todayStart = strStart;
  102. }else{
  103. rowData.diffMinutes = diffMinutes;
  104. rowData.estimate = diffMinutes-540;
  105. }
  106. rowData.currentDayOfWeek = getcurrentDayOfWeek(rowData.strDate);
  107. // 把資料加入到陣列中
  108. if(diffMinutes!=0){
  109. cell1.textContent = rowData.estimate;
  110. }
  111. data.push(rowData);
  112. sumEstimate += rowData.estimate;
  113. if(rowData.currentDayOfWeek!=3){
  114. sumEstimateMaintain += rowData.estimate;
  115. }
  116. }
  117. }
  118. }
  119.  
  120. // 印出抓取到的資料
  121. for (var j = 0; j < data.length; j++) {
  122. console.log("日期:" + data[j].strDate);
  123. console.log("開始時間:" + data[j].strStart);
  124. console.log("結束時間:" + data[j].strEnd);
  125. console.log("分鐘數:" + sumEstimate);
  126. console.log("今天分鐘數:" + sumEstimateMaintain);
  127. console.log("星期:" + data[j].currentDayOfWeek);
  128. console.log("---");
  129. }
  130. var xx=rows[0].insertCell();
  131. xx.innerHTML = addColor(sumEstimate,'') + '<br>' + addColor(sumEstimateMaintain,'[維護]');
  132. var aa= 540 - sumEstimate;
  133. var bb= 540 - sumEstimateMaintain;
  134. var dayS = new Date('1970-01-01 ' + todayStart);
  135. var newTime = addMinutes(dayS, aa);
  136. var hasMaintainTime = addMinutes(dayS, bb);
  137. console.log(dayS);
  138. console.log(aa);
  139. console.log(newTime);
  140. var tbody = document.getElementById('formTemplate:attend_rec_datatable_data');
  141. var newRow = document.createElement('tr');
  142. var cell101 = document.createElement('td');
  143. var cell102 = document.createElement('td');
  144. var cell11 = document.createElement('td');
  145. var cell12 = document.createElement('td');
  146. cell102.textContent = "幾點可走";
  147. cell11.textContent = "正常:" + formatTime(newTime);
  148. cell12.textContent = "維護:" + formatTime(hasMaintainTime);
  149. newRow.appendChild(cell101);
  150. newRow.appendChild(cell102);
  151. newRow.appendChild(cell12);
  152. newRow.appendChild(cell11);
  153. tbody.appendChild(newRow);
  154. return rowData;
  155. },2000); // 等待 1000 毫秒(1 秒)
  156. }
  157.  
  158.  
  159. (function() {
  160.  
  161. var rowData = testA();
  162.  
  163.  
  164. // Your code here...
  165. })();