-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
196 lines (165 loc) · 6.57 KB
/
script.js
File metadata and controls
196 lines (165 loc) · 6.57 KB
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
const dropZone = document.getElementById('dropZone');
const fileInput = document.getElementById('fileInput');
const preview = document.getElementById('preview');
const solveBtn = document.getElementById('solveBtn');
const cluesInput = document.getElementById('cluesInput');
const resultsArea = document.getElementById('resultsArea');
const loading = document.getElementById('loading');
const statusIndicator = document.getElementById('statusIndicator');
let selectedFile = null;
// --- Drag & Drop ---
dropZone.addEventListener('dragover', (e) => {
e.preventDefault();
dropZone.classList.add('dragover');
});
dropZone.addEventListener('dragleave', () => {
dropZone.classList.remove('dragover');
});
dropZone.addEventListener('drop', (e) => {
e.preventDefault();
dropZone.classList.remove('dragover');
if (e.dataTransfer.files.length) {
handleFile(e.dataTransfer.files[0]);
}
});
dropZone.addEventListener('click', () => fileInput.click());
fileInput.addEventListener('change', (e) => {
if (e.target.files.length) {
handleFile(e.target.files[0]);
}
});
function handleFile(file) {
if (!file.type.startsWith('image/')) {
alert('Please upload an image file.');
return;
}
selectedFile = file;
const reader = new FileReader();
reader.onload = (e) => {
preview.src = e.target.result;
preview.style.display = 'block';
// Hide the placeholders
const zoneContent = dropZone.querySelector('.zone-content');
if (zoneContent) zoneContent.style.display = 'none';
};
reader.readAsDataURL(file);
}
// --- Solve Logic ---
solveBtn.addEventListener('click', async () => {
if (!selectedFile) {
alert('Please upload an image first.');
return;
}
const clues = cluesInput.value.trim();
if (!clues) {
alert('Please paste the challenge text / clues.');
return;
}
// UI Loading State
solveBtn.disabled = true;
solveBtn.innerHTML = '<span class="btn-icon">⏳</span> SCANNING...';
// Reset Results
resultsArea.innerHTML = '';
loading.style.display = 'block';
// Update Status
if (statusIndicator) {
statusIndicator.innerHTML = '<span class="dot active"></span> ANALYZING';
}
const formData = new FormData();
formData.append('image', selectedFile);
formData.append('clues', clues);
try {
const response = await fetch('/api/solve', {
method: 'POST',
body: formData
});
const data = await response.json();
if (data.error) {
renderError(data.error);
if (statusIndicator) {
statusIndicator.innerHTML = '<span class="dot" style="background: #ef4444; box-shadow: none;"></span> ERROR';
}
} else {
renderResults(data);
if (statusIndicator) {
statusIndicator.innerHTML = '<span class="dot active" style="background: #4ade80; box-shadow: 0 0 10px #4ade80;"></span> COMPLETE';
}
}
} catch (err) {
renderError("Network or Server Error: " + err.message);
if (statusIndicator) {
statusIndicator.innerHTML = '<span class="dot" style="background: #ef4444; box-shadow: none;"></span> ERROR';
}
} finally {
solveBtn.disabled = false;
solveBtn.innerHTML = '<span class="btn-icon">⚡</span> INITIATE SCAN';
loading.style.display = 'none';
// Ensure preview stays visible
if (selectedFile) {
const zoneContent = dropZone.querySelector('.zone-content');
if (zoneContent) zoneContent.style.display = 'none';
}
}
});
function renderResults(data) {
let html = '';
// Render Grid
if (data.grid && data.grid.length > 0) {
html += '<div style="margin-bottom: 2rem; border-bottom: 1px solid rgba(255,255,255,0.1); padding-bottom: 1rem;">';
html += '<div style="font-weight: bold; margin-bottom: 0.5rem; color: var(--text-secondary); letter-spacing: 0.1em; font-size: 0.8rem;">DETECTED MATRIX:</div>';
html += '<div style="letter-spacing: 0.5em; line-height: 1.5em; font-family: monospace; color: var(--text-primary);">';
data.grid.forEach(row => {
html += `<div>${row.join('')}</div>`;
});
html += '</div></div>';
}
// Render Solutions
html += '<div style="font-weight: bold; margin-bottom: 1rem; color: var(--text-secondary); letter-spacing: 0.1em; font-size: 0.8rem;">IDENTIFIED PATTERNS:</div>';
if (data.solutions && data.solutions.length > 0) {
data.solutions.forEach(item => {
const hasMatch = item.found && item.found.length > 0;
const matchClass = hasMatch ? 'matches' : 'no-match';
let matchHtml = '[Not Found]';
if (hasMatch) {
matchHtml = item.found.map(word =>
`<span class="found-word" onclick="copyToClipboard('${word}', this)" title="Click to copy">${word}</span>`
).join(', ');
}
html += `
<div class="result-item">
<span class="pattern">${item.pattern}</span>
<span style="color: #444;">➜</span>
<span class="${matchClass}">${matchHtml}</span>
</div>
`;
});
} else {
html += '<div style="color: var(--text-muted);">No patterns found in input text.</div>';
}
resultsArea.innerHTML = html;
}
// Clipboard Helper
async function copyToClipboard(text, element) {
try {
await navigator.clipboard.writeText(text);
// Visual feedback
const originalText = element.innerText;
element.innerText = 'COPIED!';
element.classList.add('copied');
setTimeout(() => {
element.innerText = originalText;
element.classList.remove('copied');
}, 800);
} catch (err) {
console.error('Failed to copy:', err);
}
}
function renderError(msg) {
resultsArea.innerHTML = `
<div style="display: flex; flex-direction: column; align-items: center; justify-content: center; height: 100%; color: #ef4444;">
<div style="font-size: 2rem; margin-bottom: 0.5rem;">⚠️</div>
<div style="font-weight: 700;">SYSTEM ERROR</div>
<div style="font-size: 0.9rem; margin-top: 0.2rem; opacity: 0.8;">${msg}</div>
</div>
`;
}