-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxss.js
41 lines (34 loc) · 1.11 KB
/
xss.js
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
import DOMPurify from "dompurify";
const inputField = DOMPurify.sanitize(document.getElementById("user-input"));
const outputField = document.getElementById("output");
function sanitize(input) {
return DOMPurify.sanitize(input);
}
function displayUserInput() {
const userInput = sanitize(inputField.value);
outputField.textContent = userInput;
inputField.value = ""; // Clear the input field after displaying the user's input
}
inputField.addEventListener("input", displayUserInput);
function handleError(error) {
console.error(error); // Log any errors that occur during sanitization
}
// Add error handling to the sanitize function
function sanitize(input) {
try {
return DOMPurify.sanitize(input);
} catch (error) {
handleError(error);
}
}
// Handle edge cases
function displayUserInput() {
const userInput = sanitize(inputField.value);
if (userInput === "") {
// Handle the case where the user enters a blank string
outputField.textContent = "";
} else {
outputField.textContent = userInput;
inputField.value = ""; // Clear the input field after displaying the user's input
}
}