0

I've run into a bit of a snag. When I return object references like getElementsByTagName, getElementsByClassName, or querySelectorAll directly and return them to AutoIt, I have no issue enuming through the indexes and using the returned objects, but if I try to store the object references from the DOM into an array via array push, I get back the right object.length but the array is null. I thought this might be a local variable issue, so I stored the data globally and still ran into the same issue.

From the example below, is there a way to return the object references as if I were calling getElementsByTagName, getElementsByClassName, or querySelectorAll directly, but only the items I want? I'm afraid my javascript knowledge is left wanting.

//http://stackoverflow.com/a/25054465/4312966
//Thanks kapa
function IEExAutClassGetCollectionFunc() {
    var autDIV = document.createElement('div');
    autDIV.setAttribute('id', 'IEExAutClassGetCollectionId');
    document.body.appendChild(autDIV);
    var autVariable = document.getElementById("IEExAutClassGetCollectionId");
    autVariable.IEExAutClassGetCollection = function (oElm, sClass) {

        var sPattern, oElements, i, aresults = [];//, oRet;
        if (typeof sClass == 'undefined') {return 1;}
        oElm = (typeof oElm == 'undefined') ? document : oElm;

        /*  
        if (oElm.getElementsByClassName) { // IE8+
            oRet = oElm.getElementsByClassName(sClass);
            if (!oRet.length) {return 2;}
            return oRet
        }

        if (oElm.querySelectorAll) { // IE8+
            oRet = oElm.querySelectorAll("." + sClass);
            if (!oRet.length) {return 3;}
            return oRet
        }
        */

        // Pushing the array objects is not working
        // I get the object array back, but the objects are missing
        // object.length matches on both sides, here object.tagName matches
        // on the AutoIt side nothing in the .items(n)
        if (oElm.evaluate) { // IE6, IE7
            sPattern = ".//*[contains(concat(' ', @class, ' '), ' " + sClass + " ')]";
            oElements = oElm.evaluate(sPattern, oElm, null, 0, null);
            while ((i = oElements.iterateNext())) {
                aresults.push(i);
            }
        } else { // IE8
            oElements = oElm.getElementsByTagName("*");
            sPattern = new RegExp("(^|\\s)" + sClass + "(\\s|$)");
            for (i = 0; i < oElements.length; i++) {
                if ( sPattern.test(oElements[i].className) ) {
                    // this is the one I'm focused on
                    // I've tried creating a new object from the oElements object no luck
                    // I've tried adding the indexes manually (outside push) .item(n) no luck
                    // I've tried to aresults = oElements, fill results 0 - n, then use
                    //  aresults.length = length I want, and it doesn't trim like a standard
                    //  array.
                    aresults.push(oElements[i]);
                }
            }
        }
        if (!aresults.length) {return 4;}
        return aresults;
    };
}
SmOke_N
  • 146
  • 3
  • Check how you defined aresults, can't see it in the code you posted, but your problem looks like uninitialized array. Are you creating it with `aresults = []`? If yes, then I don't see any reason, why it should work like how you described. – jPO Jan 27 '15 at 12:12
  • @jPO, yes, it's defined under autVariable.IEExAutClassGetCollection (line 10) as you've suggested. Your comment suggest that this should ***not** work? Is that correct? – SmOke_N Jan 27 '15 at 12:28
  • alert the values in your array just before the return statement and see. I don't think it should behave that ways. The issue might be in oElements[i] value – jsjunkie Jan 27 '15 at 12:33
  • @jPO, I've debugged the internal values extensively. Before the return, the array does contain valid references to objects in question. But something gets lost in translation so to speak, from javascript to AutoIt. I can interact fine within the code with any of the elements, its when I try to bring them back into AutoIt's localization that its failing. – SmOke_N Jan 27 '15 at 12:37
  • Then posting that code would be more helpful. you have defined the prototype function as autVariable.IEExAutClassGetCollection which is same as the function name containing it. Check if the usage is correct. – jsjunkie Jan 27 '15 at 12:52
  • The code is in a complete library I've written, and utilizes many functions. Oops, didn't mean to enter this yet. The library part is simple, it calls IEExAutClassGetCollection as an object. I am not so much looking for a code solution as I am specifically looking if there is a means to return the data more like getElementsByTagName() would rather than an actual array. If you want to see the code, the library is here http://www.autoitscript.com/forum/topic/167035-ieex-ie-extended-library-with-some-javascript-options/#entry1221388 but I doubt you want to dig through all that code. – SmOke_N Jan 27 '15 at 13:23

0 Answers0