osu! QuickPaste Image to BBCode

Automatically pastes BBCode image links in any textarea on osu! website when you use Ctrl V and an image is in the clipboard.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

You will need to install an extension such as Tampermonkey to install this script.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

You will need to install an extension such as Tampermonkey to install this script.

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==UserScript==
// @name         osu! QuickPaste Image to BBCode
// @namespace    https://osu.ppy.sh/users/24332734
// @version      1.0
// @description  Automatically pastes BBCode image links in any textarea on osu! website when you use Ctrl V and an image is in the clipboard.
// @author       Behrauder
// @match        https://osu.ppy.sh/*
// @license      MIT
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    const API_KEY = 'YOUR_API_KEY_HERE'; // Put your imgbb API key here

    // Listen to the "paste" event on the document
    document.addEventListener('paste', async function(e) {
        const clipboardData = e.clipboardData || window.clipboardData;
        const items = clipboardData.items;

        for (const item of items) {
            if (item.type.startsWith('image')) {
                const blob = item.getAsFile();
                const formData = new FormData();
                formData.append('image', blob);
                formData.append('key', API_KEY);
                formData.append('expiration', 0); // Never expire

                // Upload the image
                const response = await fetch('https://api.imgbb.com/1/upload', {
                    method: 'POST',
                    body: formData
                });

                const data = await response.json();
                if (data.success) {
                    const bbcodeFull = `[img]${data.data.url}[/img]`;
                    insertTextAtCursor(bbcodeFull);
                } else {
                    console.error('Upload failed:', data.status_txt);
                }
                break; // Exit the loop after the first image item
            }
        }
    });

    // Function to insert text at the cursor position in a textarea
    function insertTextAtCursor(text) {
        const textarea = document.activeElement;
        if (textarea.tagName === 'TEXTAREA') {
            const start = textarea.selectionStart;
            const end = textarea.selectionEnd;

            const value = textarea.value;
            textarea.value = value.substring(0, start) + text + value.substring(end);

            textarea.selectionStart = textarea.selectionEnd = start + text.length;
        }
    }
})();