OPML to JSON Converter
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Roboto', sans-serif;
background-color: #f3f4f6;
color: #333;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.container {
background-color: #fff;
padding: 2rem;
border-radius: 10px;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
width: 100%;
max-width: 600px;
text-align: center;
}
header h1 {
color: #0073e6;
margin-bottom: 10px;
}
header p {
font-size: 1rem;
color: #555;
margin-bottom: 30px;
}
.input-area, .output-area, .download-area {
margin-bottom: 20px;
}
.upload-label {
font-size: 1.1rem;
color: #333;
display: block;
margin-bottom: 10px;
text-align: left;
}
input[type="file"] {
width: 100%;
padding: 10px;
border: 2px solid #0073e6;
border-radius: 5px;
background-color: #f1f8ff;
cursor: pointer;
}
.convert-btn {
padding: 10px 20px;
font-size: 1rem;
color: #fff;
background-color: #0073e6;
border: none;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s;
}
.convert-btn:hover {
background-color: #005bb5;
}
textarea {
width: 100%;
height: 200px;
padding: 15px;
border: 2px solid #0073e6;
border-radius: 5px;
font-family: 'Roboto', sans-serif;
background-color: #f1f8ff;
color: #333;
font-size: 1rem;
resize: none;
box-sizing: border-box;
}
.download-btn {
padding: 10px 20px;
font-size: 1rem;
color: #fff;
background-color: #28a745;
border: none;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s;
}
.download-btn:hover {
background-color: #218838;
}
let opmlData = null;
function handleFile() {
const fileInput = document.getElementById('opml-file');
const file = fileInput.files[0];
if (!file) {
alert('Please select an OPML file.');
return;
}
const reader = new FileReader();
reader.onload = function(event) {
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(event.target.result, "application/xml");
const opml = xmlDoc.getElementsByTagName('opml')[0];
if (!opml) {
alert('Invalid OPML file.');
return;
}
opmlData = parseOpmlToJson(opml);
alert('OPML file loaded successfully!');
};
reader.onerror = function() {
alert('Error reading file!');
};
reader.readAsText(file);
}
function parseOpmlToJson(opml) {
const json = [];
const outlines = opml.getElementsByTagName('body')[0].getElementsByTagName('outline');
function parseOutline(outline) {
const outlineJson = {
text: outline.getAttribute('text'),
type: outline.getAttribute('type') || 'url',
xmlUrl: outline.getAttribute('xmlUrl') || '',
htmlUrl: outline.getAttribute('htmlUrl') || '',
children: []
};
const subOutlines = outline.getElementsByTagName('outline');
for (let subOutline of subOutlines) {
outlineJson.children.push(parseOutline(subOutline));
}
return outlineJson;
}
for (let outline of outlines) {
json.push(parseOutline(outline));
}
return json;
}
function convertToJson() {
if (!opmlData) {
alert('Please load an OPML file first!');
return;
}
const jsonOutput = document.getElementById('json-output');
jsonOutput.value = JSON.stringify(opmlData, null, 2);
document.getElementById('download-btn').style.display = 'inline-block';
}
function downloadJson() {
const json = JSON.stringify(opmlData, null, 2);
const blob = new Blob([json], { type: 'application/json' });
saveAs(blob, 'opml_to_json.json');
}
<
No comments:
Post a Comment