sed-rules.js (4660B)
1 // sed-rules.js - Manage sed rules in admin interface 2 3 let sedRules = []; 4 5 // Initialize on page load 6 document.addEventListener('DOMContentLoaded', function() { 7 sedRules = window.initialSedRules || []; 8 renderSedRules(); 9 setupSedRulesForm(); 10 }); 11 12 function renderSedRules() { 13 const container = document.getElementById('sed-rules'); 14 if (!container) return; 15 16 container.innerHTML = ''; 17 18 sedRules.forEach((rule, index) => { 19 const ruleDiv = document.createElement('div'); 20 ruleDiv.style.cssText = 'border: 1px solid #ddd; padding: 15px; margin-bottom: 15px; border-radius: 5px;'; 21 22 ruleDiv.innerHTML = ` 23 <div style="display: flex; justify-content: space-between; align-items: start; margin-bottom: 10px;"> 24 <h4 style="margin: 0;">Rule ${index + 1}</h4> 25 <button onclick="removeSedRule(${index})" style="background-color: #dc3545; color: white; padding: 5px 10px; border: none; border-radius: 3px; font-size: 12px; cursor: pointer;"> 26 Remove 27 </button> 28 </div> 29 30 <div style="margin-bottom: 10px;"> 31 <label style="display: block; font-weight: bold; margin-bottom: 5px; font-size: 13px;">Name:</label> 32 <input type="text" value="${escapeHtml(rule.name)}" 33 onchange="updateSedRule(${index}, 'name', this.value)" 34 placeholder="e.g., Remove URL Parameters" 35 style="width: 100%; padding: 6px; font-size: 13px; border: 1px solid #ccc; border-radius: 3px;"> 36 </div> 37 38 <div style="margin-bottom: 10px;"> 39 <label style="display: block; font-weight: bold; margin-bottom: 5px; font-size: 13px;">Description:</label> 40 <input type="text" value="${escapeHtml(rule.description)}" 41 onchange="updateSedRule(${index}, 'description', this.value)" 42 placeholder="e.g., Removes brandIds and productId from URLs" 43 style="width: 100%; padding: 6px; font-size: 13px; border: 1px solid #ccc; border-radius: 3px;"> 44 </div> 45 46 <div style="margin-bottom: 0;"> 47 <label style="display: block; font-weight: bold; margin-bottom: 5px; font-size: 13px;">Sed Command:</label> 48 <input type="text" value="${escapeHtml(rule.command)}" 49 onchange="updateSedRule(${index}, 'command', this.value)" 50 placeholder="e.g., s?[?&]brandIds=[0-9]\\+&productId=[0-9]\\+??g" 51 style="width: 100%; padding: 6px; font-size: 13px; font-family: monospace; border: 1px solid #ccc; border-radius: 3px;"> 52 <small>Sed command syntax (e.g., s/old/new/g)</small> 53 </div> 54 `; 55 56 container.appendChild(ruleDiv); 57 }); 58 } 59 60 function addSedRule() { 61 sedRules.push({ 62 name: '', 63 description: '', 64 command: '' 65 }); 66 renderSedRules(); 67 } 68 69 function removeSedRule(index) { 70 if (confirm('Remove this sed rule?')) { 71 sedRules.splice(index, 1); 72 renderSedRules(); 73 } 74 } 75 76 function updateSedRule(index, field, value) { 77 if (sedRules[index]) { 78 sedRules[index][field] = value; 79 } 80 } 81 82 function setupSedRulesForm() { 83 const form = document.getElementById('sedrules-form'); 84 if (!form) return; 85 86 form.addEventListener('submit', function(e) { 87 // Validate rules 88 for (let i = 0; i < sedRules.length; i++) { 89 const rule = sedRules[i]; 90 if (!rule.name || !rule.command) { 91 e.preventDefault(); 92 alert(`Rule ${i + 1} is incomplete. Please fill in Name and Sed Command.`); 93 return; 94 } 95 } 96 97 // Remove any previously-injected hidden fields from a prior submit 98 this.querySelectorAll('input[data-generated]').forEach(el => el.remove()); 99 100 // Append one hidden field per value — no JSON 101 sedRules.forEach((rule, i) => { 102 appendHidden(this, `sed_rules[${i}][name]`, rule.name); 103 appendHidden(this, `sed_rules[${i}][description]`, rule.description); 104 appendHidden(this, `sed_rules[${i}][command]`, rule.command); 105 }); 106 }); 107 } 108 109 function appendHidden(form, name, value) { 110 const input = document.createElement('input'); 111 input.type = 'hidden'; 112 input.name = name; 113 input.value = value; 114 input.dataset.generated = '1'; 115 form.appendChild(input); 116 } 117 118 function escapeHtml(text) { 119 const div = document.createElement('div'); 120 div.textContent = text; 121 return div.innerHTML; 122 }