|
167 | 167 | <script>${js}<\/script> |
168 | 168 | <script> |
169 | 169 | (function() { |
| 170 | + function captureFormStates() { |
| 171 | + const clone = document.body.cloneNode(true); |
| 172 | + |
| 173 | + // Update checkbox and radio states |
| 174 | + const inputs = document.querySelectorAll('input[type="checkbox"], input[type="radio"]'); |
| 175 | + const cloneInputs = clone.querySelectorAll('input[type="checkbox"], input[type="radio"]'); |
| 176 | + |
| 177 | + for (let i = 0; i < inputs.length; i++) { |
| 178 | + if (inputs[i].checked) { |
| 179 | + cloneInputs[i].setAttribute('checked', 'checked'); |
| 180 | + } else { |
| 181 | + cloneInputs[i].removeAttribute('checked'); |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + // Update text input, textarea and select values |
| 186 | + const formElements = document.querySelectorAll('input:not([type="checkbox"]):not([type="radio"]), textarea, select'); |
| 187 | + const cloneFormElements = clone.querySelectorAll('input:not([type="checkbox"]):not([type="radio"]), textarea, select'); |
| 188 | + |
| 189 | + for (let i = 0; i < formElements.length; i++) { |
| 190 | + cloneFormElements[i].value = formElements[i].value; |
| 191 | + if (formElements[i].tagName === 'SELECT') { |
| 192 | + const options = formElements[i].options; |
| 193 | + const cloneOptions = cloneFormElements[i].options; |
| 194 | + |
| 195 | + for (let j = 0; j < options.length; j++) { |
| 196 | + if (options[j].selected) { |
| 197 | + cloneOptions[j].setAttribute('selected', 'selected'); |
| 198 | + } else { |
| 199 | + cloneOptions[j].removeAttribute('selected'); |
| 200 | + } |
| 201 | + } |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + return clone.innerHTML; |
| 206 | + } |
| 207 | + |
170 | 208 | function handleInputChange(e) { |
171 | 209 | if (e.target.tagName === 'INPUT' || e.target.tagName === 'TEXTAREA' || e.target.tagName === 'SELECT') { |
172 | 210 | window.parent.postMessage({ |
173 | 211 | type: 'inputChanged', |
174 | | - html: document.body.innerHTML |
| 212 | + html: captureFormStates() |
175 | 213 | }, '*'); |
176 | 214 | } |
177 | 215 | } |
|
0 commit comments