/
auto-login.js
63 lines (49 loc) · 1.89 KB
/
auto-login.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
console.log('auto-login.js loaded');
// select span with class="input-group-addon"
const $span = document.querySelector('.input-group-addon');
// select image inside span
const $img = $span.querySelector('img');
// convert image to base64
function getDataUrl(img) {
// Create canvas
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
// Set width and height
canvas.width = img.width;
canvas.height = img.height;
// Draw the image
ctx.drawImage(img, 0, 0);
return canvas.toDataURL('image/jpeg');
}
const dataUrl = getDataUrl($img);
console.log(dataUrl);
// parameters that need to be passed to the OCR API, as a FormData object
const formData = new FormData();
formData.append('apikey', '030d69f19088957'); // replace with your own API key? hope this is okay to put out on github
formData.append('base64Image', dataUrl);
const url = 'https://api.ocr.space/parse/image';
// Send a request to the OCR API
fetch(url, {
method: 'POST',
body: formData
// Use json() to interpret the returned information
}).then(response => response.json())
.then((jsonData) => {
const parsedResults = jsonData['ParsedResults'];
const errorMessage = jsonData['ErrorMessage'];
// If the returned result is not empty
if (parsedResults != null) {
// Storing details on each result
parsedResults.forEach((value) => {
let exitCode = value['FileParseExitCode'];
let parsedText = value['ParsedText'];
let errorMessage = value['ErrorMessage'];
// If the result is successful, fill form
console.log(parsedText);
const captchabox = document.querySelector('input[name="verif_box"]');
captchabox.value = parsedText;
});
} else { // If the returned result is empty (need to work on error conditions)
console.log('Error: ' + errorMessage);
}
})