File size: 11,978 Bytes
51584a9
 
 
 
 
7318db9
 
3453b9b
51584a9
 
 
 
 
7318db9
51584a9
 
7318db9
 
 
 
 
 
3453b9b
51584a9
 
 
3453b9b
 
51584a9
3453b9b
 
 
7318db9
51584a9
 
7318db9
3453b9b
7318db9
3453b9b
 
7318db9
3453b9b
 
7318db9
51584a9
 
3453b9b
51584a9
 
7318db9
51584a9
7318db9
 
 
 
 
 
51584a9
 
3453b9b
7318db9
3453b9b
51584a9
 
3453b9b
7318db9
3453b9b
51584a9
 
7318db9
 
 
 
 
 
 
 
 
 
 
3453b9b
 
 
 
 
7318db9
51584a9
 
3453b9b
 
 
 
51584a9
 
7318db9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3453b9b
 
 
 
51584a9
 
 
3453b9b
 
7318db9
3453b9b
 
 
51584a9
 
3453b9b
 
51584a9
 
3453b9b
 
 
 
51584a9
 
3453b9b
 
7318db9
 
3453b9b
51584a9
 
3453b9b
51584a9
 
3453b9b
51584a9
 
3453b9b
7318db9
 
51584a9
 
7318db9
 
3453b9b
 
7318db9
 
 
 
3453b9b
 
 
 
 
7318db9
51584a9
 
3453b9b
7318db9
3453b9b
7318db9
3453b9b
7318db9
 
3453b9b
7318db9
 
3453b9b
 
7318db9
 
 
3453b9b
 
 
 
 
 
51584a9
 
 
 
7318db9
3453b9b
7318db9
 
51584a9
 
7318db9
 
 
 
51584a9
 
7318db9
3453b9b
7318db9
3453b9b
51584a9
7318db9
3453b9b
51584a9
3453b9b
7318db9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51584a9
7318db9
51584a9
7318db9
 
 
 
51584a9
7318db9
 
 
 
 
 
51584a9
7318db9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3453b9b
 
7318db9
51584a9
7318db9
 
 
 
 
 
 
3453b9b
51584a9
 
 
7318db9
 
51584a9
7318db9
 
 
 
 
 
 
 
 
 
 
 
 
51584a9
 
7318db9
3453b9b
7318db9
 
 
3453b9b
7318db9
51584a9
7318db9
 
 
 
 
 
 
3453b9b
7318db9
3453b9b
7318db9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51584a9
 
7318db9
 
 
 
 
 
 
 
 
 
 
51584a9
 
 
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Screen Analysis & OCR Tool</title>
    <!-- Include Tesseract.js for OCR -->
    <script src="https://unpkg.com/tesseract.js@v2.1.0/dist/tesseract.min.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: Arial, sans-serif;
        }

        body {
            background: #f0f2f5;
            padding: 20px;
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
        }

        .header {
            background: white;
            padding: 20px;
            border-radius: 8px;
            margin-bottom: 20px;
            text-align: center;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }

        .controls {
            background: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
            margin-bottom: 20px;
            display: flex;
            gap: 10px;
            flex-wrap: wrap;
        }

        button {
            padding: 10px 20px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-weight: bold;
            display: flex;
            align-items: center;
            gap: 8px;
            min-width: 120px;
            justify-content: center;
        }

        .start-btn {
            background: #4CAF50;
            color: white;
        }

        .stop-btn {
            background: #f44336;
            color: white;
        }

        .clear-btn {
            background: #2196F3;
            color: white;
        }

        button:disabled {
            background: #ccc;
            cursor: not-allowed;
        }

        .preview-area {
            background: black;
            border-radius: 8px;
            overflow: hidden;
            margin-bottom: 20px;
            position: relative;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }

        #preview {
            width: 100%;
            aspect-ratio: 16/9;
            object-fit: contain;
        }

        .recording-dot {
            position: absolute;
            top: 20px;
            right: 20px;
            width: 12px;
            height: 12px;
            background: red;
            border-radius: 50%;
            animation: pulse 1s infinite;
        }

        @keyframes pulse {
            0% { opacity: 1; }
            50% { opacity: 0.5; }
            100% { opacity: 1; }
        }

        .logs {
            background: white;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
            overflow: hidden;
        }

        .log-entry {
            padding: 20px;
            border-bottom: 1px solid #eee;
            display: grid;
            grid-template-columns: 300px 1fr;
            gap: 20px;
        }

        .screenshot-container {
            position: relative;
        }

        .screenshot {
            width: 100%;
            border-radius: 4px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }

        .change-highlight {
            position: absolute;
            border: 2px solid red;
            background: rgba(255,0,0,0.2);
            pointer-events: none;
        }

        .info-panel {
            display: flex;
            flex-direction: column;
            gap: 15px;
        }

        .timestamp {
            color: #666;
            font-size: 14px;
        }

        .text-content {
            background: #f8f9fa;
            padding: 15px;
            border-radius: 4px;
            font-size: 14px;
            line-height: 1.5;
            max-height: 200px;
            overflow-y: auto;
        }

        .actions {
            display: flex;
            gap: 10px;
            margin-top: 10px;
        }

        .download-btn {
            background: #4CAF50;
            color: white;
            padding: 8px 16px;
            border-radius: 4px;
            text-decoration: none;
            display: inline-flex;
            align-items: center;
            gap: 5px;
            font-size: 14px;
        }

        .processing {
            color: #666;
            font-style: italic;
        }

        @media (max-width: 768px) {
            .log-entry {
                grid-template-columns: 1fr;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>Screen Analysis & OCR Tool</h1>
            <p>Capture, analyze, and extract text from your screen</p>
        </div>

        <div class="controls">
            <button id="startBtn" class="start-btn">โ–ถ Start Capture</button>
            <button id="stopBtn" class="stop-btn" disabled>โฌ› Stop</button>
            <button id="clearBtn" class="clear-btn">๐Ÿ—‘ Clear Logs</button>
        </div>

        <div class="preview-area">
            <video id="preview" autoplay></video>
            <div class="recording-dot" style="display: none;"></div>
        </div>

        <div class="logs" id="logContainer"></div>
    </div>

    <script>
        let mediaStream = null;
        let captureInterval = null;
        let lastImageData = null;
        
        // Initialize Tesseract
        const worker = Tesseract.createWorker();
        
        (async () => {
            await worker.load();
            await worker.loadLanguage('eng');
            await worker.initialize('eng');
        })();

        const startBtn = document.getElementById('startBtn');
        const stopBtn = document.getElementById('stopBtn');
        const clearBtn = document.getElementById('clearBtn');
        const preview = document.getElementById('preview');
        const logContainer = document.getElementById('logContainer');
        const recordingDot = document.querySelector('.recording-dot');

        async function startCapture() {
            try {
                mediaStream = await navigator.mediaDevices.getDisplayMedia({
                    video: { cursor: "always" }
                });

                preview.srcObject = mediaStream;
                startBtn.disabled = true;
                stopBtn.disabled = false;
                recordingDot.style.display = 'block';

                captureInterval = setInterval(captureAndAnalyze, 1000);

                mediaStream.getVideoTracks()[0].onended = stopCapture;

            } catch (err) {
                console.error("Error starting capture:", err);
            }
        }

        function stopCapture() {
            if (mediaStream) {
                mediaStream.getTracks().forEach(track => track.stop());
                preview.srcObject = null;
            }
            clearInterval(captureInterval);
            startBtn.disabled = false;
            stopBtn.disabled = true;
            recordingDot.style.display = 'none';
            lastImageData = null;
        }

        function detectChanges(current, last) {
            const blockSize = 20;
            const threshold = 30;
            const changes = [];
            const width = current.width;
            const height = current.height;

            for (let y = 0; y < height; y += blockSize) {
                for (let x = 0; x < width; x += blockSize) {
                    let diffCount = 0;
                    const maxY = Math.min(y + blockSize, height);
                    const maxX = Math.min(x + blockSize, width);

                    for (let py = y; py < maxY; py++) {
                        for (let px = x; px < maxX; px++) {
                            const i = (py * width + px) * 4;
                            if (Math.abs(current.data[i] - last.data[i]) > threshold ||
                                Math.abs(current.data[i + 1] - last.data[i + 1]) > threshold ||
                                Math.abs(current.data[i + 2] - last.data[i + 2]) > threshold) {
                                diffCount++;
                            }
                        }
                    }

                    if (diffCount > (blockSize * blockSize * 0.3)) {
                        changes.push({
                            x: x / width * 100,
                            y: y / height * 100,
                            width: Math.min(blockSize / width * 100, 100 - x / width * 100),
                            height: Math.min(blockSize / height * 100, 100 - y / height * 100)
                        });
                    }
                }
            }

            return changes;
        }

        async function captureAndAnalyze() {
            const canvas = document.createElement('canvas');
            canvas.width = preview.videoWidth;
            canvas.height = preview.videoHeight;
            
            const ctx = canvas.getContext('2d');
            ctx.drawImage(preview, 0, 0);
            
            const currentImageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
            let changes = [];

            if (lastImageData) {
                changes = detectChanges(currentImageData, lastImageData);
            }

            lastImageData = currentImageData;

            if (changes.length > 0 || !lastImageData) {
                const imageUrl = canvas.toDataURL('image/jpeg', 0.8);
                await addLogEntry(imageUrl, changes);
            }
        }

        async function performOCR(imageUrl) {
            try {
                const result = await worker.recognize(imageUrl);
                return result.data.text;
            } catch (error) {
                console.error('OCR Error:', error);
                return 'Error performing OCR';
            }
        }

        async function addLogEntry(imageUrl, changes) {
            const logEntry = document.createElement('div');
            logEntry.className = 'log-entry';

            const timestamp = new Date().toLocaleString();
            
            // Create initial structure with loading state
            logEntry.innerHTML = `
                <div class="screenshot-container">
                    <img class="screenshot" src="${imageUrl}" alt="Screenshot">
                    ${changes.map(change => `
                        <div class="change-highlight" style="
                            left: ${change.x}%;
                            top: ${change.y}%;
                            width: ${change.width}%;
                            height: ${change.height}%;
                        "></div>
                    `).join('')}
                </div>
                <div class="info-panel">
                    <div class="timestamp">${timestamp}</div>
                    <div class="text-content processing">Processing text extraction...</div>
                    <div class="actions">
                        <a href="${imageUrl}" download="screenshot-${Date.now()}.jpg" class="download-btn">
                            ๐Ÿ’พ Download Image
                        </a>
                    </div>
                </div>
            `;

            logContainer.insertBefore(logEntry, logContainer.firstChild);

            // Perform OCR
            const extractedText = await performOCR(imageUrl);
            
            // Update text content
            const textContent = logEntry.querySelector('.text-content');
            textContent.classList.remove('processing');
            textContent.textContent = extractedText || 'No text detected';
        }

        clearBtn.addEventListener('click', () => {
            logContainer.innerHTML = '';
        });

        startBtn.addEventListener('click', startCapture);
        stopBtn.addEventListener('click', stopCapture);

        // Cleanup when page is closed
        window.addEventListener('beforeunload', () => {
            worker.terminate();
        });
    </script>
</body>
</html>