[Project_owners] getElementsByTagName() problem

HJ bugs4hj at yahoo.com
Tue Apr 11 18:20:49 EDT 2006


Eric H. Jung wrote:
> Hi,
> 
> The following code, which build an array of all <input/> elements on a page,
> works at every website I've tried except
> https://banking.ing-diba.de/OnlineBanking/index.html.
> 
> Would someone be able to have a look? I cannot figure out why.
> 
> Context is browser.xul scope with
> https://banking.ing-diba.de/OnlineBanking/index.html loaded:
> 
> var recentWindow = Components.classes["@mozilla.org/appshell/window-mediator;1"]
>   .getService(Components.interfaces.nsIWindowMediator)
>   .getMostRecentWindow("navigator:browser");

Ok, this looks like some dialog window so why don't you pass on the 
window used to open the dialog, because that saves you the above lines.

> if (recentWindow) {
>   doc = recentWindow.content.document;
>   // Build an array of all <input/> elements
>   // in the doc (including frames)
>   var inputFields = new Array();
> 
>   // Check doc
>   var fields = doc.getElementsByTagName("input");
>   for (var i=0; i<fields.length; i++)
>     inputFields.push(fields[i]);
>       
>   // Check frames
>   var frames = doc.getElementsByTagName("frame");
>   for (var i=0; i<frames.length; i++) {
>     fields = frames[i].contentDocument.getElementsByTagName("input");
>     for (var j=0; j<fields.length; j++)
>       inputFields.push(fields[j]);
>   }
>   
>   // Check iframes
>   var frames = doc.getElementsByTagName("iframe");
>   for (var i=0; i<frames.length; i++) {
>     fields = frames[i].contentDocument.getElementsByTagName("input");
>     for (var j=0; j<fields.length; j++)
>       inputFields.push(fields[j]);
>   }

Why this difficult?  I mean, all frames end up in content.frames so why 
don't you use that instead?

I hate code duplication so I would use something like this:

getInputFields(aWindow)
{
   var inputFields = new Array();
   var frames = (aWindow.frames.length) ? aWindow.frames : aWindow.document;

   function _getInputFields(aFrame)
   {
     var inputElements = aFrames.contentDocument.getElementsByName('input');

     for (var i = 0; i < inputElements.length; i++) {
       inputFields.push(inputElements[i]);
     }
   }
   for (var fIndex = 0; fIndex < frames.length; fIndex++) {
     if (!frames[fIndex].length) {
       _getInputFields(frames[fIndex]);
     } else {
       frames.push(frames[iFrame]);
     }
   }
   return inputFields;
}

Note: the above lines are nothing more than some quick untested JS code 
snipped, but it should give some "how to" clues (at least I hope).

/HJ


More information about the Project_owners mailing list