SmOke_N

Fresh pair of eyes... Javascript array to AutoIt

14 posts in this topic

Those of you familiar with javascript (probably most more than I), please have a look at this function.

Specifically the area where getElementsByTagName is.

I can return any standard object + item, but when I try to populate an array with objects and return them, all I get back is the initiating object with null array index objects.

If you take a look at the comments, you'll see I tried many things... one of the things not listed is I thought maybe the referenced object went out of scope and tried a global variable container that also didn't work.

If you have any ideas on what may help here I'd appreciate it, been banging my head on this one for hours.

//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;
    };
}

.


[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites



I just had an eye surgery ... but I do not think this is what you mean by "fresh pair of eyes" :D

1 person likes this

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

This is really over my head, but I would like to say this. Whenever I have used javascript, I have always looked for a working example that I can modify and play around with. I need to see a script in action to understand it. Perhaps you can find an implemetation of this, or a similar, function somewhere.

Edit: Perhaps I'm stating the obvious.

Edited by czardas

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

The commented out section works fine, it returns object indexes fine when I call something like: getElementsByTagName, getElementsByClassName, or querySelectorAll directly and return the results.  It's only failing when I try to push object references into an array and return the array.  The array is fine if I just use it within IE itself.

Edited by SmOke_N

[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

Yes, extensively.  That particular thread uses native autoit eval, which would cause issues for those that eval fails for (me as an example on IE9 and how this got started to begin with).  As far as ozmike's solution, it's going to run into the same situation, other than his object creation method (which I'm totally confused on what he's actually accomplishing or how he's accomplishing it there).


[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

Thanks jaberwacky, I actually made that comment in my own stackoverflow help request.


[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

dont know if obects literals have acces to "slice" and "push", but maybe you can try to use "slice" to remove unneeded elements from array instead of trying to recreate it yourself, and see can you access original array that don't have specific elements anymore? 

Btw. can you show autoit code that don't get array results, so that others can test and play around cos if i understand autoit part of code don't get results and you tested that your javascript it's working fine? So where is your autoit script? :P  :)

edit: full of wrong spelling, no wonder i edit every post like 50 times :)

Edited by bogQ

TCP server and client - Learning about TCP servers and clients connection
Au3 oIrrlicht - Irrlicht project
Au3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related)



460px-Thief-4-temp-banner.jpg
There are those that believe that the perfect heist lies in the preparation.
Some say that it’s all in the timing, seizing the right opportunity. Others even say it’s the ability to leave no trace behind, be a ghost.

 

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

Quite funny you posted that... I was just reading up on the prototype actions:

http://nfriedly.com/techblog/2009/06/advanced-javascript-objects-arrays-and-array-like-objects/

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice

http://eloquentjavascript.net/13_dom.html

I have a feeling I'm going to have to go down a custom type of road though:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys with some kind of polyfill

Edit:

@bogQ

I'm sorry, I thought I mentioned it before, it's for the library I wrote:

_IEEx_JSClassNameGetArray function (and I'm sure a few others now that I see this issue)

IE8 or less version (which is the javascript I don't have commented out above.

Edited by SmOke_N

[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

are your reffering to post number 5 problem looks i can confirm that your $oItems.length is diffrent from count in $oItem In $oItems

coz length return 6 and $oItem In $oItems count 49 O_o or  you posted link to #5 dont have nothing to do with problem

edit: dumb buggy javascipt, maybe your getting undefined if you used delite option on array?

Edit:

note that maybe problem is that some elements maybe don't have class name so maybe

 if ( sPattern.test(oElements[i].className) ) {

this part is incorrect?

Edited by bogQ

TCP server and client - Learning about TCP servers and clients connection
Au3 oIrrlicht - Irrlicht project
Au3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related)



460px-Thief-4-temp-banner.jpg
There are those that believe that the perfect heist lies in the preparation.
Some say that it’s all in the timing, seizing the right opportunity. Others even say it’s the ability to leave no trace behind, be a ghost.

 

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

I believe I've fixed that issue so far... It's the empty array indexes that he received.  I've confirmed they are empty (non-objects).

Edit:

In the code (if you concentrate on the getElementsByTagName area, when the code in that section is done, I confirm that the array holds the right object references.

The issue is, I get back the "array reference" with a length to the array, but the indexes to the array (in that case 6 indexes I believe) are empty on the autoit side.

Edited by SmOke_N

[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

thats why i suggested to look @ sPattern.test(oElements[i].className)

Coz if it return undefined and still write it as undefined object

so logic in my head if....

sClass  is equal to 'undefined'

and oElements[i].className is null ellement (no class for it)

then

 if ( sPattern.test(oElements[i].className) ) is trigered and maybe will hold 49 where 43 are undefined

and length maybe don't count undefined in its array ... so your getting 6 in array of 49 where 43 are undefined coz your "if case" on sPattern.test is wrongly formated.

then  my question is how many if casse-s are triggered in that java loop? im not talking about return results from length , if its 49 that your "if case" need to be changed coz its writing undefined to new array.

 

so i thought that maybe something like that is cause of a problem here or my logic in my head is false and undefined  :)  :P

Edited by bogQ

TCP server and client - Learning about TCP servers and clients connection
Au3 oIrrlicht - Irrlicht project
Au3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related)



460px-Thief-4-temp-banner.jpg
There are those that believe that the perfect heist lies in the preparation.
Some say that it’s all in the timing, seizing the right opportunity. Others even say it’s the ability to leave no trace behind, be a ghost.

 

Share this post


Link to post
Share on other sites

sPattern only continues upon success.  So the array wouldn't be enumerated at all if it weren't successful.

But I understand your logic all the same ;).

I'm no longer concerned with it returning 49, it was an enumeration issue when using For/In, I've changed that on my end while I try to get to the real issue, the $oElements object reference IS going properly into the array.  If I were to pass that array to any other javascript function, it is (would be) fine.  It's losing something coming back to AutoIt.

Ideally we would want to create the same type of object that functions like getElementsByTagName() or querySelectorAll() return.  These objects we are able to enum properly, while they're not standard "array", they are "array-like" for javascript.  I need to find the means to create the "like" part :) .


[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now