// document.addEventListener('DOMContentLoaded', function () { // if (document.getElementById("sortable") && typeof simpleDatatables.DataTable !== 'undefined') { // console.log("simpleDatatables") // const table = document.querySelector('#sortable'); // const dataTable = new simpleDatatables.DataTable(table, { // paging: false, // searching: false, // info: false, // perPageSelect: false, // sortable: true // }); // } // }); document.addEventListener('DOMContentLoaded', function () { // set darkmode document.documentElement.classList.add('dark'); // Select all tables with the class "sortable" const tables = document.querySelectorAll('table.sortable'); if (tables.length > 0 && typeof simpleDatatables.DataTable !== 'undefined') { console.log("Initializing simpleDatatables for sortable tables"); // Loop through each table and initialize DataTable tables.forEach(table => { new simpleDatatables.DataTable(table, { paging: false, searchable: false, info: false, perPageSelect: false, sortable: true }); }); } }); document.body.addEventListener("redirectToDelayed", function(evt){ let event = JSON.parse(evt.detail.value); setTimeout(function(){ location.href = event.redirect; }, event.delay); }) document.body.addEventListener("reInitFB", function(evt){ initFlowbite(); const tables = document.querySelectorAll('table.sortable'); if (tables.length > 0 && typeof simpleDatatables.DataTable !== 'undefined') { console.log("Initializing simpleDatatables for sortable tables"); // Loop through each table and initialize DataTable tables.forEach(table => { new simpleDatatables.DataTable(table, { paging: false, searchable: false, info: false, perPageSelect: false, sortable: true }); }); } }) document.body.addEventListener("highlightJS", function(evt){hljs.highlightAll();}) // Fancybox config Fancybox.bind("[data-fancybox]", {}); Fancybox.bind('[data-fancybox="gallery"]', { on: { "wheel": (fancybox, eventName) => { Fancybox.close(); }, }, }); // Highlighter config hljs.configure({languages: ["js","html","css"]}) hljs.highlightAll(); // Dropdown menu function toggleDropdown(target, trigger) { initDropdowns(); const $targetEl = document.getElementById(target); const $triggerEl = document.getElementById(trigger); const options = { placement: 'bottom-end', onHide: () => { // Cancel any request sendt from the dropdown button so we don't reopen it htmx.trigger('#'+trigger, 'htmx:abort') }, }; const instanceOptions = { id: target, override: true, }; const dropdown = new Dropdown($targetEl, $triggerEl, options, instanceOptions); dropdown.show(); } // YARA function createRule(id) { let formData = document.querySelectorAll(id); data = { name : formData[0].querySelector('#name').value, comment : formData[0].querySelector('#description').value, rule : formData[0].querySelector('#yara_rule').value } console.log(`${data}`) let dataReceived = ""; fetch("/user/detection/yara/rule", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(data) }).then(resp => { if (resp.status === 200) { console.log("Status: " + resp.status) return resp.json() } else { console.log("Status: " + resp.status) return Promise.reject("server") } }) .then(dataJson => { dataReceived = JSON.parse(dataJson) }) .catch(err => { if (err === "server") return }) console.log(`Received: ${dataReceived}`) } function updateRule(id) { let formData = document.querySelectorAll(id); data = { id : formData[0].querySelector('#rule_id').value, enabled : formData[0].querySelector('#enabled').checked, name : formData[0].querySelector('#name').value, comment : formData[0].querySelector('#description').value, rule : formData[0].querySelector('#yara_rule').value } fetch("/user/detection/yara/rule", { credentials: "same-origin", mode: "same-origin", method: "PATCH", headers: { "Content-Type": "application/json" }, body: JSON.stringify(data) }) .then(resp => { if (resp.status === 200) { location.reload(true); } else if (resp.status === 400) { console.log(resp); JSON.stringify(resp.data); } else { console.log("Status: " + resp.status) return Promise.reject("server") } }) .catch(err => { if (err === "server") return console.log(err) }) } function deleteRule(id) { let formData = document.querySelectorAll(id); rule_id = formData[0].querySelector('#rule_id').value; let dataReceived = ""; fetch("/user/detection/yara/rule/"+rule_id, { credentials: "same-origin", mode: "same-origin", method: "DELETE", headers: { "Content-Type": "application/json" } }) .then(resp => { if (resp.status === 200) { location.reload(true); } else { console.log("Status: " + resp.status) return Promise.reject("server") } }) .catch(err => { if (err === "server") return console.log(err) }) console.log("Received: ${dataReceived}") } // TODO: Use standard toast document.body.addEventListener("createYaraRuleError", function(evt){ let toast = JSON.parse(evt.detail.value); if (toast.level=="error") { $('#modal-toast-error-msg').html(toast.message); $('#modal-toast-error').removeClass('hidden'); setTimeout(function(){ $('#modal-toast-error').addClass('hidden'); },7000); } })