Skip to content

Instantly share code, notes, and snippets.

@Braunson
Last active February 11, 2025 19:55
Show Gist options
  • Save Braunson/42daa1a0081b5a8b9b357fa41ab1987c to your computer and use it in GitHub Desktop.
Save Braunson/42daa1a0081b5a8b9b357fa41ab1987c to your computer and use it in GitHub Desktop.
Quickly generate a form filler bookmarklet!
// Save this as a bookmark in your browser for the next time you want to fill in the page form!
javascript:(function() {
const fillValues = {
// Paste the object in here from the previous command
}
function fillField(element, value) {
if (!element) return;
// Handle different input types
switch (element.tagName.toLowerCase()) {
case 'select':
// Find option with matching value or text
const option = Array.from(element.options).find(opt =>
opt.value === value || opt.text === value
);
if (option) element.selectedIndex = option.index;
break;
case 'input':
switch (element.type.toLowerCase()) {
case 'checkbox':
case 'radio':
element.checked = value === true || value === 'true' || value === element.value;
break;
default:
element.value = value;
}
break;
default:
element.value = value;
}
// Trigger events
['input', 'change', 'blur'].forEach(eventType => {
element.dispatchEvent(new Event(eventType, {
bubbles: true
}));
});
}
Object.entries(fillValues).forEach(([field, value]) => {
fillField(document.querySelector(`#${field}, [name="${field}"]`), value);
});
})();
// Use this on the page you've filled in all fields for to grab the field names and values. Run this in your console or in the URL.
// This only includes input, select and textarea for now, expand as needed.
(function() {
let values = {};
document.querySelectorAll('input, select, textarea').forEach(field => {
if (field.id || field.name) {
values[field.id || field.name] = field.value;
}
});
console.log('Copy this for your bookmark:');
console.log(JSON.stringify(values, null, 2));
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment