在有道 ocr Demo 中使用 Ctrl + V 上传图片
目前為
// ==UserScript==
// @name 有道 ocr Ctrl+V
// @namespace http://tampermonkey.net/
// @version 0.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);
img.src = urlBlob;
ocr(res, "auto", "1");
});
//ocr(v1, "auto", "1");
}
else {
alert("请粘贴图片!");
}
}
document.getElementsByClassName('demo')[0].addEventListener('paste', handlePaste);
})();