G00DB0Y / Imgbox Auto Content Select and Auto Submit

// ==UserScript==
// @name         Imgbox Auto Content Select and Auto Submit
// @namespace    http://tampermonkey.net/
// @version      2.9
// @description  Automatically set content type to "Adult content" and auto-submit on imgbox.com if images are selected and "Adult content" is set. Auto-submit when a file-item is detected in the DOM after selecting images from the popup box.
// @author       NiGHTCUM
// @license     MIT
// @match        https://imgbox.com/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    let adultContentSelected = false; // Track if "Adult content" is already selected
    let submitTimeoutId = null; // To track and control the form submission timeout

    // Function to select the "Adult content" option
    function selectAdultContent() {
        const contentTypeDropdown = document.querySelector('.dropdown-menu.open');
        if (contentTypeDropdown) {
            console.log("Content type dropdown is open, attempting to select Adult content");

            const adultOption = Array.from(contentTypeDropdown.querySelectorAll('a'))
                .find(option => option.textContent.trim() === "Adult Content");

            if (adultOption) {
                console.log("Adult content option found, clicking it...");
                adultOption.click(); // Trigger a click on the Adult content option
                adultContentSelected = true; // Mark that "Adult content" is selected
                console.log("Adult content selected");

                // Check if file-item is already present and start delay submission if so
                if (isFileItemPresent()) {
                    startDelayedSubmit(); // Use the delayed submission function
                }
            } else {
                console.log("Adult content option not found within open dropdown");
            }
        } else {
            console.log("Content type dropdown is not open or not found");
        }
    }

    // Function to check if the file-item is present in the DOM
    function isFileItemPresent() {
        const fileItems = document.querySelectorAll('.file-item.template-upload.in');
        if (fileItems.length > 0) {
            console.log('File items found in the DOM:', fileItems.length);
            return true; // Return true if any file items are found
        }
        console.log('File items not found in the DOM.');
        return false; // Return false if no file items are found
    }

    // Function to submit the form automatically after a delay
    function startDelayedSubmit() {
        if (submitTimeoutId) {
            clearTimeout(submitTimeoutId); // Clear any existing timeout to prevent multiple triggers
        }

        console.log("Starting 5-second delay before submitting the form...");

        submitTimeoutId = setTimeout(() => { // Introduce a 5-second delay before submitting the form
            const submitButton = document.querySelector('button[type="submit"], input[type="submit"]'); // Select the submit button
            if (submitButton) {
                console.log("5 seconds passed, submitting the form...");
                submitButton.click(); // Trigger a click on the submit button to submit the form
                console.log("Form submitted automatically.");
            } else {
                console.log("Submit button not found. Could not auto-submit the form.");
            }
        }, 1000); // 1 seconds delay
    }

    // Function to handle settings based on the current state
    function setAutoSettings() {
        if (!adultContentSelected) {
            selectAdultContent(); // Select "Adult content"
        }
    }

    // Function to attempt setting values multiple times
    function attemptSettingValues(retries = 20, interval = 1500) {
        let attempts = 0;
        const intervalId = setInterval(() => {
            console.log(`Attempt ${attempts + 1} to set values`);
            setAutoSettings();
            attempts++;
            if (adultContentSelected || attempts >= retries) {
                clearInterval(intervalId);
                console.log('Settings applied or maximum attempts reached. Stopping attempts.');
            }
        }, interval);
    }

    // MutationObserver to detect the presence of file-item elements in the DOM
    function observeFileItems() {
        const observer = new MutationObserver(mutations => {
            mutations.forEach(mutation => {
                if (mutation.addedNodes.length > 0) {
                    mutation.addedNodes.forEach(node => {
                        if (node.nodeType === 1 && node.matches('.file-item.template-upload.in')) { // Check if the added node matches the target class
                            console.log('File item detected in the DOM:', node);
                            if (adultContentSelected) {
                                console.log("Adult content is set. Starting delay before submitting the form...");
                                startDelayedSubmit(); // Start delayed submit when file item is detected
                            }
                        }
                    });
                }
            });
        });

        // Start observing the body for the addition of file-item elements
        observer.observe(document.body, { childList: true, subtree: true });
        console.log("Started observing for file items...");
    }

    // MutationObserver to detect changes in the page's DOM
    const pageObserver = new MutationObserver(() => {
        console.log("DOM mutation detected, will attempt to set settings...");
        setTimeout(() => {
            attemptSettingValues(); // Attempt to set the values multiple times after a short delay
        }, 500); // Short delay to ensure elements are fully rendered
    });

    // Start observing the body for changes
    pageObserver.observe(document.body, { childList: true, subtree: true });
    console.log("Started observing DOM mutations...");

    // Initial call to set the settings in case elements are already loaded
    attemptSettingValues();
    observeFileItems(); // Start observing file items
})();