Smart Copy Image

Custom "Copy Image As" with better filename

スクリプトをインストールするには、Tampermonkey, GreasemonkeyViolentmonkey のような拡張機能のインストールが必要です。

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

スクリプトをインストールするには、TampermonkeyViolentmonkey のような拡張機能のインストールが必要です。

スクリプトをインストールするには、TampermonkeyUserscripts のような拡張機能のインストールが必要です。

このスクリプトをインストールするには、Tampermonkeyなどの拡張機能をインストールする必要があります。

このスクリプトをインストールするには、ユーザースクリプト管理ツールの拡張機能をインストールする必要があります。

(ユーザースクリプト管理ツールは設定済みなのでインストール!)

このスタイルをインストールするには、Stylusなどの拡張機能をインストールする必要があります。

このスタイルをインストールするには、Stylus などの拡張機能をインストールする必要があります。

このスタイルをインストールするには、Stylus tなどの拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

(ユーザースタイル管理ツールは設定済みなのでインストール!)

このスクリプトの質問や評価の投稿はこちら通報はこちらへお寄せください
// ==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();
    });
})();