var ruleRowTemplate = '' + '

[INDEX]

' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' '; var configJson = {}; $("document").ready(function() { loadConfig(); $("#add-rule-btn").click(function() { appendRow(generateRowTemplate("", "", true, true)); }); $(document).on("click", ".rule-trash-btn", function() { $(this).closest("tr").remove(); updateRowIndexes(); buildAndSaveConfig(); }); bindChangeEvents(); }); function appendRow(rowTemplate) { $(".table tbody").append(rowTemplate); bindChangeEvents(); updateRowIndexes(); } function generateRowTemplate(str, outputStr, enabled, ignoreCase) { var row = ruleRowTemplate; row = row.replaceAll("[INDEX]", $(".table tbody tr").length + 1); row = row.replace("[STR-TO-REPLACE]", str); row = row.replace("[REPLACE-STR]", outputStr); if (enabled) { row = row.replace("[ENABLED]", "checked"); } else { row = row.replace("[ENABLED]", ""); } if(ignoreCase){ row = row.replace("[IGNORECASE]", "checked"); } else { row = row.replace("[IGNORECASE]", ""); } return row; } function updateRowIndexes() { $(".table tbody tr").each(function(index) { $(this).attr("id", "rule-" + (index + 1)); $(this).find("td:first p").text(index + 1); }); } function buildAndSaveConfig() { // Show saving status $("#saving-status").show(); $("#saved-status").hide(); var rules = []; $(".table tbody tr").each(function() { var rule = {}; rule["strToReplace"] = $(this).find(".rule-input-str-input").val(); rule["replaceStr"] = $(this).find(".rule-output-str-input").val(); rule["enabled"] = $(this).find(".rule-enabled-by-default-toggle").is(":checked"); rule["ignoreCase"] = $(this).find(".ignore-case-toggle").is(":checked"); rules.push(rule); }); configJson["rules"] = rules; console.log(configJson); window.localStorage.setItem("config", JSON.stringify(configJson)); // Simulate a delay for saving process setTimeout(function() { $("#saving-status").hide(); $("#saved-status").show(); }, 200); } function loadConfig() { var config = window.localStorage.getItem("config"); if (config) { configJson = JSON.parse(config); configJson["rules"].forEach(function(rule) { appendRow(generateRowTemplate(rule["strToReplace"], rule["replaceStr"], rule["enabled"], rule["ignoreCase"])); }); } $("#saving-status").hide(); $("#saved-status").show(); bindChangeEvents(); } function bindChangeEvents() { $(".rule-input-str-input, .rule-output-str-input").off("input").on("input", function() { buildAndSaveConfig(); }); $(".rule-enabled-by-default-toggle").off("change").on("change", function() { buildAndSaveConfig(); }); $(".ignore-case-toggle").off("change").on("change", function() { buildAndSaveConfig(); }); }