GenesisAN / OvertimeProofSaver

// ==UserScript==
// @name         OvertimeProofSaver
// @namespace    http://tampermonkey.net/
// @version      2024-01-18
// @description  从特定XPath下获取所有超链接,并在打开每个链接前显示确认对话框
// @author       You
// @match        http://58.20.202.41:8099/spa/workflow/static/index.html
// @match        http://58.20.202.41:8099/spa/workflow/static4form/index.html?_rdm=*
// @require      https://html2canvas.hertzen.com/dist/html2canvas.min.js
// @icon         https://www.google.com/s2/favicons?sz=64&domain=202.41
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    let totalSum = 0;

    // 等待网页加载完毕
    window.onload = function() {
        // 定时检测元素是否存在
        var checkExist = setInterval(function() {
            var xpath = '/html/body/div[1]/div/div/div[2]/div[1]/div[2]/div[1]/div/div/div[2]/div[1]/div/table/tbody/tr[17]/td[3]/div/div/div/div/div[2]/table/tbody';
            var tbody = document.evaluate(xpath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;

            if (tbody&&tbody.children.length>3) {
                const trElements = tbody.children;
                for (let tr of trElements) {
                    let inputElement = tr.querySelector('td:nth-child(9) div span input');
                    if (inputElement) {
                        let value = parseFloat(inputElement.value);
                        if (!isNaN(value)) {
                            totalSum += value;
                        }
                    }
                }
                console.log('总和:', totalSum);
               // window.print();

                clearInterval(checkExist); // 停止检测
                takeFullScreenshot();
            }
        }, 1000); // 每隔1000毫秒检查一次
    };
    // 设置观察者来监视DOM变化
    var observer = new MutationObserver(function(mutations, me) {
        var buttonTargetXPath = '/html/body/div[1]/div/div/div[1]/div[1]/div[1]/div[2]';
        var buttonTarget = document.evaluate(buttonTargetXPath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;

        if (buttonTarget) {
            var button = document.createElement('button');
            button.textContent = '保存加班图片';
            button.style.marginLeft = '10px';

            button.onclick = function() {
                var targetXPath = '/html/body/div[1]/div/div/div[1]/div[1]/div[2]/div/div[3]/div[2]/div[2]/div/div[1]/div/div/div/div/div/div/div/span/div[2]/table/tbody';
                var targetElement = document.evaluate(targetXPath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;

                if (targetElement) {
                    var rows = targetElement.children;
                    for (var i = 0; i < rows.length; i++) {
                        (function(index) {
                            setTimeout(function() {
                                var linkXPath = './/td[3]/span/a';
                                var linkElement = document.evaluate(linkXPath, rows[index], null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;

                                if (linkElement) {
                                    var onClickAttribute = linkElement.getAttribute('onclick');
                                    if (onClickAttribute) {
                                        //var confirmOpen = confirm("是否打开此链接: " + linkElement.textContent.trim() + "?");
                                       // if (confirmOpen) {
                                            eval(onClickAttribute);
                                      //  }
                                    }
                                }
                            }, 9000 * index); // 延时递增,每次增加20秒
                        })(i);
                    }
                }
            };

            buttonTarget.appendChild(button);
            // 停止观察
            me.disconnect();
            return;
        }
    });

    // 配置观察者选项:
    var observerConfig = {
        childList: true,
        subtree: true
    };

    // 选择需要观察变动的节点
    var targetNode = document.body;

    // 使用配置文件对目标节点进行观察
    observer.observe(targetNode, observerConfig);




    function takeFullScreenshot() {
    var xpath = '/html/body/div[1]/div/div/div[2]/div[1]/div[2]/div[1]/div';
    var date = '/html/body/div[1]/div/div/div[2]/div[1]/div[2]/div[1]/div/div/div[2]/div[1]/div/table/tbody/tr[12]/td[7]/div/span/span'
    var scrollableElement = document.evaluate(xpath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;

    if (scrollableElement) {
        var originalHeight = scrollableElement.style.height; // 保存原始高度

        scrollableElement.style.height = scrollableElement.scrollHeight + 'px'; // 调整高度以展示所有内容
        html2canvas(scrollableElement).then(canvas => {
  // 将Canvas转换为图像
            var imgData = canvas.toDataURL('image/png');

            // 创建下载链接
            var downloadLink = document.createElement('a');
            downloadLink.href = imgData;
            setTimeout(function () {
                var dateElement = document.evaluate(date, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
                downloadLink.download = dateElement.textContent + '(' + totalSum +'H).png'

                // 模拟点击下载
                document.body.appendChild(downloadLink);
                downloadLink.click();
                document.body.removeChild(downloadLink);

                scrollableElement.style.height = originalHeight; // 恢复原始高度
                setTimeout(function () {
                    window.close(); // 关闭当前窗口
                }, 4000); // 10000毫秒 = 10秒
            }, 2000); // 10000毫秒 = 10秒
        });
    } else {
        console.error('无法找到指定的元素');
    }
}
})();