Skip to content

Instantly share code, notes, and snippets.

@Patabugen
Created November 8, 2012 15:53
Show Gist options
  • Save Patabugen/4039642 to your computer and use it in GitHub Desktop.
Save Patabugen/4039642 to your computer and use it in GitHub Desktop.
Form Exporter
/**
* This is a bit of code to make it easier to turn forms into PHP Arrays
* for use in scrapers etc.
* It's designed to be run from the console, e.g in Firebug. Just copy
* everything from this file into the console and hit Go. It shouldn't
* take much to turn it into a browser extension by someone with the know how.
**/
/**
* The arrays didn't seem to want to .concat, so I just call this
* function for each one.
**/
function listFields(fields)
{
var output = '';
for (var i = 0; i < fields.length; i++) {
// We output single quotes so names or values with a $ don't appear as variables.
output += "<li>'" + fields[i].name + "'\t\t\t=> '" + fields[i].value + "',</li>";
}
return output;
}
var output = "";
// Get all the forms
var forms = document.getElementsByTagName('form');
// We will have one bit of output for each one, so loop through each form
for (var i = 0; i < forms.length; i++) {
var form = forms[i];
// Get the different kinds of fields. Here is where it'd be cool to keep the correct ordering of fields.
// Also nice would be to filter out duplicate form names (i.e radio buttons) and only keep the current value)
var inputs = form.getElementsByTagName('input');
var selects = form.getElementsByTagName('select');
var textarea = form.getElementsByTagName('textarea');
var buttons = form.getElementsByTagName('button');
// Make some output
output += "<p>$" + form.id + " = array(";
output += "<ul style='list-style-type: none'>"
output += listFields(inputs);
output += "</ul>";
output += ");";
output += "</p>";
}
// Add a seperator so we can run it more than once without getting too confused
output += "<hr />";
// Cheeply throw it at the bottom of the page.
document.body.innerHTML += output;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment