File size: 2,590 Bytes
a0004ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
let loadingInterval;

function handlePaste(event) {
    const items = (event.clipboardData || event.originalEvent.clipboardData).items;
    for (const item of items) {
        if (item.type.indexOf("image") === 0) {
            const blob = item.getAsFile();
            const reader = new FileReader();
            reader.onload = function(event) {
                const img = document.getElementById("pasted-image");
                img.src = event.target.result;
                img.style.display = "block";
            };
            reader.readAsDataURL(blob);
        }
    }
}

function saveImage() {
    const img = document.getElementById("pasted-image");
    if (img.src) {
        fetch('/save_pasted_image', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({ image: img.src })
        })
        .then(response => response.json())
        .then(data => {
            console.log(data.message);
            alert("Image saved successfully!");
        })
        .catch(console.error);
    } else {
        alert("No image to save!");
    }
}

function performOCR() {
    clearOCRResult();
    showLoading();
    disableButton();
    fetch('/imgocr', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json'
        }
    })
    .then(response => response.json())
    .then(data => {
        document.getElementById('ocr-result').innerText = data.result;
        hideLoading();
        enableButton();
    })
    .catch(error => {
        console.error(error);
        hideLoading();
        enableButton();
    });
}

function showLoading() {
    const loadingElement = document.getElementById('loading');
    loadingElement.style.display = 'block';
    let dots = 0;
    loadingInterval = setInterval(() => {
        dots = (dots + 1) % 4;
        loadingElement.innerText = 'Loading' + '.'.repeat(dots);
    }, 500);
}

function hideLoading() {
    clearInterval(loadingInterval);
    const loadingElement = document.getElementById('loading');
    loadingElement.style.display = 'none';
}

function clearOCRResult() {
    document.getElementById('ocr-result').innerText = '';
}

function disableButton() {
    const button = document.querySelector('button[onclick="performOCR()"]');
    button.disabled = true;
    button.style.backgroundColor = '#cccccc';
}

function enableButton() {
    const button = document.querySelector('button[onclick="performOCR()"]');
    button.disabled = false;
    button.style.backgroundColor = '#007bff';
}