OPML to JSON Converter
/* styles.css */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: Arial, sans-serif;
background-color: #f0f0f0;
color: #333;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
padding: 20px;
}
.container {
width: 100%;
max-width: 800px;
background-color: #fff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}
header {
text-align: center;
margin-bottom: 20px;
}
header h1 {
font-size: 2rem;
color: #4caf50;
}
header p {
font-size: 1rem;
color: #555;
}
.upload-section {
text-align: center;
margin-bottom: 30px;
}
input[type="file"] {
padding: 10px;
border: 2px solid #4caf50;
border-radius: 4px;
background-color: #f9f9f9;
cursor: pointer;
font-size: 1rem;
}
button {
padding: 10px 20px;
background-color: #4caf50;
color: white;
border: none;
border-radius: 4px;
font-size: 1rem;
cursor: pointer;
margin-left: 10px;
transition: background-color 0.3s ease;
}
button:disabled {
background-color: #ddd;
cursor: not-allowed;
}
button:hover {
background-color: #45a049;
}
.result-section {
margin-top: 30px;
}
pre {
background-color: #f4f4f4;
padding: 15px;
border-radius: 5px;
font-size: 0.9rem;
overflow-x: auto;
white-space: pre-wrap;
word-wrap: break-word;
color: #333;
}
@media (max-width: 600px) {
.container {
padding: 10px;
}
header h1 {
font-size: 1.5rem;
}
input[type="file"], button {
width: 100%;
margin-top: 10px;
}
}
// script.js
document.getElementById('fileInput').addEventListener('change', handleFileSelect);
document.getElementById('convertButton').addEventListener('click', convertToJSON);
let opmlData = null;
function handleFileSelect(event) {
const file = event.target.files[0];
if (file && file.name.endsWith('.opml')) {
document.getElementById('convertButton').disabled = false;
const reader = new FileReader();
reader.onload = function(e) {
opmlData = e.target.result;
};
reader.readAsText(file);
} else {
alert('Please select a valid OPML file.');
document.getElementById('convertButton').disabled = true;
}
}
function convertToJSON() {
if (!opmlData) return;
try {
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(opmlData, 'application/xml');
const opmlObject = parseOPML(xmlDoc.documentElement);
const jsonOutput = JSON.stringify(opmlObject, null, 2);
document.getElementById('jsonOutput').textContent = jsonOutput;
} catch (error) {
alert('Error parsing the OPML file.');
console.error(error);
}
}
function parseOPML(node) {
const result = {};
if (node.nodeName === 'outline') {
const attributes = node.attributes;
const outlineObj = {
text: attributes.getNamedItem('text') ? attributes.getNamedItem('text').value : '',
type: attributes.getNamedItem('type') ? attributes.getNamedItem('type').value : '',
xmlUrl: attributes.getNamedItem('xmlUrl') ? attributes.getNamedItem('xmlUrl').value : '',
htmlUrl: attributes.getNamedItem('htmlUrl') ? attributes.getNamedItem('htmlUrl').value : '',
children: []
};
Array.from(node.children).forEach(childNode => {
outlineObj.children.push(parseOPML(childNode));
});
result.outline = outlineObj;
}
return result;
}
No comments:
Post a Comment