Auto Click "Try GPT-5" Button

Automatically clicks the "Try GPT-5" button on page load with improved timing

// ==UserScript==
// @name         Auto Click "Try GPT-5" Button
// @namespace    http://tampermonkey.net/
// @version      1.2
// @description  Automatically clicks the "Try GPT-5" button on page load with improved timing
// @author       You
// @match        https://m365.cloud.microsoft/*
// @grant        none
// @homepage    https://greasyfork.org/en/scripts/550204-auto-click-try-gpt-5-button
// ==/UserScript==

(function() {
    'use strict';

    let attempts = 0;
    const maxAttempts = 20;
    const delay = 500; // 500ms between attempts
    let buttonClicked = false; // Flag to prevent multiple clicks

    function findAndClickButton() {
        if (buttonClicked) return; // Exit if already clicked

        attempts++;
        console.log(`Attempt ${attempts}: Looking for GPT-5 button...`);

        const buttons = document.querySelectorAll('button[aria-pressed="false"]');
        let found = false;

        buttons.forEach(button => {
            if (button.textContent.includes("Try GPT-5") && !buttonClicked) {
                console.log("Found GPT-5 button, clicking...");
                button.click();
                found = true;
                buttonClicked = true; // Set flag to prevent further attempts
            }
        });

        if (found) {
            console.log("GPT-5 button clicked successfully!");
            observer.disconnect(); // Stop observing immediately
            return;
        }

        // If not found and haven't reached max attempts, try again
        if (attempts < maxAttempts && !buttonClicked) {
            setTimeout(findAndClickButton, delay);
        } else if (!buttonClicked) {
            console.log("Max attempts reached, GPT-5 button not found");
        }
    }

    // Multiple event listeners for better coverage

    // Try immediately when DOM is ready
    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', () => {
            setTimeout(findAndClickButton, 1000); // 1 second delay after DOM ready
        });
    } else {
        setTimeout(findAndClickButton, 1000);
    }

    // Try after window load event
    window.addEventListener('load', () => {
        setTimeout(findAndClickButton, 2000); // 2 second delay after load
    });

    // Also use MutationObserver to watch for dynamically added content
    const observer = new MutationObserver((mutations) => {
        if (buttonClicked) return; // Exit if already clicked

        mutations.forEach((mutation) => {
            if (mutation.type === 'childList' && mutation.addedNodes.length > 0) {
                // Check if any new buttons were added
                mutation.addedNodes.forEach((node) => {
                    if (node.nodeType === 1 && (node.tagName === 'BUTTON' || node.querySelector('button'))) {
                        setTimeout(findAndClickButton, 500);
                    }
                });
            }
        });
    });

    // Start observing
    observer.observe(document.body, {
        childList: true,
        subtree: true
    });

    // Stop observing after 30 seconds to avoid memory leaks
    setTimeout(() => {
        observer.disconnect();
        console.log("MutationObserver disconnected");
    }, 30000);
})();