Listing the Forms and Fields on a page in Internet Explorer or Maxthon
This post is an introduction to what I’ve been researching in my spare time over the last few weeks. It will be the first in a series of posts about form submission. It will lead you basically along the path that I took in discovering this information, and will hopefully allow you to skip over some of the trouble spots along the way. I’ll note each of the trouble spots in the comments in the doc (for instance in this one I include a note about the value of a button also being submitted with the form).
This HTML file builds on top of the work that I did on with the Copy as Text Link tool referenced elsewhere on this site. However, this routine processes the currently active webpage/frame and generates a report of the Forms and fields on that page. For each form it shows its name, action and then list the inputs on that form. For each input, it shows its name and value. From this information, you can see what is being tracked between pages on a website. You could also use this information to debug an entry form you are creating. It could also be used to generate the form submission information that will be generated by a certain action so that it can be repeated programmatically in the future.
This routine will only work in IE right now and older releases of Maxthon. It could easily be transposed to other browsers and tools. The process should be relatively easy to follow thanks to Microsoft’s Document Object Module (DOM) technology. DOM allows HTML code to be accessed as if it were Object Oriented. In other words, this routine doesn’t work with the HTML source code. Instead it asks IE for specific parts of the webpage one at a time. This allows each of the various parts/divisions of any web page to have its own particular properties and methods. This routine asks asks the document object for the list of forms it contains. It then asks each form for a list of fields. It displays each in its turn.
To install this file in IE, right click on the following link and choose Save Target As. Then in the destination window type %WINDIR%\web\formlist.htm and press enter.
%windir% is an environment variable that represents your Windows directory. So you can put C:\Windows\Web\formlist.htm if you know you have a c:\Windows directory.
Then choose Start->Run and put in this text and then press enter:
reg ADD "HKCU\\Software\\Microsoft\\Internet Explorer\\MenuExt\\F&orms and Fields" /ve /t REG_SZ /d "%WINDIR%\\web\\formlist.htm" /f
That will update your registry to point to the file. If you have problems, use this batch file instead. It contains the same command but will not be corrupted by windows.
Then close all instances of IE.
Now browse to a web page that has a form, most of them do now-a-days.
Right click anywhere on the text of the form and choose Form and Fields from the menu.
You’ll now be able to see all sorts of information about the page you were on and the fields and hidden fields it contains.
BTW, this routine uses a pop up window. Popup blockers like the Google Toolbar will try to block this form. So, you just might have to hold down the CTRL key as you choose Form and Fields to allow this to work.
Also, FYI it works in Maxthon through at least version 1.5.2 build 21. I think build 23 reworks the context menu stuff, but don’t quote me on that.
Enjoy!
Sample output from FoxNews.com
(Source code follows)
NOTE: There is now a reference to the final location of this file in the source code. If you install this as something other than c:\windows\web\formlist.htm you must now update the source code.
-
<script type=“text/javascript” defer>
-
/*
-
Forms & Fields List
-
Copyright (C) 2006 TheCodeCave.com
-
Brian Layman
-
Processes the currently active webpage/frame and generates a report of the Forms and fields on that page.
-
For each form it shows its name, action and then list the inputs on that form. For each input, it shows
-
its name and value. From this information, you can see what is being tracked between pages on a website.
-
You could also use this information to debug an entry form you are creating. It could also be used to
-
generate the form submission information that will be generated by a certain action so that it can be
-
repeated programatically in the future.
-
The process should be relatively easy to follow thanks to Microsoft’s Document Object Module (DOM) technology.
-
DOM allows HTML code to be accessed as if it were Object Oriented. In other words, this routine doesn’t work
-
with the HTML source code. Instead it asks IE for specific parts of the webpage one at a time. This allows
-
eacg of the various parts/divisions of any web page to have its own particular properties and methods. This
-
routine asks asks the document object for the list of forms it contains. It then asks each form for a list
-
of fields. It displays each in its turn.
-
Please note that when a form is submitted the input is not limited to fields. The button/img used to submit
-
the form also contains a value. In this way, the destination action can determine what button was pressed.
-
So, you will likely see more fields listed in this routine than are obvious when glancing at the screen. Also
-
just because a value is listed on this form, does not mean that it will reach the final form. Javascript can
-
be used to validate/alter information before the form is submitted.
-
To use this file, save it as:
-
%WINDIR%\web\formlist.htm
-
You can put C:\windows in there for %WINDIR% id your computer doesn’t like it or if you know you have a c:\windows
-
directory. Then choose START->Run and put in this text and then press enter:
-
reg ADD "HKCU\Software\Microsoft\Internet Explorer\MenuExt\F&orms and Fields" /ve /t REG_SZ /d "%WINDIR%\web\formlist.htm" /f
-
Version Notes
-
————————————————————————————————————————
-
1.0 BL 2006-07-18 Initial version
-
1.1 BL 2006-07-19 Fixed "access is denied" error that blocked the pop up on some systems
-
*/
-
-
// External menuArguments returns the window object where the context menu item was executed.
-
var parentwin=external.menuArguments;
-
-
// If no form was used to call this routine, calling documents title will be blank.
-
// If that is the case, display a message.
-
// NOTE: Referring to parentwin or parentwin.document only here will cause IE in the else clause.
-
// The title seems to be one of the only safe properties to access.
-
if (!parentwin.document.title)
-
{
-
document.writeln (“<html>”);
-
document.writeln (“<head>”);
-
document.writeln (“<title>TCC Form List</title>”);
-
document.writeln (“</head>”);
-
document.writeln (“<body>”);
-
document.writeln (“This file is meant to be accessed from a popup menu. It is not a standard HTML file.<br/>”);
-
document.writeln (“<br/>”);
-
document.writeln (“If you were trying to save this file to your local drive, go back to the original link, right click and choose Save Target As.<br/>”);
-
document.writeln (“<br/>”);
-
document.writeln (“<br/>”);
-
document.writeln (“<center>For more information, please visit <a href=’http://www.thecodecave.com/article208′ Title=’Cold Storage for my best ideas…’>The Code Cave</a> - Brian<br/><br/><br/><a href=’javascript:window.close()’ style=\”color:#FF0000;text-decoration:none\”>close</a></center><br/></body>”);
-
document.writeln (“</font>”);
-
document.writeln (“</body>”);
-
document.writeln (“</html>”);
-
-
}
-
else {
-
// At this point we know that this file was reached from a popup menu on a webpage.
-
-
// Now we need to create a new window. The first task is to describe the way the window should appear.
-
var new_window_features = new String (“toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes, resizable=yes, top=0, left=0, width=400, height=”);
-
new_window_features = new_window_features + (screen.height - 100);
-
// Use those properties to create a new window for our output.
-
// NOTE: The second parameter here is the name of the new target window. Having a standard name will force all form listings to appear in the same
-
// window/tab. If you wish to get a new window/tab for every listing, change this parameter to say "_blank" instead.
-
// IMPORTANT: The first parameter, the URL would normally be blank. After all, what is desired is a blank slate on
-
// which to build the new report. However, some IE installations block blank window.open calls. This is due to th
-
// local host being in a different security zone from the web page you are surfing. So, we trick the window into
-
// thinking that it is already in the local domain, and therefore the window.open succeeds.
-
var new_window = window.open(“file:///C:/windows/web/formlist.htm”, “tcc_formlister”, new_window_features);
-
var new_document = new_window.document;
-
-
// Now create a document in that window.
-
new_document.open();
-
-
// Define the header of the form and a script to close the window when we are done.
-
new_document.writeln (“<html>”);
-
new_document.writeln (“<head>”);
-
new_document.writeln (“<title>TCC Field List</title>”);
-
new_document.writeln (“</head>”);
-
new_document.writeln (“<body topmargin=0 leftmargin=0 rightmargin=0 bottommargin=0>”);
-
new_document.writeln (“<font style=\”font: 8pt Verdana, Arial, Helvetica, Sans-serif; line-height:18pt;\”>”);
-
new_document.writeln (“<script language=javascript>function navigateClose(str){if (document.my_parent != null){document.my_parent.location.href=str;window.close();}else{alert(\”Please wait until the list has populated.\”);}}<\/script>”);
-
new_document.writeln (“List forms and fields in <b>” + parentwin.document.title + “</b>:<ol>”)
-
-
// Get the list of forms from the original document
-
var forms = parentwin.document.forms;
-
new_document.writeln (“<b>This document contains: “ + forms.length + ” forms.</b>”);
-
new_document.writeln (“<br/>”);
-
new_document.writeln (“<hr/>”);
-
new_document.writeln (“<br/>”);
-
new_document.writeln (“<b>Form and Field listings</b><br/>”);
-
new_document.writeln (“<br/>”);
-
new_document.writeln (“This section will list all of the forms and their fields.<br/>”);
-
new_document.writeln (“<br/>”);
-
-
// Iterate all of the forms and display their information and then the fields.
-
for (i = 0; i <forms.length; i++)
-
{
-
curform = forms[i];
-
new_document.writeln (“<br/><h2>Form “ + (i + 1) + “: “ + curform.name + “</h2>”);
-
new_document.writeln (“<b>Submits to: </b>” + curform.action + “<br/><br/>”);
-
new_document.writeln (“<b>Fields:</b> (FieldName: Value)<br/>”);
-
-
// There are several methods to determine the fields that are used on a form.
-
// In this example we will request an element array. We will request a list of
-
// all input tags.
-
var elements = curform.getElementsByTagName(“input”);
-
-
// The list will now be iterated and the name and current value will be displayed for each.
-
// Other properties such as default value could be displayed if this were desired.
-
for (var j = 0; j <elements.length; j++)
-
{
-
var curelement = elements[j];
-
new_document.writeln (curelement.name +“: “ + curelement.value + “<br/>”);
-
}
-
-
// This is a freebie that will be used as a reference in other articles on TheCodeCave.com
-
// This routine quickly builds Delphi source to assign the post data to a variable.
-
// Since post data can be any length, this routine breaks the lines up at around 255
-
// characters for easier handling.
-
// I suppose it should truncate off the trailin " + & + " and put in a semicolon, but I’ll
-
// leave that up to you to do if you think you’ll actually use this.
-
new_document.writeln (“<br/>”)
-
new_document.writeln (“<br/>”)
-
new_document.writeln (“<b>DELPHI ENCODEDED DATA STRING</b> - Paste this text into a Delphi Form Post to automate the post.<br/>”)
-
new_document.writeln (“<br/>”)
-
new_document.writeln (“EncodedDataString := “);
-
var linelen = 0;
-
for (var j = 0; j <elements.length; j++)
-
{
-
var curelement = elements[j];
-
var scrap = “HTTPEncode(’” + curelement.name + “‘) + ‘=’ + HTTPEncode(’” + curelement.value + “‘) + ‘&’ + “
-
-
if ((linelen + scrap.length)> 255) {
-
new_document.writeln (“<br/>”)
-
linelen = 0;
-
}
-
else {
-
linelen = linelen + scrap.length;
-
}
-
new_document.writeln (scrap);
-
}
-
new_document.writeln (“<hr/>”)
-
-
}
-
// Close off the document with a footer.
-
new_document.writeln (“</ul><center>For more information, please visit <a href=’http://www.thecodecave.com’ Title=’Cold Storage for my best ideas…’>The Code Cave</a> - Brian<br/><br/><br/><a href=’javascript:window.close()’ style=\”color:#FF0000;text-decoration:none\”>close</a></center><br/></body>”);
-
new_document.writeln (“</font></html>”);
-
new_document.close();
-
-
// Assign the parent form to the new document so that when the parent page is closed, this page is closed too.
-
new_document.my_parent = parentwin;
-
}
-
</script>
















[...] I’ve made two changes to the Forms and Field lister I posted yesterday - the original post has been altered to include them. [...]
Pingback by The Code Cave — July 20, 2006 @ 10:45 am