// @name Form Filler // @description This script records the values of all form fields and stores them in localStorage. When the script is run again, it will ask the user if they want to restore values or save the new ones. const saveFormValues = () => { const formData = []; [...document.forms].forEach((form, formIndex) => { formData[formIndex] = []; [...form.elements].forEach((element, elementIndex) => { const type = element.type.toLowerCase(); let value = null; switch (type) { case 'text': case 'password': case 'textarea': case 'email': case 'date': case 'number': case 'select-one': value = element.value; break; case 'checkbox': case 'radio': value = element.checked; break; case 'select-multiple': value = Array.from(element.options).map(option => option.selected); break; default: return; } formData[formIndex][elementIndex] = { type, value }; }); }); localStorage.setItem(`_FormFiller_${location.pathname}`, JSON.stringify(formData)); }; const loadFormValues = (storedData) => { if (!storedData) return; [...document.forms].forEach((form, formIndex) => { if (!storedData[formIndex]) return; [...form.elements].forEach((element, elementIndex) => { const { type, value } = storedData[formIndex][elementIndex] || {}; switch (type) { case 'text': case 'password': case 'textarea': case 'email': case 'date': case 'number': case 'select-one': element.value = value; break; case 'checkbox': case 'radio': element.checked = value; break; case 'select-multiple': Array.from(element.options).forEach((option, idx) => { option.selected = value[idx]; }); break; } }); }); }; function askUser() { const overlay = document.createElement('div'); overlay.innerHTML = /*html*/ `
Would you like to restore or save the form values?
${JSON.stringify(storedData, null, 2)}