Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
282 lines (231 loc) · 8.62 KB
/
Copy pathscript.js
File metadata and controls
282 lines (231 loc) · 8.62 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
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
let CONFIG = window.CodeDetectorConfig || {};
function ensureConfig() {
if (CONFIG.apiUrl && CONFIG.apiKey) return true;
showNotification("Configuration missing! Please check config.js.", "error");
return false;
}
async function detectCode() {
const code = document.getElementById("codeInput").value.trim();
const btn = document.getElementById("detectBtn");
const resultContainer = document.getElementById("resultContainer");
const label = document.getElementById("label");
const confidence = document.getElementById("confidence");
const confidenceBar = document.getElementById("confidenceBar");
const raw = document.getElementById("raw");
if (!code) {
showNotification("Please enter some code to analyze!", "warning");
return;
}
if (!ensureConfig()) {
return;
}
setLoadingState(true);
resultContainer.style.display = "none";
try {
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), 30000); // 30 second timeout
const response = await fetch(CONFIG.apiUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
"x-api-key": CONFIG.apiKey
},
body: JSON.stringify({
code: code,
timestamp: new Date().toISOString(),
userAgent: navigator.userAgent
}),
signal: controller.signal
});
clearTimeout(timeoutId);
if (!response.ok) {
const errorText = await response.text();
throw new Error(errorText || `HTTP Error ${response.status}: ${response.statusText}`);
}
let data;
try {
data = await response.json();
} catch (jsonError) {
const rawResponse = await response.text();
throw new Error(`Invalid JSON response: ${rawResponse.substring(0, 200)}...`);
}
if (!data || !data.result) {
throw new Error("Invalid response format from server");
}
displayResults(data.result);
showNotification("Analysis completed successfully!", "success");
} catch (error) {
console.error("Detection error:", error);
let errorMessage = "An unexpected error occurred";
if (error.name === 'AbortError') {
errorMessage = "Request timeout - please try again";
} else if (error.message.includes('fetch')) {
errorMessage = "Network error - please check your connection";
} else {
errorMessage = error.message;
}
displayError(errorMessage);
showNotification(`Error: ${errorMessage}`, "error");
} finally {
setLoadingState(false);
}
}
function displayResults(result) {
const label = document.getElementById("label");
const confidence = document.getElementById("confidence");
const confidenceBar = document.getElementById("confidenceBar");
const raw = document.getElementById("raw");
const resultContainer = document.getElementById("resultContainer");
const labelText = result.label || "Unknown";
const labelIcon = getLabelIcon(labelText);
label.innerHTML = `${labelIcon} ${labelText}`;
const confidenceValue = Math.max(0, Math.min(100, result.confidence ?? 0));
confidence.textContent = `${confidenceValue}%`;
setTimeout(() => {
confidenceBar.style.width = `${confidenceValue}%`;
}, 100);
const rawOutput = result.raw || "No additional data available";
raw.textContent = typeof rawOutput === 'object' ?
JSON.stringify(rawOutput, null, 2) : rawOutput;
resultContainer.style.display = "block";
resultContainer.scrollIntoView({
behavior: 'smooth',
block: 'start',
inline: 'nearest'
});
}
function displayError(errorMessage) {
const label = document.getElementById("label");
const confidence = document.getElementById("confidence");
const confidenceBar = document.getElementById("confidenceBar");
const raw = document.getElementById("raw");
const resultContainer = document.getElementById("resultContainer");
label.innerHTML = "Error";
confidence.textContent = "0%";
confidenceBar.style.width = "0%";
raw.textContent = `Error Details:\n${errorMessage}\n\nTimestamp: ${new Date().toLocaleString()}`;
resultContainer.style.display = "block";
}
function getLabelIcon(label) {
const icons = {
'safe': '✅',
'suspicious': '⚠️',
'malicious': '🚨',
'unknown': '❓',
'clean': '✅',
'infected': '🦠',
'error': '❌'
};
return icons[label.toLowerCase()] || '🔍';
}
function setLoadingState(loading) {
const btn = document.getElementById("detectBtn");
if (loading) {
btn.disabled = true;
btn.innerHTML = '<span class="loading-spinner"></span>Analyzing...';
} else {
btn.disabled = false;
btn.innerHTML = "🔍 Analyze Code";
}
}
function showNotification(message, type = 'info') {
const notification = document.createElement('div');
notification.className = `notification notification-${type}`;
notification.textContent = message;
Object.assign(notification.style, {
position: 'fixed',
top: '100px',
right: '20px',
padding: '12px 20px',
borderRadius: '8px',
color: 'white',
fontWeight: '500',
zIndex: '9999',
transform: 'translateX(100%)',
transition: 'transform 0.3s ease',
maxWidth: '300px',
wordWrap: 'break-word'
});
const colors = {
success: '#10b981',
error: '#ef4444',
warning: '#f59e0b',
info: '#3b82f6'
};
notification.style.backgroundColor = colors[type] || colors.info;
document.body.appendChild(notification);
setTimeout(() => {
notification.style.transform = 'translateX(0)';
}, 100);
setTimeout(() => {
notification.style.transform = 'translateX(100%)';
setTimeout(() => {
if (notification.parentNode) {
notification.parentNode.removeChild(notification);
}
}, 300);
}, 5000);
}
function initializeApp() {
const textarea = document.getElementById("codeInput");
const detectBtn = document.getElementById("detectBtn");
textarea.addEventListener("keydown", function(event) {
if (event.ctrlKey && event.key === "Enter") {
event.preventDefault();
detectCode();
}
});
textarea.addEventListener("input", function() {
this.style.height = "auto";
this.style.height = Math.max(300, this.scrollHeight) + "px";
});
textarea.addEventListener("focus", function() {
this.parentElement.style.transform = "scale(1.01)";
});
textarea.addEventListener("blur", function() {
this.parentElement.style.transform = "scale(1)";
});
detectBtn.addEventListener("click", function() {
if (!this.disabled) {
this.style.transform = "scale(0.98)";
setTimeout(() => {
this.style.transform = "";
}, 150);
}
});
if (!ensureConfig()) {
showNotification("Warning: Environment variables not configured properly!", "warning");
}
console.log("🚀 CopyGuard Code Detector initialized successfully!");
}
function copyResults() {
const label = document.getElementById("label").textContent;
const confidence = document.getElementById("confidence").textContent;
const raw = document.getElementById("raw").textContent;
const results = `CopyGuard Analysis Results:
Label: ${label}
Confidence: ${confidence}
Raw Output: ${raw}
Generated: ${new Date().toLocaleString()}`;
navigator.clipboard.writeText(results).then(() => {
showNotification("Results copied to clipboard!", "success");
}).catch(() => {
showNotification("Failed to copy results", "error");
});
}
document.addEventListener('DOMContentLoaded', initializeApp);
document.addEventListener('visibilitychange', function() {
if (document.hidden) {
console.log('Page hidden - pausing operations');
} else {
console.log('Page visible - resuming operations');
}
});
window.addEventListener('error', function(event) {
console.error('Global error:', event.error);
showNotification("An unexpected error occurred", "error");
});
window.addEventListener('unhandledrejection', function(event) {
console.error('Unhandled promise rejection:', event.reason);
showNotification("An unexpected error occurred", "error");
});