有道 ocr Ctrl+V

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

目前為 2020-07-30 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 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.1
// @description  在有道 ocr Demo 中使用 Ctrl + V 上传图片
// @author       You
// @match        https://ai.youdao.com/product-ocr.s
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    /*
    function convertImgBlobToBase64() {
        var canvas = document.getElementById("mycanvas");
    }
    */

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

    function handlePaste(e) {
        var clipboardData, pastedData;

        // Stop data actually being pasted into div
        e.stopPropagation();
        e.preventDefault();

        // Get pasted data via clipboard API
        clipboardData = e.clipboardData || window.clipboardData;
        pastedData = clipboardData.items[0];

        if (pastedData.type.indexOf("image") !== -1) {
            //alert("图片");
            var file = pastedData.getAsFile();
            var urlBlob = (window.URL || window.webkitURL).createObjectURL(file);
            getBase64(pastedData.getAsFile(), 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, "auto", "1");
            });
            //ocr(v1, "auto", "1");
        }
        else {
            alert("请粘贴图片!");
        }
    }

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