GazelleGames Batch Screenshot Processor

Batch process PTPImg URLs with option to preserve existing screenshots

// ==UserScript==
// @name         GazelleGames Batch Screenshot Processor
// @namespace    http://tampermonkey.net/
// @version      1.1
// @description  Batch process PTPImg URLs with option to preserve existing screenshots
// @author       YourName
// @match        https://gazellegames.net/torrents.php?action=editgroup&groupid=*
// @grant        none
// @run-at       document-end
// ==/UserScript==

(function() {
    'use strict';

    // Create batch processor UI
    function createBatchProcessor() {
        const container = document.createElement('div');
        container.style.flex = '1';
        container.style.marginLeft = '20px';
        container.style.marginTop = '5px';

        container.innerHTML = `
            <div style="margin-bottom: 10px;">
                <textarea id="gg-batch-urls" rows="10" style="width: 80%; padding: 5px; border: 1px solid #ccc; font-family: inherit; font-size: 13px;" placeholder="Paste PTPImg URLs here, one per line"></textarea>
            </div>
            <div style="margin-bottom: 8px;">
                <input type="checkbox" id="gg-keep-existing">
                <label for="gg-keep-existing" style="font-size: 13px;">Keep existing screenshots</label>
            </div>
            <input type="button" id="gg-process-btn" value="Fill Screenshot Fields">
        `;

        return container;
    }

    // Process URLs
    function processURLs(e) {
        e.preventDefault();

        const textarea = document.getElementById('gg-batch-urls');
        const keepExisting = document.getElementById('gg-keep-existing').checked;
        const urls = textarea.value.split('\n')
            .map(url => url.trim())
            .filter(url => url !== '');

        if (urls.length === 0) {
            alert('Please enter at least one URL');
            return;
        }

        // Get all existing fields and their values
        const existingFields = Array.from(document.querySelectorAll('#image_block input[name="screens[]"]'))
            .map(input => input.value.trim())
            .filter(val => val !== '');

        // Combine URLs based on checkbox
        const allUrls = keepExisting ? [...existingFields, ...urls] : urls;

        // Adjust field count if needed
        if (allUrls.length > existingFields.length) {
            const toAdd = allUrls.length - existingFields.length;
            for (let i = 0; i < toAdd; i++) {
                if (typeof AddScreenField === 'function') {
                    AddScreenField(true);
                } else {
                    const newField = document.createElement('span');
                    newField.innerHTML = `<br><input type="text" name="screens[]" style="width: 90%;"><input type="button" value="PTPImg It">`;
                    document.getElementById('image_block').appendChild(newField);
                }
            }
        } else if (allUrls.length < existingFields.length && !keepExisting) {
            const toRemove = existingFields.length - allUrls.length;
            for (let i = 0; i < toRemove; i++) {
                if (typeof RemoveScreenField === 'function') {
                    RemoveScreenField();
                } else {
                    const fields = document.querySelectorAll('#image_block span');
                    if (fields.length > 1) {
                        fields[fields.length - 1].remove();
                    }
                }
            }
        }

        // Populate fields
        const inputs = document.querySelectorAll('#image_block input[name="screens[]"]');
        for (let i = 0; i < allUrls.length; i++) {
            if (i < inputs.length) {
                inputs[i].value = allUrls[i];
            }
        }
    }

    // Main function to set up the UI
    function init() {
        const imageBlock = document.getElementById('image_block');
        if (!imageBlock) return;

        // Create container for the row layout
        const wrapper = document.createElement('div');
        wrapper.style.display = 'flex';
        wrapper.style.gap = '20px';
        wrapper.style.marginTop = '5px';

        // Wrap the image block
        const screenshotsColumn = document.createElement('div');
        screenshotsColumn.style.flex = '1.2';
        screenshotsColumn.appendChild(imageBlock);

        // Create the batch processor
        const batchColumn = createBatchProcessor();

        // Add both columns to the wrapper
        wrapper.appendChild(screenshotsColumn);
        wrapper.appendChild(batchColumn);

        // Find the best insertion point
        const screenshotsHeader = [...document.querySelectorAll('h3')].find(h3 =>
            h3.textContent.includes('Screenshots')
        );

        if (screenshotsHeader) {
            screenshotsHeader.parentNode.insertBefore(wrapper, screenshotsHeader.nextSibling);
        } else {
            imageBlock.parentNode.insertBefore(wrapper, imageBlock);
        }

        // Add event listener to the process button
        document.getElementById('gg-process-btn').addEventListener('click', processURLs);
    }

    // Run the script
    setTimeout(init, 500);
})();