Smart Copy Image

Custom "Copy Image As" with better filename

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

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

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

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

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

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.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

// ==UserScript==
// @name         Smart Copy Image
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Custom "Copy Image As" with better filename
// @match        *://*/*
// @grant        GM_setClipboard
// ==/UserScript==

(function () {
    'use strict';

    let menu;

    document.addEventListener('contextmenu', async (e) => {
        const img = e.target.closest('img');
        if (!img) return;

        e.preventDefault();

        if (menu) menu.remove();

        menu = document.createElement('div');
        menu.style.position = 'fixed';
        menu.style.top = `${e.clientY}px`;
        menu.style.left = `${e.clientX}px`;
        menu.style.background = '#222';
        menu.style.color = '#fff';
        menu.style.padding = '8px';
        menu.style.borderRadius = '6px';
        menu.style.zIndex = 999999;
        menu.style.cursor = 'pointer';
        menu.textContent = 'Copy image as smart name';

        menu.onclick = async () => {
            try {
                const response = await fetch(img.src);
                const blob = await response.blob();

                // Generate better filename
                let name = img.alt || img.src.split('/').pop().split('?')[0];
                name = name || 'image';

                // Clean filename
                name = name.replace(/[^\w.-]+/g, '_');

                // Clipboard API
                await navigator.clipboard.write([
                    new ClipboardItem({ [blob.type]: blob })
                ]);

                console.log(`Copied as: ${name}`);
            } catch (err) {
                console.error(err);
            }

            menu.remove();
        };

        document.body.appendChild(menu);
    });

    document.addEventListener('click', () => {
        if (menu) menu.remove();
    });
})();