iqdb Ctrl + V

在 iqdb 中使用 Ctrl + V 上传图片

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

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

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            iqdb Ctrl + V
// @name:en         iqdb Ctrl + V
// @namespace       http://tampermonkey.net/
// @version         0.1.4
// @description     在 iqdb 中使用 Ctrl + V 上传图片
// @description:en  Upload image to iqdb by Ctrl + V
// @author          apkipa
// @match           https://*.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 extractTextFromClipboard(event) {
        var clipboardData = event.clipboardData || window.clipboardData;
        return clipboardData.getData('Text');
    }

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

    function isHttpURL(text) {
        var url;
        try {
            url = new URL(text);
        } catch(_) {
            return false;
        }
        return url.protocol === "http:" || url.protocol === "https:";
    }

    function handlePaste(e) {
        var files = extractImageFilesFromClipboard(e);

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

            var fileInput = document.getElementById("file");

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

            fileInput.files = createFilelistFromSingleFile(files[0]);
        }
        else {
            console.log("Paste event does not contain images. Assuming text is pasted.");
            var text = extractTextFromClipboard(e);
            if (text == "") {
                console.log("No text in paste event, event is propagated.");
                return;
            }

            if (!isHttpURL(text)) {
                console.log("Pasted text is not a URL, event is propagated.");
                return;
            }

            var urlInput = document.getElementById("url");

            e.stopPropagation();
            e.preventDefault();

            urlInput.value = text;
        }

        // Automatically submit the pasted data
        // (If this is not desired behavior, comment the line containing submit())
        var formUpload = document.querySelectorAll("input[type=submit]")[0];
        formUpload.form.submit();
    }

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