iqdb Ctrl + V

Upload image to iqdb by Ctrl + V

As of 2021-06-14. See the latest version.

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 or Violentmonkey 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            iqdb Ctrl + V
// @name:en         iqdb Ctrl + V
// @namespace       http://tampermonkey.net/
// @version         0.1.2
// @description     在 iqdb 中使用 Ctrl + V 上传图片
// @description:en  Upload image to iqdb by Ctrl + V
// @author          apkipa
// @match           https://www.iqdb.org
// @grant           none
// ==/UserScript==

(function() {
    'use strict';

    function extractImageFilesFromClipboard(event) {
        var clipboardData = event.clipboardData || window.clipboardData;
        var files = clipboardData.files;
        var a = new DataTransfer();

        for (var i = 0; i < files.length; i++) {
            if (files[i].type.indexOf("image") !== -1) {
                a.items.add(files[i]);
            }
        }

        if (a.files.length < 1) {
            return null;
        }

        return a.files;
    }

    function createFilelistFromSingleFile(file) {
        var a = new DataTransfer();
        a.items.add(file);
        return a.files;
    }

    function handlePaste(e) {
        var clipboardData, pastedData;

        var files = extractImageFilesFromClipboard(e);

        if (files !== null) {
            e.stopPropagation();
            e.preventDefault();

            var fileInput = document.getElementById("file");
            var formUpload = document.querySelectorAll("input[type=submit]")[0];

            /* ? Not working here
            fileInput.addEventListener("change", () => {
                formUpload.form.submit();
            });
            */

            fileInput.files = createFilelistFromSingleFile(files[0]);

            // Automatically submit the pasted image
            // (If this is not desired behavior, comment the next line)
            formUpload.form.submit();
        }
        else {
            console.log("Not an image, paste event propagated");
        }
    }

    window.addEventListener('paste', handlePaste);
})();