Shoutbox Image Uploader with Label

Upload images to shoutbox via PixVid. Originally created by 4everInf.

2025-09-05 يوللانغان نەشرى. ئەڭ يېڭى نەشرىنى كۆرۈش.

// ==UserScript==
// @name         Shoutbox Image Uploader with Label
// @version      1
// @description  Upload images to shoutbox via PixVid. Originally created by 4everInf.
// @author       BOMBASTIC
// @match        https://*.torrentbd.com/*
// @match        https://*.torrentbd.net/*
// @match        https://*.torrentbd.org/*
// @match        https://*.torrentbd.me/*
// @grant        GM_xmlhttpRequest
// @connect      pixvid.org
// @run-at       document-end
// @namespace https://greasyfork.org/users/1512130
// ==/UserScript==

(function() {
    'use strict';

    const UPLOAD_API_URL = 'https://pixvid.org/api/1/upload';
    const API_KEY = 'f2aee9480da2c75211f143f4a308bff0c83b2990b72cf21794f207591db93a39';
    const SHOUTBOX_INPUT_SELECTOR = '#shout_text';
    const SHOUTBOX_TRAY_SELECTOR  = '#shout-ibb-container';

    function initializeUploader() {
        const shoutInput = document.querySelector(SHOUTBOX_INPUT_SELECTOR);
        const shoutTray  = document.querySelector(SHOUTBOX_TRAY_SELECTOR);
        if (!shoutInput || !shoutTray || document.getElementById('tbd-uploader-button')) return false;

        // Hidden file input
        const fileInput = document.createElement('input');
        fileInput.type = 'file';
        fileInput.accept = 'image/*';
        fileInput.style.display = 'none';
        document.body.appendChild(fileInput);

        // Panel (hidden by default)
        const panel = document.createElement('div');
        panel.id = 'imagePanel';
        panel.style.position = 'absolute';
        panel.style.bottom = '45px';
        panel.style.right = '0';
        panel.style.width = '280px';
        panel.style.background = 'var(--main-bg)';
        panel.style.border = '1px solid var(--border-color)';
        panel.style.padding = '4px';
        panel.style.display = 'flex';
        panel.style.flexDirection = 'row';
        panel.style.alignItems = 'stretch';
        panel.style.gap = '4px';
        panel.style.zIndex = '9999';
        panel.style.height = 'auto';
        panel.style.visibility = 'hidden';

        // Label input
        const labelInput = document.createElement('input');
        labelInput.type = 'text';
        labelInput.placeholder = 'Label (optional)';
        labelInput.style.flex = '1';
        labelInput.style.padding = '0 6px';
        labelInput.style.margin = '0';
        labelInput.style.fontSize = '0.85rem';
        labelInput.style.lineHeight = '28px';
        labelInput.style.height = '28px';
        labelInput.style.border = '1px solid var(--border-color)';
        labelInput.style.borderRadius = '2px';
        labelInput.style.boxSizing = 'border-box';
        panel.appendChild(labelInput);

        // Upload button
        const uploadBtn = document.createElement('button');
        uploadBtn.textContent = 'Upload';
        uploadBtn.style.height = '28px';
        uploadBtn.style.padding = '0 8px';
        uploadBtn.style.margin = '0';
        uploadBtn.style.lineHeight = '28px';
        uploadBtn.style.flex = '0 0 auto';
        uploadBtn.style.cursor = 'pointer';
        uploadBtn.style.borderRadius = '2px';
        panel.appendChild(uploadBtn);

        // Insert button
        const insertBtn = document.createElement('button');
        insertBtn.textContent = 'Insert';
        insertBtn.style.height = '28px';
        insertBtn.style.padding = '0 8px';
        insertBtn.style.margin = '0';
        insertBtn.style.lineHeight = '28px';
        insertBtn.style.flex = '0 0 auto';
        insertBtn.style.cursor = 'pointer';
        insertBtn.style.borderRadius = '2px';
        panel.appendChild(insertBtn);

        shoutTray.appendChild(panel);

        // Image icon button
        const uploaderButton = document.createElement('span');
        uploaderButton.id = 'tbd-uploader-button';
        uploaderButton.className = 'inline-submit-btn';
        uploaderButton.title = 'Upload Image';
        uploaderButton.innerHTML = '<i class="material-icons">add_photo_alternate</i>';
        uploaderButton.style.cursor = 'pointer';
        shoutTray.insertBefore(uploaderButton, shoutTray.firstChild);

        // Show/hide panel on click
        uploaderButton.addEventListener('click', () => {
            panel.style.visibility = panel.style.visibility === 'visible' ? 'hidden' : 'visible';
        });

        // Upload button triggers file input
        uploadBtn.addEventListener('click', () => {
            fileInput.click();
        });

        let lastUploadedUrl = '';

        fileInput.addEventListener('change', (e) => {
            const file = e.target.files[0];
            if (!file) return;
            uploadBtn.disabled = true;
            insertBtn.disabled = true;
            uploadImage(file);
            fileInput.value = null;
        });

        insertBtn.addEventListener('click', () => {
            if (!lastUploadedUrl) return;
            const label = labelInput.value.trim();
            if (label) {
                // Remove any existing [url=...] wrapping for the last uploaded URL
                const escapedUrl = lastUploadedUrl.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
                const regex = new RegExp(`\\[url=${escapedUrl}\\].*?\\[\\/url\\]`);
                shoutInput.value = shoutInput.value.replace(regex, lastUploadedUrl);

                // Replace the last occurrence of plain URL with new label
                const plainRegex = new RegExp(escapedUrl + '(?!.*' + escapedUrl + ')');
                shoutInput.value = shoutInput.value.replace(plainRegex, `[url=${lastUploadedUrl}]${label}[/url]`);
            }
            labelInput.value = '';
        });

        function uploadImage(file) {
            const originalText = shoutInput.value;
            shoutInput.disabled = true;

            const formData = new FormData();
            formData.append('source', file);

            let percent = 0;
            const interval = setInterval(() => {
                if (percent < 95) {
                    percent += Math.floor(Math.random() * 5) + 1;
                    shoutInput.value = `[Uploading ${percent}%…]`;
                }
            }, 500);

            GM_xmlhttpRequest({
                method: 'POST',
                url: UPLOAD_API_URL,
                data: formData,
                headers: { 'X-API-Key': API_KEY },
                onload: (response) => {
                    clearInterval(interval);
                    try {
                        if (response.status !== 200) throw new Error(`HTTP ${response.status}`);
                        const json = JSON.parse(response.responseText);
                        const imageUrl = json.image?.url || json.image?.url_short;
                        if (!imageUrl) throw new Error('Upload failed');

                        lastUploadedUrl = imageUrl; // store for label insertion
                        shoutInput.value = originalText + imageUrl + ' ';
                    } catch (err) {
                        shoutInput.value = `[Upload failed! ${err.message}]`;
                        setTimeout(() => { shoutInput.value = originalText; }, 3000);
                    } finally {
                        shoutInput.disabled = false;
                        uploadBtn.disabled = false;
                        insertBtn.disabled = false;
                        shoutInput.focus();
                    }
                },
                onerror: () => {
                    clearInterval(interval);
                    shoutInput.value = '[Upload failed! Network error.]';
                    setTimeout(() => { shoutInput.value = originalText; }, 3000);
                    shoutInput.disabled = false;
                    uploadBtn.disabled = false;
                    insertBtn.disabled = false;
                    shoutInput.focus();
                }
            });
        }

        return true;
    }

    let attempts = 0;
    const interval = setInterval(() => {
        if (initializeUploader() || ++attempts > 60) clearInterval(interval);
    }, 250);

})();