Tampermonkey®油猴脚本实践——自动填工时
// ==UserScript==
// @name         自动填工时
// @namespace    http://tampermonkey.net/
// @version      2024-07-24
// @description  try to take over the world!
// @author       kai.shi
// @match        https://e-cology.beyondsoft.com/spa/workflow/static4form/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=beyondsoft.com
// @grant        none
// ==/UserScript==
(function() {
    'use strict';
    console.log('加载油猴脚本');
    // 模拟输入事件的函数
    function simulateInput(inputElement, value) {
        const focusEvent = new Event('focus');
        const inputEvent = new Event('input', { bubbles: true });
        const changeEvent = new Event('change', { bubbles: true });
        const blurEvent = new Event('blur');
        // 模拟获得焦点
        inputElement.dispatchEvent(focusEvent);
        // 设置输入框的值
        inputElement.value = value;
        // 模拟输入操作
        inputElement.dispatchEvent(inputEvent);
        // 模拟值改变
        inputElement.dispatchEvent(changeEvent);
        // 模拟失去焦点
        inputElement.dispatchEvent(blurEvent);
    }
    // 获取指定按钮元素
    const buttonElement = document.querySelector('#addbutton0');
    // 函数:填充所有非只读的输入框
    function fillInputsWithValue(value) {
        const inputs = document.querySelectorAll('input[type="text"].wf-input-2.wf-input-detail:not([readonly])');
        inputs.forEach(input => {
            simulateInput(input, value);
        });
    }
    // 等待直到按钮元素在DOM中,然后点击
    function clickWhenInDOM(elementSelector) {
        var interval = setInterval(function() {
            var element = document.querySelector(elementSelector);
            if (element && element.offsetParent !== null) { // 检查元素是否在DOM中
                element.click();
                clearInterval(interval);
            }
        }, 4000); // 每4秒检查一次
        // 使用 setInterval 定期检查非只读输入框的数量
        const intervalId = setInterval(() => {
            const inputs = document.querySelectorAll('input[type="text"].wf-input-2.wf-input-detail:not([readonly])');
            // 当非只读输入框数量大于 10 时,执行填充操作并清除定时器
            if (inputs.length > 10) {
                fillInputsWithValue('8');
                clearInterval(intervalId); // 清除定时器
            }
        }, 5000); // 每5秒检查一次
    }
    // 调用函数并传入按钮的选择器(假设选择器为 'button')
    clickWhenInDOM("#addbutton0");
})();
                Tampermonkey®油猴脚本实践——自动填工时
        https://shikai.info/archives/l2016TKO