Smart Copy Image

Custom "Copy Image As" with better filename

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

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

(I already have a user script manager, let me install it!)

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.

(I already have a user style manager, let me install it!)

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