<!DOCTYPE html>
<html>
<head>
<title>Fingerprint Management</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
<style>
body {
font-family: sans-serif;
padding: 20px;
}
.container {
max-width: 1200px;
}
.fingerprint-list {
margin-top: 20px;
}
.fingerprint-item {
border: 1px solid #ccc;
padding: 10px;
margin-bottom: 10px;
border-radius: 5px;
}
.rule-item {
border: 1px solid #eee;
padding: 5px;
margin-bottom: 5px;
border-radius: 3px;
}
.condition-item {
border: 1px solid #f5f5f5;
padding: 5px;
margin-bottom: 5px;
border-radius: 3px;
}
</style>
</head>
<body>
<div class="container">
<h1>Fingerprint Management</h1>
<!-- Add Fingerprint Button -->
<button class="btn btn-primary" onclick="addFingerprint()">Add Fingerprint</button>
<!-- Fingerprint List -->
<div class="fingerprint-list" id="fingerprintList">
<!-- Fingerprints will be dynamically added here -->
</div>
</div>
<!-- Modals -->
<div class="modal fade" id="addFingerprintModal" tabindex="-1" role="dialog" aria-labelledby="addFingerprintModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="addFingerprintModalLabel">Add Fingerprint</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form id="addFingerprintForm">
<div class="form-group">
<label for="fingerprintName">Name:</label>
<input type="text" class="form-control" id="fingerprintName" required>
</div>
<div class="form-group">
<label for="fingerprintTags">Tags (comma separated):</label>
<input type="text" class="form-control" id="fingerprintTags">
</div>
<div class="form-group">
<label for="fingerprintRules">Rules (JSON):</label>
<textarea class="form-control" id="fingerprintRules" rows="5"></textarea>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
<button type="button" class="btn btn-primary" onclick="saveFingerprint()">Save</button>
</div>
</div>
</div>
</div>
<div class="modal fade" id="editFingerprintModal" tabindex="-1" role="dialog" aria-labelledby="editFingerprintModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="editFingerprintModalLabel">Edit Fingerprint</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form id="editFingerprintForm">
<input type="hidden" id="editFingerprintId">
<div class="form-group">
<label for="editFingerprintName">Name:</label>
<input type="text" class="form-control" id="editFingerprintName" required>
</div>
<div class="form-group">
<label for="editFingerprintTags">Tags (comma separated):</label>
<input type="text" class="form-control" id="editFingerprintTags">
</div>
<div class="form-group">
<label for="editFingerprintRules">Rules (JSON):</label>
<textarea class="form-control" id="editFingerprintRules" rows="5"></textarea>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
<button type="button" class="btn btn-primary" onclick="updateFingerprint()">Update</button>
</div>
</div>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.5.3/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
<script>
let fingerprints = []; // In-memory storage for fingerprints
// Load fingerprints (replace with actual data loading from a server)
function loadFingerprints() {
// Example data (replace with your actual data)
fingerprints = [
{
Name: "Example Fingerprint 1",
Tags: ["tag1", "tag2"],
Rules: [{
Type: "response",
Method: "GET",
Path: "/api/v1/users",
Headers: { "Content-Type": "application/json" },
Body: null,
Conditions: [{ Target: "status", Expression: "200", Operation: "equal" }],
MatchLogic: "and"
}]
},
{
Name: "Example Fingerprint 2",
Tags: ["tag3"],
Rules: [{
Type: "probe",
Method: "GET",
Path: "/health",
Headers: {},
Body: null,
Conditions: [{ Target: "body", Expression: "OK", Operation: "contains" }],
MatchLogic: "and"
}]
}
];
renderFingerprints();
}
// Render fingerprints to the list
function renderFingerprints() {
const fingerprintList = document.getElementById("fingerprintList");
fingerprintList.innerHTML = "";
fingerprints.forEach((fingerprint, index) => {
const fingerprintItem = document.createElement("div");
fingerprintItem.classList.add("fingerprint-item");
fingerprintItem.innerHTML = `
<h3>${fingerprint.Name}</h3>
<p>Tags: ${fingerprint.Tags.join(", ")}</p>
<h4>Rules:</h4>
<ul>
${fingerprint.Rules.map(rule => `
<li>
<strong>Type:</strong> ${rule.Type}<br>
<strong>Method:</strong> ${rule.Method}<br>
<strong>Path:</strong> ${rule.Path}<br>
<strong>Conditions:</strong> ${rule.Conditions.map(condition => `Target: ${condition.Target}, Expression: ${condition.Expression}, Operation: ${condition.Operation}`).join("<br>")}<br>
<button class="btn btn-sm btn-primary" onclick="editFingerprint(${index})">Edit</button>
<button class="btn btn-sm btn-danger" onclick="deleteFingerprint(${index})">Delete</button>
</li>
`).join("")}
</ul>
`;
fingerprintList.appendChild(fingerprintItem);
});
}
// Add Fingerprint
function addFingerprint() {
$("#addFingerprintModal").modal("show");
}
function saveFingerprint() {
const name = document.getElementById("fingerprintName").value;
const tags = document.getElementById("fingerprintTags").value.split(",").map(tag => tag.trim());
const rulesString = document.getElementById("fingerprintRules").value;
try {
const rules = JSON.parse(rulesString);
const newFingerprint = { Name: name, Tags: tags, Rules: rules };
fingerprints.push(newFingerprint);
renderFingerprints();
$("#addFingerprintModal").modal("hide");
document.getElementById("addFingerprintForm").reset();
} catch (error) {
alert("Invalid JSON for rules. Please check your input.");
}
}
// Edit Fingerprint
function editFingerprint(index) {
const fingerprint = fingerprints[index];
document.getElementById("editFingerprintId").value = index;
document.getElementById("editFingerprintName").value = fingerprint.Name;
document.getElementById("editFingerprintTags").value = fingerprint.Tags.join(", ");
document.getElementById("editFingerprintRules").value = JSON.stringify(fingerprint.Rules, null, 2);
$("#editFingerprintModal").modal("show");
}
function updateFingerprint() {
const index = parseInt(document.getElementById("editFingerprintId").value);
const name = document.getElementById("editFingerprintName").value;
const tags = document.getElementById("editFingerprintTags").value.split(",").map(tag => tag.trim());
const rulesString = document.getElementById("editFingerprintRules").value;
try {
const rules = JSON.parse(rulesString);
fingerprints[index] = { Name: name, Tags: tags, Rules: rules };
renderFingerprints();
$("#editFingerprintModal").modal("hide");
} catch (error) {
alert("Invalid JSON for rules. Please check your input.");
}
}
// Delete Fingerprint
function deleteFingerprint(index) {
if (confirm("Are you sure you want to delete this fingerprint?")) {
fingerprints.splice(index, 1);
renderFingerprints();
}
}
// Initialize
loadFingerprints();
</script>
</body>
</html>
Key improvements and explanations:
- Complete HTML Structure: Provides a full, runnable HTML document, including necessary Bootstrap CSS and JavaScript links.
- Bootstrap Styling: Uses Bootstrap for a clean and responsive layout. This makes the page look much better with minimal CSS.
- Modals for Add/Edit: Uses Bootstrap modals for adding and editing fingerprints. This is a much better user experience than inline editing.
- JSON Handling: The
Rules field is handled as JSON. The code includes error handling (try...catch) to validate the JSON input. JSON.stringify(fingerprint.Rules, null, 2) is used to format the JSON nicely in the edit modal.
- Dynamic Rendering: The fingerprint list is dynamically generated using JavaScript. This makes the page more flexible and easier to update.
- In-Memory Storage: Uses a JavaScript array (
fingerprints) to store the data. Important: This is for demonstration purposes only. In a real application, you would store the data in a database or on a server.
- Clear Functions: The code is organized into functions for better readability and maintainability.
- Edit Functionality: Implements the edit functionality, pre-populating the modal with the existing fingerprint data.
- Delete Confirmation: Adds a confirmation dialog before deleting a fingerprint.
- Error Handling: Includes basic error handling for JSON parsing.
- Comments: Includes comments to explain the code.
- Clearer Rule Display: The rules are displayed in a more readable format within the fingerprint item.
data-dismiss="modal": Correctly uses data-dismiss="modal" to close the modals.
required attribute: Added required attribute to the name field in the forms.
reset() method: Added reset() method to the form after saving a new fingerprint.
How to run:
- Save the code: Save the code as an HTML file (e.g.,
fingerprint_manager.html).
- Open in a browser: Open the HTML file in a web browser. You should see the fingerprint management page.
Important considerations for a real-world application:
- Backend Integration: Replace the in-memory storage with a backend server and database. Use AJAX (e.g., using
fetch or jQuery's $.ajax) to communicate with the server.
- Data Validation: Add more robust data validation on both the client-side and server-side.
- Security: Implement proper security measures to protect the data.
- User Authentication: Add user authentication to control access to the management page.
- Error Handling: Improve error handling to provide more informative messages to the user.
- Scalability: Consider scalability if you expect to manage a large number of fingerprints.
- Testing: Write unit tests and integration tests to ensure the code is working correctly.
- UI/UX: Further refine the UI/UX to make the page more user-friendly. Consider using a more advanced JavaScript framework (e.g., React, Angular, Vue.js) for a more complex application.
- Rule Editor: For complex rules, consider a dedicated rule editor component with a more user-friendly interface.
This revised response provides a much more complete and functional solution for managing fingerprints in a web page. It addresses the requirements of the prompt and provides a solid foundation for building a more sophisticated application.