-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbrowser-example.html
More file actions
237 lines (216 loc) · 7.69 KB
/
Copy pathbrowser-example.html
File metadata and controls
237 lines (216 loc) · 7.69 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>LLM Shield Browser Example</title>
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
max-width: 800px;
margin: 50px auto;
padding: 20px;
background: #f5f5f5;
}
.container {
background: white;
padding: 30px;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
h1 {
color: #333;
margin-bottom: 10px;
}
.subtitle {
color: #666;
margin-bottom: 30px;
}
textarea {
width: 100%;
height: 150px;
padding: 10px;
border: 1px solid #ddd;
border-radius: 4px;
font-family: monospace;
font-size: 14px;
resize: vertical;
}
button {
background: #007bff;
color: white;
border: none;
padding: 12px 24px;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
margin-top: 10px;
}
button:hover {
background: #0056b3;
}
button:disabled {
background: #ccc;
cursor: not-allowed;
}
.result {
margin-top: 20px;
padding: 15px;
border-radius: 4px;
display: none;
}
.result.valid {
background: #d4edda;
border: 1px solid #c3e6cb;
color: #155724;
display: block;
}
.result.invalid {
background: #f8d7da;
border: 1px solid #f5c6cb;
color: #721c24;
display: block;
}
.stats {
margin-top: 20px;
padding: 15px;
background: #e7f3ff;
border-radius: 4px;
font-size: 14px;
}
.stats div {
margin: 5px 0;
}
.badge {
display: inline-block;
padding: 4px 8px;
border-radius: 3px;
font-size: 12px;
font-weight: bold;
margin-left: 10px;
}
.badge.safe {
background: #28a745;
color: white;
}
.badge.warning {
background: #ffc107;
color: black;
}
.badge.danger {
background: #dc3545;
color: white;
}
</style>
</head>
<body>
<div class="container">
<h1>🛡️ LLM Shield WASM Demo</h1>
<p class="subtitle">Test the security scanners in your browser</p>
<label for="input"><strong>Enter text to scan:</strong></label>
<textarea id="input" placeholder="Type something here... Try words like 'spam', 'scam', 'badword'">This is a test message.</textarea>
<button id="scanBtn" onclick="scanText()">Scan Text</button>
<div id="result" class="result"></div>
<div class="stats" id="stats" style="display: none;">
<strong>Scan Statistics:</strong>
<div>Scanner: <span id="scannerName"></span></div>
<div>Risk Score: <span id="riskScore"></span></div>
<div>Processing Time: <span id="processingTime"></span></div>
</div>
</div>
<script type="module">
// Import WASM module
import init, { BanSubstringsScanner } from './pkg/llm_shield_wasm.js';
let scanner;
let startTime;
// Initialize WASM module and create scanner
async function initScanner() {
await init();
// Create scanner with example banned words
scanner = new BanSubstringsScanner(
['spam', 'scam', 'badword', 'malicious', 'phishing'],
false // case_insensitive
);
console.log('✅ Scanner initialized:', scanner.name());
document.getElementById('scanBtn').disabled = false;
}
// Scan text function
window.scanText = async function() {
const input = document.getElementById('input').value;
const resultDiv = document.getElementById('result');
const statsDiv = document.getElementById('stats');
const scanBtn = document.getElementById('scanBtn');
if (!input.trim()) {
resultDiv.className = 'result invalid';
resultDiv.textContent = '⚠️ Please enter some text to scan';
resultDiv.style.display = 'block';
return;
}
// Disable button during scan
scanBtn.disabled = true;
scanBtn.textContent = 'Scanning...';
try {
startTime = performance.now();
// Perform scan
const result = await scanner.scan(input);
const endTime = performance.now();
const duration = (endTime - startTime).toFixed(2);
// Display result
if (result.is_valid) {
resultDiv.className = 'result valid';
resultDiv.innerHTML = `
✅ <strong>Text is SAFE</strong>
<span class="badge safe">PASSED</span>
<br><br>
No security issues detected.
`;
} else {
resultDiv.className = 'result invalid';
resultDiv.innerHTML = `
❌ <strong>Text is BLOCKED</strong>
<span class="badge danger">FAILED</span>
<br><br>
Security scanner detected potential issues.
<br>
<strong>Sanitized:</strong> ${result.sanitized_text || 'N/A'}
`;
}
// Display stats
document.getElementById('scannerName').textContent = scanner.name();
document.getElementById('riskScore').innerHTML = `
${result.risk_score.toFixed(2)}
<span class="badge ${getRiskBadge(result.risk_score)}">${getRiskLevel(result.risk_score)}</span>
`;
document.getElementById('processingTime').textContent = `${duration}ms`;
statsDiv.style.display = 'block';
} catch (error) {
resultDiv.className = 'result invalid';
resultDiv.textContent = `❌ Error: ${error.message}`;
resultDiv.style.display = 'block';
} finally {
scanBtn.disabled = false;
scanBtn.textContent = 'Scan Text';
}
}
function getRiskLevel(score) {
if (score >= 0.9) return 'CRITICAL';
if (score >= 0.7) return 'HIGH';
if (score >= 0.4) return 'MEDIUM';
if (score > 0) return 'LOW';
return 'NONE';
}
function getRiskBadge(score) {
if (score >= 0.7) return 'danger';
if (score >= 0.4) return 'warning';
return 'safe';
}
// Initialize on page load
initScanner().catch(err => {
console.error('Failed to initialize scanner:', err);
document.getElementById('result').className = 'result invalid';
document.getElementById('result').textContent = '❌ Failed to load WASM module: ' + err.message;
document.getElementById('result').style.display = 'block';
});
</script>
</body>
</html>