You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Tampermonkey脚本检测指定按钮并自动点击功能故障排查求助

Fixing Your Tampermonkey Auto-Click Script

Hey there! Let's walk through why your current script isn't working and get you a reliable solution for auto-clicking that "accept" button when it pops up.

First, Let's Break Down the Issues in Your Original Code

  • You're grabbing the button right at the start of the script, before it might even exist in the DOM. That means caseHide could be null from the get-go, making your checks useless.
  • The offsetParent check alone isn't always reliable for detecting visibility (e.g., fixed-position elements might have offsetParent as null even when visible).
  • You're using an older method to create and dispatch click events—modern browsers support a much simpler click() method that's more dependable.
  • You accidentally called checkTimer() (the interval ID) instead of letting setInterval run automatically, though that part might not be breaking everything.

Solution 1: Improved Polling Method

This version fixes the issues with your original approach, with better checks and cleaner code:

// ==UserScript==
// @name         Auto Click Accept Button
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Auto clicks the accept button after a 2-second delay once it appears
// @author       You
// @match        YOUR_TARGET_WEBSITE_URL
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    const POLL_INTERVAL = 500; // Check every 500ms (adjust as needed)
    const CLICK_DELAY = 2000; // Wait 2 seconds before clicking

    function checkAndClickButton() {
        const acceptBtn = document.getElementById("accept");
        
        // Make sure the button exists and is visible
        if (acceptBtn && acceptBtn.offsetParent !== null && window.getComputedStyle(acceptBtn).visibility !== 'hidden') {
            // Delay the click by 2 seconds
            setTimeout(() => {
                acceptBtn.click(); // Simple, reliable click method
                console.log("Successfully clicked the accept button!");
                clearInterval(pollTimer); // Stop polling once we've clicked
            }, CLICK_DELAY);
        }
    }

    // Start polling for the button
    const pollTimer = setInterval(checkAndClickButton, POLL_INTERVAL);
})();

Key improvements here:

  • We grab the button every time we check, so we always get the latest instance if it's added dynamically.
  • Added extra visibility checks to ensure we don't try clicking an invisible button.
  • Uses the native click() method instead of manual event creation.
  • Clears the interval after clicking to avoid unnecessary repeated checks.

Solution 2: MutationObserver (More Efficient)

If you want a more performance-friendly option (no constant polling), use MutationObserver to listen for changes to the DOM—this will trigger immediately when the button is added by the popup:

// ==UserScript==
// @name         Auto Click Accept Button (Observer Version)
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Auto clicks the accept button after a 2-second delay when it's added to the DOM
// @author       You
// @match        YOUR_TARGET_WEBSITE_URL
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    const CLICK_DELAY = 2000;

    function handleAcceptButton() {
        const acceptBtn = document.getElementById("accept");
        if (acceptBtn && acceptBtn.offsetParent !== null && window.getComputedStyle(acceptBtn).visibility !== 'hidden') {
            setTimeout(() => {
                acceptBtn.click();
                console.log("Successfully clicked the accept button!");
                observer.disconnect(); // Stop observing once done
            }, CLICK_DELAY);
            return true;
        }
        return false;
    }

    // First, check if the button is already on the page
    if (handleAcceptButton()) return;

    // Create an observer to watch for DOM changes
    const observer = new MutationObserver((mutations) => {
        mutations.forEach(mutation => {
            // Check newly added nodes for our button
            if (mutation.addedNodes.length) {
                for (const node of mutation.addedNodes) {
                    if (node.id === "accept" || (node.querySelector && node.querySelector("#accept"))) {
                        if (handleAcceptButton()) return;
                    }
                }
            }
        });
    });

    // Observe changes to the entire body (you can narrow this to the popup container if you know its selector)
    observer.observe(document.body, {
        childList: true,
        subtree: true
    });
})();

This method is better because:

  • No constant polling wasting resources.
  • Detects the button the moment it's added to the DOM.
  • Still includes the 2-second delay before clicking, as you requested.

Just remember to replace YOUR_TARGET_WEBSITE_URL with the actual URL of the site you're running this on—Tampermonkey needs that to know when to activate the script.

内容的提问来源于stack exchange,提问作者Eirandir

火山引擎 最新活动