有道 ocr Ctrl + V

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

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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);
})();