Greasy Fork

Greasy Fork is available in English.

🤖IMYAI网站自动签到 Auto Sign-in for IMYAI Website

打开网页后等待3秒自动签到,签到完成后自动关闭签到对话框(每天只会执行一次) Automate sign-in with a 3-second delay after page load and automatically close the sign-in dialog after sign-in.

当前为 2023-12-17 提交的版本,查看 最新版本

// ==UserScript==
// @name         🤖IMYAI网站自动签到 Auto Sign-in for IMYAI Website
// @namespace    http://tampermonkey.net/
// @version      1.1
// @description  打开网页后等待3秒自动签到,签到完成后自动关闭签到对话框(每天只会执行一次) Automate sign-in with a 3-second delay after page load and automatically close the sign-in dialog after sign-in.
// @author       GPT4.0
// @match        https://ai.imyai.top/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    // Function to get today's date as a string
    function getTodayString() {
        const today = new Date();
        return today.toISOString().split('T')[0]; // Format: YYYY-MM-DD
    }

    // Function to check if the script should run
    function shouldRunToday() {
        const lastRunDate = localStorage.getItem('lastRunDate');
        const todayString = getTodayString();

        if (lastRunDate === todayString) {
            return false; // Do not run if already run today
        }

        // Update the last run date to today
        localStorage.setItem('lastRunDate', todayString);
        return true;
    }

    // Function to simulate a mouse click
    function simulateClick(element) {
        const mouseClickEvents = ['mousedown', 'click', 'mouseup'];
        mouseClickEvents.forEach(mouseEventType =>
            element.dispatchEvent(
                new MouseEvent(mouseEventType, {
                    view: window,
                    bubbles: true,
                    cancelable: true,
                    buttons: 1
                })
            )
        );
    }

    // Function to close the sign-in dialog after a delay
    function closeSignInDialog() {
        const closeButton = document.querySelector('.n-base-close.n-base-close--absolute.n-card-header__close');
        if (closeButton) {
            simulateClick(closeButton); // First attempt to close the dialog
            setTimeout(() => {
                simulateClick(closeButton); // Second attempt to close the dialog
            }, 500); // Delay between attempts
        }
    }

    // Function to handle the sign-in process
    function handleSignIn() {
        // Query for the first sign-in button
        const firstSignInButton = document.querySelector('svg.iconify--noto');
        if (firstSignInButton) {
            simulateClick(firstSignInButton);
        }

        // Check for the second sign-in button every half second
        const checkExist = setInterval(function() {
            const secondSignInButton = document.querySelector('div.flex.mt-3.w-full.mt-14 > button.n-button--info-type');
            if (secondSignInButton) {
                simulateClick(secondSignInButton);
                clearInterval(checkExist); // Stop checking once the button has been clicked
            }
        }, 500); // Check every 500ms
    }

    // Function to add the sign-in button to the page (for manual control if needed)
    function addButton() {
        const newButton = document.createElement('button');
        newButton.innerText = '';
        // Styling the button
        newButton.style.cssText = `
            position: fixed;
            top: 10px;
            left: 10px;
            z-index: 10000;
            padding: 25px 25px;
            background-color: transparent;
            color: transparent;
            border: none;
            border-radius: 5px;
            box-shadow: none;
            font-weight: bold;
            cursor: pointer;
            transition: background-color 0.3s, box-shadow 0.3s;
        `;

        newButton.onclick = handleSignIn;

        document.body.appendChild(newButton);
    }

    // Modified logic to add button and trigger sign-in
    if (shouldRunToday()) {
        if (document.readyState === 'complete') {
            addButton();
            setTimeout(handleSignIn, 3000);
            setTimeout(closeSignInDialog, 4000);
            setTimeout(closeSignInDialog, 4500);
        } else {
            window.addEventListener('load', function() {
                addButton();
                setTimeout(handleSignIn, 3000);
                setTimeout(closeSignInDialog, 4000);
                setTimeout(closeSignInDialog, 4500);
            });
        }
    }
})();