From 38b107b09465bc794417a181a2aee25b1e237b12 Mon Sep 17 00:00:00 2001 From: Jerome Wu Date: Tue, 20 Nov 2018 00:26:37 +0800 Subject: [PATCH] Fix lint error --- src/browser/index.js | 149 ++++++++++++++++--------------------------- 1 file changed, 56 insertions(+), 93 deletions(-) diff --git a/src/browser/index.js b/src/browser/index.js index f17f378..671bb71 100644 --- a/src/browser/index.js +++ b/src/browser/index.js @@ -1,98 +1,61 @@ -var defaultOptions = { - // workerPath: 'https://cdn.jsdelivr.net/gh/naptha/tesseract.js@0.2.0/dist/worker.js', - corePath: 'https://cdn.jsdelivr.net/gh/naptha/tesseract.js-core@0.1.0/index.js', - langPath: 'https://cdn.jsdelivr.net/gh/naptha/tessdata@gh-pages/3.02/', +const isURL = require('is-url'); + +const defaultOptions = { + // workerPath: 'https://cdn.jsdelivr.net/gh/naptha/tesseract.js@0.2.0/dist/worker.js', + corePath: 'https://cdn.jsdelivr.net/gh/naptha/tesseract.js-core@0.1.0/index.js', + langPath: 'https://cdn.jsdelivr.net/gh/naptha/tessdata@gh-pages/3.02/', +}; + +if (process.env.NODE_ENV === 'development') { + console.debug('Using Development Configuration'); + // eslint-disable-next-line + const { protocol, host } = location; + defaultOptions.workerPath = `${protocol}://${host}/dist/worker.dev.js?nocache=${Math.random().toString(36).slice(3)}`; +} else { + const { version } = require('../../package.json'); + defaultOptions.workerPath = `https://cdn.jsdelivr.net/gh/naptha/tesseract.js@${version}/dist/worker.js`; } -if (process.env.NODE_ENV === "development") { - console.debug('Using Development Configuration') - defaultOptions.workerPath = location.protocol + '//' + location.host + '/dist/worker.dev.js?nocache=' + Math.random().toString(36).slice(3) -}else{ - var version = require('../../package.json').version; - defaultOptions.workerPath = 'https://cdn.jsdelivr.net/gh/naptha/tesseract.js@' + version + '/dist/worker.js' -} +const loadImage = (imageURI) => { + if (isURL(imageURI)) { + return fetch(imageURI) + .then(resp => resp.buffer()); + } + return new Promise(); +}; exports.defaultOptions = defaultOptions; - -exports.spawnWorker = function spawnWorker(instance, workerOptions){ - if(window.Blob && window.URL){ - var blob = new Blob(['importScripts("' + workerOptions.workerPath + '");']) - var worker = new Worker(window.URL.createObjectURL(blob)); - }else{ - var worker = new Worker(workerOptions.workerPath) - } - - worker.onmessage = function(e){ - var packet = e.data; - instance._recv(packet) - } - return worker -} - -exports.terminateWorker = function(instance){ - instance.worker.terminate() -} - -exports.sendPacket = function sendPacket(instance, packet){ - loadImage(packet.payload.image, function(img){ - packet.payload.image = img - instance.worker.postMessage(packet) - }) -} - - -function loadImage(image, cb){ - if(typeof image === 'string'){ - if(/^\#/.test(image)){ - // element css selector - return loadImage(document.querySelector(image), cb) - }else if(/(blob|data)\:/.test(image)){ - // data url - var im = new Image - im.src = image; - im.onload = e => loadImage(im, cb); - return - }else{ - var xhr = new XMLHttpRequest(); - xhr.open('GET', image, true) - xhr.responseType = "blob"; - xhr.onload = e => loadImage(xhr.response, cb); - xhr.onerror = function(e){ - if(/^https?:\/\//.test(image) && !/^https:\/\/crossorigin.me/.test(image)){ - console.debug('Attempting to load image with CORS proxy') - loadImage('https://crossorigin.me/' + image, cb) - } - } - xhr.send(null) - return - } - }else if(image instanceof File){ - // files - var fr = new FileReader() - fr.onload = e => loadImage(fr.result, cb); - fr.readAsDataURL(image) - return - }else if(image instanceof Blob){ - return loadImage(URL.createObjectURL(image), cb) - }else if(image.getContext){ - // canvas element - return loadImage(image.getContext('2d'), cb) - }else if(image.tagName == "IMG" || image.tagName == "VIDEO"){ - // image element or video element - var c = document.createElement('canvas'); - c.width = image.naturalWidth || image.videoWidth; - c.height = image.naturalHeight || image.videoHeight; - var ctx = c.getContext('2d'); - ctx.drawImage(image, 0, 0); - return loadImage(ctx, cb) - }else if(image.getImageData){ - // canvas context - var data = image.getImageData(0, 0, image.canvas.width, image.canvas.height); - return loadImage(data, cb) - }else{ - return cb(image) - } - throw new Error('Missing return in loadImage cascade') - -} +exports.spawnWorker = (instance, workerOptions) => { + let worker; + if (window.Blob && window.URL) { + const blob = new Blob([`importScripts("${workerOptions.workerPath}");`]); + worker = new Worker(window.URL.createObjectURL(blob)); + } else { + worker = new Worker(workerOptions.workerPath); + } + + worker.onmessage = ({ data }) => { + instance._recv(data); + }; + + return worker; +}; + +exports.terminateWorker = (instance) => { + instance.worker.terminate(); +}; + +exports.sendPacket = (instance, packet) => { + loadImage(packet.payload.image) + .then(buf => new Uint8Array(buf)) + .then((img) => { + instance.worker.postMessage({ + ...packet, + payload: { + ...packet.payload, + image: Array.from(img), + }, + }); + }); +};