email-dns-tools/assets/scripts/creator.js
2026-01-14 15:46:37 +01:00

52 lines
1.3 KiB
JavaScript

import { DmarcTool } from "./tools/DmarcTool.js";
const tools = {
"/dmarc-creator": DmarcTool,
};
const Tool = tools[location.pathname];
addFields(document.getElementById("form"), Tool.fields.filter(field => !field.categoryName));
const categories = Tool.fields.map(field => field.categoryName).filter(isUnique).filter(val => val);
for (const category of categories) {
const details = document.createElement("details");
details.innerHTML = `<summary>${Tool.categories[category]}</summary>`;
document.getElementById("form").appendChild(details);
addFields(details, Tool.fields.filter(field => field.categoryName === category));
}
function addFields(elem, fields) {
for (const field of fields) {
if (!field.getInputHtml()) continue;
elem.innerHTML += `
<label for="${field.key}">${field.displayName}</label>
<p class="description">${field.description ?? ""}</p>
${field.getInputHtml()}
`;
}
}
document.getElementById("form").onchange = () => generate();
generate();
function generate() {
const tool = new Tool();
document.getElementById("record").value = tool.fieldsToString();
}
document.getElementById("record").onclick = (e) => {
e.target.select();
document.execCommand("copy");
}
function isUnique(value, index, array) {
return array.indexOf(value) === index;
}