有道 ocr Ctrl + V

在有道 ocr Demo 中使用 Ctrl + V 上传图片

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         有道 ocr Ctrl + V
// @namespace    http://tampermonkey.net/
// @version      0.1.3
// @description  在有道 ocr Demo 中使用 Ctrl + V 上传图片
// @author       apkipa
// @match        https://ai.youdao.com/product-ocr-*.s
// @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;
    }

    var $index = 0;

    // From product-ocr-common.js
    if (location.pathname === "/product-ocr-receipt.s") {
        $index = 1;
    } else if (location.pathname === "/product-ocr-id.s") {
        $index = 2;
    } else if (location.pathname === "/product-ocr-bizcard.s") {
        $index = 3;
    } else if (location.pathname === "/product-ocr-question.s") {
        $index = 4;
    } else if (location.pathname === "/product-ocr-table.s") {
        $index = 5;
    } else if (location.pathname === "/product-ocr-hand.s") {
        $index = 6;
    } else if (location.pathname === "/product-ocr-print.s") {
        $index = 7;
    } else {
        console.error("URL path is unknown:", location.pathname)
    }

    function getBase64(file, callback) {
        var reader = new FileReader();
        reader.onload = function () {
            callback(reader.result);
        };
        reader.onerror = function (error) {
            console.log('Error: ', error);
        };
        reader.readAsDataURL(file);
    }

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

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

            var file = files[0];
            var urlBlob = (window.URL || window.webkitURL).createObjectURL(file);
            getBase64(file, function (res) {
                //var index = $('.service_active').eq(0).attr('dataindex');
                var img = document.getElementById('preview' + $index);
                if (img.src) {
                    (window.URL || window.webkitURL).revokeObjectURL(img.src);
                }
                img.src = urlBlob;

                ocr(res, "", $index);
            });
        }
        else {
            console.log("Not an image, paste event propagated");
        }
    }

    //document.getElementsByClassName('demo')[0].addEventListener('paste', handlePaste);
    window.addEventListener('paste', handlePaste);
})();