Smart Copy Image

Custom "Copy Image As" with better filename

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

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

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

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.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==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();
    });
})();