自动填充表单

自动填充

目前為 2024-07-29 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         自动填充表单
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  自动填充
// @author       72
// @match        *://*/*
// @grant        none
// ==/UserScript==


(function() {
    'use strict';

    function getElementText(element) {
        return Array.from(element.childNodes)
            .map(child => child.nodeType === Node.TEXT_NODE ? child.textContent.trim() : getElementText(child))
            .join(' ')
            .trim();
    }

    function autofill() {
        let filled = false;
        document.querySelectorAll('label, div, span').forEach(element => {
            const text = getElementText(element);
            let fillValue;

            switch (true) {
                case /^(贴吧|ID|)$/.test(text): fillValue = '自动填充的名字'; break;
                case /^(姓名|姓名1)$/.test(text): fillValue = '香菜'; break;
                case /^(姓名2)$/.test(text): fillValue = '小王'; break;
                case /^(手机号|手机号1|手机号码|手机号码2)$/.test(text): fillValue = '110'; break;
                case /^(手机号2|手机号码2)$/.test(text): fillValue = '112'; break;
                case /^(身份证|身份证1身份证号码|身份证号码1)$/.test(text): fillValue = '320'; break;
                case /^(身份证2|身份证号码2)$/.test(text): fillValue = '220'; break;
            }

            if (fillValue !== undefined) {
                const inputField = element.closest('.ant-form-item, .ant-col')?.querySelector('input');
                if (inputField) {
                    inputField.value = fillValue;
                    filled = true;
                    console.log('Filled value:', fillValue, 'in input field.');
                } else {
                    console.log('Input field not found.');
                }
            }
        });

        // 滚动到页面底部
        window.scrollTo(0, document.body.scrollHeight);

        return filled;
    }

    function attemptAutofill(retries = 20, delay = 500) {
        let attempt = 0;
        const tryFill = () => {
            if (autofill() || attempt >= retries) return;
            attempt++;
            setTimeout(tryFill, delay);
        };
        tryFill();
    }

    setTimeout(() => attemptAutofill(), 100);

    new MutationObserver(mutations => {
        mutations.forEach(mutation => {
            if (mutation.type === 'childList' || mutation.type === 'attributes') {
                attemptAutofill();
            }
        });
    }).observe(document.body, { attributes: true, childList: true, subtree: true });

    window.addEventListener('load', () => {
        attemptAutofill();
        window.scrollTo(0, document.body.scrollHeight);
    });

})();