Here's a complete index.html file that fulfills your requirements:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Domain Viewer</title>
<!-- Include Bootstrap CSS from CDN for styling -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
<style>
/* Custom styles for better presentation */
body {
background-color: #f8f9fa;
}
.card-header .btn {
text-decoration: none;
width: 100%;
text-align: left;
color: #007bff;
}
.card-header .btn:hover {
color: #0056b3;
}
.iframe-container {
position: relative;
width: 100%;
padding-bottom: 56.25%; /* 16:9 Aspect Ratio */
height: 0;
overflow: hidden;
}
.iframe-container iframe {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
border: 1px solid #dee2e6;
}
</style>
</head>
<body>
<div class="container">
<h1 class="my-4">Domain Viewer</h1>
<input type="file" id="fileInput" class="form-control-file mb-4" />
<div id="domainAccordion">
<!-- Accordion items will be added here -->
</div>
</div>
<!-- Include jQuery, Popper.js, and Bootstrap JS from CDN -->
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script>
<script script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
<!-- JavaScript code -->
<script>
document.getElementById('fileInput').addEventListener('change', function(event) {
let file = event.target.files[0];
if (!file) return;
let reader = new FileReader();
reader.onload = function(e) {
let content = e.target.result; // the file content as text
processFileContent(content);
};
reader.readAsText(file);
});
function processFileContent(content) {
let lines = content.split('\n');
lines.forEach((line, index) => {
line = line.trim();
if (line) {
try {
let data = JSON.parse(line);
displayDomainData(data, index);
} catch (e) {
console.error('Error parsing JSON:', e);
}
}
});
}
function displayDomainData(data, index) {
let domainAccordion = document.getElementById('domainAccordion');
let card = document.createElement('div');
card.className = 'card';
let cardHeader = document.createElement('div');
cardHeader.className = 'card-header';
cardHeader.id = 'heading' + index;
let h5 = document.createElement('h5');
h5.className = 'mb-0';
let button = document.createElement('button');
button.className = 'btn btn-link';
button.setAttribute('data-toggle', 'collapse');
button.setAttribute('data-target', '#collapse' + index);
button.setAttribute('aria-expanded', 'true');
button.setAttribute('aria-controls', 'collapse' + index);
button.textContent = data.domain;
h5.appendChild(button);
cardHeader.appendChild(h5);
card.appendChild(cardHeader);
let collapseDiv = document.createElement('div');
collapseDiv.id = 'collapse' + index;
collapseDiv.className = 'collapse';
collapseDiv.setAttribute('aria-labelledby', 'heading' + index);
collapseDiv.setAttribute('data-parent', '#domainAccordion');
let cardBody = document.createElement('div');
cardBody.className = 'card-body';
// Headers
let headersTitle = document.createElement('h5');
headersTitle.textContent = 'Headers';
cardBody.appendChild(headersTitle);
let headersPre = document.createElement('pre');
headersPre.textContent = JSON.stringify(data.headers, null, 2);
cardBody.appendChild(headersPre);
// Response Body
let bodyTitle = document.createElement('h5');
bodyTitle.textContent = 'Response';
cardBody.appendChild(bodyTitle);
// Create iframe to render the response body
let iframeContainer = document.createElement('div');
iframeContainer.className = 'iframe-container mb-3';
let iframe = document.createElement('iframe');
iframe.setAttribute('sandbox', '');
iframe.srcdoc = data.body; // set the content of the iframe
iframeContainer.appendChild(iframe);
cardBody.appendChild(iframeContainer);
collapseDiv.appendChild(cardBody);
card.appendChild(collapseDiv);
domainAccordion.appendChild(card);
}
</script>
</body>
</html>
Instructions for Use:
- Save this code as
index.html on your local machine.
- Prepare your data file (
file.txt) with the domain entries, ensuring each line is a valid JSON object as per your specified format:
{"domain": "example.com", "headers": {"key": "value"}, "body": "response text"}
{"domain": "anotherexample.com", "headers": {"key": "value"}, "body": "response text"}
- Open
index.html in your web browser.
- Click the "Choose File" button to select your
file.txt.
- The page will display an accordion list of domains from your file. Each domain can be expanded to view its headers and a rendered response in an iframe.
Features:
- Simple File Selection: Easily choose your domains file using the built-in file input.
- Dynamic Accordion View: Domains are displayed in a collapsible accordion for quick navigation, making it efficient to browse through a large number of domains.
- Formatted Headers: Headers are neatly formatted in a readable JSON structure.
- Response Rendering: The response body for each domain is rendered within an iframe, giving you a live preview of the content.
- Responsive Design: The layout is built with Bootstrap, ensuring it looks good on various screen sizes.
- Pleasant Aesthetics: A clean and modern interface enhances the user experience.
Note: This solution uses external resources from CDNs for Bootstrap and its dependencies to keep the index.html file simple and self-contained as per your request.