document.links.length var / .js or .iim???

Discussions and Tech Support specific to the iMacros Firefox add-on.
Forum rules
Before asking a question or reporting an issue:
1. Please review the list of FAQ's.
2. Use the search box (at the top of each forum page) to see if a similar problem or question has already been addressed.
3. Try searching the iMacros Wiki - it contains the complete iMacros reference as well as plenty of samples and tutorials.
4. We can respond much faster to your posts if you include the following information: CLICK HERE FOR IMPORTANT INFORMATION TO INCLUDE IN YOUR POST
Post Reply
danieljph
Posts: 5
Joined: Tue Nov 17, 2009 12:57 am

document.links.length var / .js or .iim???

Post by danieljph » Tue Nov 17, 2009 6:26 pm

beloved macro-men+women--I am somewhat of a novice here, apologies in advance:

I am trying to use document.links.length to call the number of links on a web page, and to then pass that variable onto a macro called test.js.

I understand we can call document.links.length using 2 methods |http://forum.imacros.net/viewtopic.php?f=6&t=3983| of execution from within iMacros for Firefox:

via test.js--

Code: Select all

if(frames.length<1){alert('This page has '+document.links.length+' links.')}else{alert('The page has frames!')}
RETURNS: "This page has frames!" (I believe this might have something to do with the iMacros sidebar?)

via test.iim
-or-
via iimPlay("CODE:...") by way of test.js

Code: Select all

URL GOTO=javascript:if(frames.length<1){alert('This<SP>page<SP>has<SP>'+document.links.length+'<SP>links.')}else{alert('The<SP>page<SP>has<SP>frames!')}
RETURNS: "This page has 166 links."


Q: Is there a way to pass document.links.length onto test.js while executed using iimPlay("CODE:...")?

I've tried--

Code: Select all

ONDIALOG POS=1 BUTTON=OK CONTENT={{!EXTRACT}}
to no avail...
[/size]
ABooth
Posts: 223
Joined: Mon Aug 10, 2009 4:25 pm

Re: document.links.length var / .js or .iim???

Post by ABooth » Tue Nov 17, 2009 8:14 pm

danieljph wrote:beloved macro-men+women--I am somewhat of a novice here, apologies in advance:

I am trying to use document.links.length to call the number of links on a web page, and to then pass that variable onto a macro called test.js.

I understand we can call document.links.length using 2 methods |http://forum.imacros.net/viewtopic.php?f=6&t=3983| of execution from within iMacros for Firefox:

via test.js--

Code: Select all

if(frames.length<1){alert('This page has '+document.links.length+' links.')}else{alert('The page has frames!')}
RETURNS: "This page has frames!" (I believe this might have something to do with the iMacros sidebar?)

via test.iim
-or-
via iimPlay("CODE:...") by way of test.js

Code: Select all

URL GOTO=javascript:if(frames.length<1){alert('This<SP>page<SP>has<SP>'+document.links.length+'<SP>links.')}else{alert('The<SP>page<SP>has<SP>frames!')}
RETURNS: "This page has 166 links."


Q: Is there a way to pass document.links.length onto test.js while executed using iimPlay("CODE:...")?

I've tried--

Code: Select all

ONDIALOG POS=1 BUTTON=OK CONTENT={{!EXTRACT}}
to no avail...
[/size]
You could make test.js call the macro with iimPlay(). The javascript in the macro could include something like

Code: Select all

document.write("<input type='hidden' id='iMacroExtract' value = '" + document.links.length + "' />");
Then use the imacros Extract on the next line

Code: Select all

TAG POS=1 TYPE=INPUT:HIDDEN ATTR=ID:iMacroExtract EXTRACT=TXT
Then in your test.js : -

Code: Select all

var extract = iimGetLastExtract ( );
Example: -

Code: Select all

var totalLinks = ( 1 == iimPlay('Macro') ) ? parseInt( iimGetLastExtract ( ) ) : 0;
and the Macro.iim

Code: Select all

URL GOTO=javascript:if(frames.length<1) document.write("<input type='hidden' id='iMacroExtract' value = '" + document.links.length + "' />");
TAG POS=1 TYPE=INPUT:HIDDEN ATTR=ID:iMacroExtract EXTRACT=TXT
iMacros for Firefox supports JavaScript Macros (Scripting)
JavaScript supports Java via LiveConnect

Therefore: You can write powerful macros with iMacros for Firefox. Have a look at this one

Post feature requests here. Maybe one day, they'll pin it?
danieljph
Posts: 5
Joined: Tue Nov 17, 2009 12:57 am

Re: document.links.length var / .js or .iim???

Post by danieljph » Tue Nov 17, 2009 10:10 pm

@ABooth: First and foremost, thank you for taking a moment to address this little kink I've wrapped myself around.

Logic-wise, I'm right there with you.

However, I'm stumbling a bit over the syntax. Do you think you might walk me through its proper execution?

iMacros v. Javascript
<SP> v. %20
I'm no stranger to trial and error--but any guidance you might offer would be greatly appreciated. I'm surprised I've made it this far, only to realize I have so much more yet to learn.
-Daniel
ABooth
Posts: 223
Joined: Mon Aug 10, 2009 4:25 pm

Re: document.links.length var / .js or .iim???

Post by ABooth » Wed Nov 18, 2009 9:05 am

danieljph wrote:@ABooth: First and foremost, thank you for taking a moment to address this little kink I've wrapped myself around.

Logic-wise, I'm right there with you.

However, I'm stumbling a bit over the syntax. Do you think you might walk me through its proper execution?

iMacros v. Javascript
<SP> v. %20
I'm no stranger to trial and error--but any guidance you might offer would be greatly appreciated. I'm surprised I've made it this far, only to realize I have so much more yet to learn.
-Daniel
Well, if you need to represent a space in your im macro file, you would replace spaces with <sp>. This is iMacros specific syntax.
The %20 is a URL encoded representation of a space character, because a space (as well as some other characters) are not allowed in a url.

Both <sp> and %20 do the same thing, they just represent space characters for different uses. <sp> in an iim file and %20 as part of a URL

%20 means hexadecimal 20. This is the numeric representation of a space character.

If you wanted to go to the windows "C:\Program Files" folder in a web browser, the path would be: -

Code: Select all

file:///C:/Program%20Files/
where: -
  • file:// is the protocol (like http:// for web pages, ftp:// for file transfer protocol etc)
  • /C:/Program%20Files/ is the path. Note the space has been replaced with %20, which the browser parses as space.
In fact, you can get any character and url encode them. Just find out what the hexadecimal value is and prefix with %
So 'A' , which is character 65 decimal can be represented as %41 as 41 is hexadecimal for 65 decimal
It is not uncommon to obfuscate urls to make hacking that bit more difficult.

Paste the following into IE (not Firefox) address bar as an example

Code: Select all

http://%77%77%77%2E%67%6F%6F%67%6C%65%2E%63%6F%6D
In javascript, you would not need to use url encoding, unless you were doing something like a redirect to a url that had spaces in it.
If you need to mark the spaces for the javascript in the iim script, use <sp>, because it's still an iMacro.

The iMacro code (iim file) would look like this:-

Code: Select all

URL GOTO=javascript:if(frames.length<1)<SP>document.write("<input<SP>type='hidden'<SP>id='iMacroExtract'<SP>value<SP>=<SP>'"<SP>+<SP>document.links.length<SP>+<SP>"'<SP>/>");
TAG POS=1 TYPE=INPUT:HIDDEN ATTR=ID:iMacroExtract EXTRACT=TXT
iMacros for Firefox supports JavaScript Macros (Scripting)
JavaScript supports Java via LiveConnect

Therefore: You can write powerful macros with iMacros for Firefox. Have a look at this one

Post feature requests here. Maybe one day, they'll pin it?
danieljph
Posts: 5
Joined: Tue Nov 17, 2009 12:57 am

Re: document.links.length var / .js or .iim???

Post by danieljph » Wed Nov 18, 2009 5:24 pm

That makes sense. Thanks for the crash-course in encoded-representation-of-space-characters.

When I execute this using the Firefox Add-on--
RETURNS: Blank Page / Loading...
It seems to just go on forever and ever.

Is this unique to my setup?
Do you think there is something I can change in about:config?

Thank-You x 1,000,000
-Daniel

UPDATE!!!

I've included SET !TIMEOUT & !ERRORIGNORE to your code (Macro.iim):

Code: Select all

SET !TIMEOUT 5
SET !ERRORIGNORE YES
URL GOTO=javascript:if(frames.length<1)<SP>document.write("<input<SP>type='hidden'<SP>id='iMacroExtract'<SP>value<SP>=<SP>'"<SP>+<SP>document.links.length<SP>+<SP>"'<SP>/>");
TAG POS=1 TYPE=INPUT:HIDDEN ATTR=ID:iMacroExtract EXTRACT=TXT
BACK
Then, to test.js I've added an alert to check our return:

Code: Select all

var totalLinks = ( 1 == iimPlay('Macro') ) ? parseInt( iimGetLastExtract ( ) ) : 0;
var extract = iimGetLastExtract ( );
alert(extract)
test.js RETURNS: document.links.length extract var correct!!!
however, I'm getting: Error# 802, Website did not respond (Timeout)

Q: Does Javascript allow a function similar to !ERRORIGNORE?

UPDATE!!!

2009-11-05 V6.3.0 Beta for Firefox solves Error# 802.
However, our script now returns: #EANF#

So close, I can almost feel it! Thanks in advance for any advice you might offer.
[/size]
ABooth
Posts: 223
Joined: Mon Aug 10, 2009 4:25 pm

Re: document.links.length var / .js or .iim???

Post by ABooth » Wed Nov 18, 2009 7:41 pm

danieljph wrote:That makes sense. Thanks for the crash-course in encoded-representation-of-space-characters.

When I execute this using the Firefox Add-on--
RETURNS: Blank Page / Loading...
It seems to just go on forever and ever.

Is this unique to my setup?
Do you think there is something I can change in about:config?

Thank-You x 1,000,000
-Daniel

UPDATE!!!

I've included SET !TIMEOUT & !ERRORIGNORE to your code (Macro.iim):

Code: Select all

SET !TIMEOUT 5
SET !ERRORIGNORE YES
URL GOTO=javascript:if(frames.length<1)<SP>document.write("<input<SP>type='hidden'<SP>id='iMacroExtract'<SP>value<SP>=<SP>'"<SP>+<SP>document.links.length<SP>+<SP>"'<SP>/>");
TAG POS=1 TYPE=INPUT:HIDDEN ATTR=ID:iMacroExtract EXTRACT=TXT
BACK
Then, to test.js I've added an alert to check our return:

Code: Select all

var totalLinks = ( 1 == iimPlay('Macro') ) ? parseInt( iimGetLastExtract ( ) ) : 0;
var extract = iimGetLastExtract ( );
alert(extract)
test.js RETURNS: document.links.length extract var correct!!!
however, I'm getting: Error# 802, Website did not respond (Timeout)

Q: Does Javascript allow a function similar to !ERRORIGNORE?

UPDATE!!!

2009-11-05 V6.3.0 Beta for Firefox solves Error# 802.
However, our script now returns: #EANF#

So close, I can almost feel it! Thanks in advance for any advice you might offer.
[/size]

#EANF# means the extract failed because it couldn't find the html element to extract from.
It could be that it rendered the javascript too slowly because the updated macro engine ran faster. If this is the case, you could try inserting a pause in the imacro. So: -

Code: Select all

SET !TIMEOUT 5
SET !ERRORIGNORE YES
URL GOTO=javascript:if(frames.length<1)<SP>document.write("<input<SP>type='hidden'<SP>id='iMacroExtract'<SP>value<SP>=<SP>'"<SP>+<SP>document.links.length<SP>+<SP>"'<SP>/>");

'---------- Added the line below ----------
WAIT SECONDS=1
'----------------------------------------------

TAG POS=1 TYPE=INPUT:HIDDEN ATTR=ID:iMacroExtract EXTRACT=TXT
BACK
Adding the [SET !ERRORIGNORE YES] will in fact hide any problems, it will not solve them. When debugging, it would be better to remove/comment out this line. It might identify the reason the extract failed?
iMacros for Firefox supports JavaScript Macros (Scripting)
JavaScript supports Java via LiveConnect

Therefore: You can write powerful macros with iMacros for Firefox. Have a look at this one

Post feature requests here. Maybe one day, they'll pin it?
danieljph
Posts: 5
Joined: Tue Nov 17, 2009 12:57 am

Re: document.links.length var / .js or .iim???

Post by danieljph » Wed Nov 18, 2009 7:59 pm

Wow. It works. (and it's damn beautiful, to boot{h}...) Thank you.
ABooth
Posts: 223
Joined: Mon Aug 10, 2009 4:25 pm

Re: document.links.length var / .js or .iim???

Post by ABooth » Wed Nov 18, 2009 8:08 pm

danieljph wrote:Wow. It works. (and it's damn beautiful, to boot{h}...) Thank you.
You're welcome. :D

BTW: I noticed you did this:

Code: Select all

var totalLinks = ( 1 == iimPlay('Macro') ) ? parseInt( iimGetLastExtract ( ) ) : 0;
var extract = iimGetLastExtract ( );
alert(extract);
You call the iimGetLastExtract twice, when the result was already stored in totalLinks

This would have worked just fine: -

Code: Select all

var totalLinks = ( 1 == iimPlay('Macro') ) ? parseInt( iimGetLastExtract ( ) ) : 0;
alert(totalLinks);
iMacros for Firefox supports JavaScript Macros (Scripting)
JavaScript supports Java via LiveConnect

Therefore: You can write powerful macros with iMacros for Firefox. Have a look at this one

Post feature requests here. Maybe one day, they'll pin it?
danieljph
Posts: 5
Joined: Tue Nov 17, 2009 12:57 am

Re: document.links.length var / .js or .iim???

Post by danieljph » Thu Nov 19, 2009 12:33 am

I am forever appreciative.
ABooth
Posts: 223
Joined: Mon Aug 10, 2009 4:25 pm

Re: document.links.length var / .js or .iim???

Post by ABooth » Thu Nov 19, 2009 5:35 pm

danieljph wrote:I am forever appreciative.
Thank you.
iMacros for Firefox supports JavaScript Macros (Scripting)
JavaScript supports Java via LiveConnect

Therefore: You can write powerful macros with iMacros for Firefox. Have a look at this one

Post feature requests here. Maybe one day, they'll pin it?
Post Reply