Nested Loops with Javascript

Discussions and Tech Support related to the iMacros Firefox Add-on, including the built-in Javascript scripting interface for playing .js files.
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
dave86c
Posts: 8
Joined: Tue Oct 11, 2016 4:41 pm

Nested Loops with Javascript

Post by dave86c » Tue Oct 11, 2016 4:59 pm

I could need some help with a double Loop in imacros. This is what I want to do:

Go to this page: "https://www.ebay-kleinanzeigen.de/s-musik-cds/c78" and extract all the offer titles into an csv file. Then, go to the next page and repeat the same steps until all results are extracted. I created 2 separate .iim macro´s for this. Macro1 for extraction and macro 2 for the next page click. Both macro´s are working fine in .iim so far.

Now I wrote this code in Javascript and it´s not working. This is the code:

var macro1 = "CODE:";
macro1 +="SET !ERRORIGNORE YES"+"\n";
macro1 +="VERSION BUILD=9030808 RECORDER=FX"+"\n";
macro1 +="URL GOTO=https://www.ebay-kleinanzeigen.de/s-musik-cds/c78"+"\n";
macro1 +="TAG POS=1 TYPE=SECTION ATTR=CLASS:"aditem-image" CONTENT=MOUSEOVER"+"\n";
macro1 +="TAG POS=R1 TYPE=A ATTR=* EXTRACT=HREF"+"\n";
macro1 +="SAVEAS TYPE=EXTRACT FOLDER=* FILE=javascript_test.csv"+"\n";

var macro2 = "CODE:";
macro2 +="TAG POS=1 TYPE=A ATTR=HREF:https://www.ebay-kleinanzeigen.de/s-sei ... platten/k0" +"\n";

for (j = 1;j < 5; j + +) { // I set the loop to 5 to test the first 5 pages. Is this correct?
for (i = 1, i < 28, i ++) { // I set this loop to 28 because there´s a maximum of 28 results on one page. Is this correct?
iimSet("i", i);
iimPlayCode(macro1);
}
iimPlayCode(macro2);
}

For the playback I tried normal Play and Loop. When I hit normal play it returns this error:
"SyntaxError: missing ; before statement, line NaN (Error code: -991)"

I did some research but cannot find the solution for this. Can anyone tell me if this is the correct way to do this and if not...what am I doing wrong here?

I am working on an Imac El Capitan and I use Firefox.

Looking forward to your replies :) Thanks!
chivracq
Posts: 8424
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: Nested Loops with Javascript

Post by chivracq » Tue Oct 11, 2016 8:43 pm

dave86c wrote:I need some help with a nested Loop using Javascript. I have a working .iim macro which extracts all offer titles (HREF´s) from the page and puts in into the excel file. This is the macro.

Code: Select all

VERSION BUILD=9030808 RECORDER=FX
TAB T=1
SET !ERRORIGNORE YES
URL GOTO=https://www.ebay-kleinanzeigen.de/s-musik-cds/c78
TAG POS={{!LOOP}} TYPE=SECTION ATTR=CLASS:"aditem-image" CONTENT=MOUSEOVER
TAG POS=R1 TYPE=A ATTR=* EXTRACT=HREF
SAVEAS TYPE=EXTRACT FOLDER=* FILE=javascript_test.csv
The problem comes when the macro hits the last item. Now I need to go to the next page and repeat the same steps again until all results are extracted. I created a Javascript file for that and this is how the code looks: (I set i<27 because there´s a maximum of 27 results on the page. )

Code: Select all

for(var i=0;i<27;i++)
{

var macro = "CODE";
macro +="SET !ERRORIGNORE YES"+"\n";
macro +="URL GOTO=https://www.ebay-kleinanzeigen.de/s-musik-cds/c78"+"\n";
macro +="TAG POS={{!LOOP}} TYPE=SECTION ATTR=CLASS:"aditem-image" CONTENT=MOUSEOVER"+"\n";
macro +="TAG POS=R1 TYPE=A ATTR=* EXTRACT=HREF"+"\n";
macro +="SAVEAS TYPE=EXTRACT FOLDER=* FILE=javascript_test.csv"+"\n";

iimPlay(macro)
}
When I run the code it returns this error:
SyntaxError: missing ; before statement, line NaN (Error code: -991)
Can someone tell me if this is the correct way to setup a nested loop? I tried different setups but none of them seems to work. I used the Play button and not the loop button.

Working on an

Code: Select all

Imac El Capitan with Firefox.
Looking forward to any replies!
Nested Loop with Javascript not working properly
Posted by dave86c on 11 Oct 2016, 21:13

I could need some help with a double Loop in imacros. This is what I want to do:

Go to this page: "https://www.ebay-kleinanzeigen.de/s-musik-cds/c78" and extract all the offer titles into an csv file. Then, go to the next page and repeat the same steps until all results are extracted. I created 2 separate .iim macro´s for this. Macro1 for extraction and macro 2 for the next page click. Both macro´s are working fine in .iim so far.

Now I wrote this code in Javascript and it´s not working. This is the code:

Code: Select all

var macro1 = "CODE:";
macro1 +="SET !ERRORIGNORE YES"+"\n";
macro1 +="VERSION BUILD=9030808 RECORDER=FX"+"\n";
macro1 +="URL GOTO=https://www.ebay-kleinanzeigen.de/s-musik-cds/c78"+"\n";
macro1 +="TAG POS=1 TYPE=SECTION ATTR=CLASS:"aditem-image" CONTENT=MOUSEOVER"+"\n";
macro1 +="TAG POS=R1 TYPE=A ATTR=* EXTRACT=HREF"+"\n";
macro1 +="SAVEAS TYPE=EXTRACT FOLDER=* FILE=javascript_test.csv"+"\n";

var macro2 = "CODE:";
macro2 +="TAG POS=1 TYPE=A ATTR=HREF:https://www.ebay-kleinanzeigen.de/s-seite:*/schallplatten/k0" +"\n";

for (j = 1;j < 5; j + +) { // I set the loop to 5 to test the first 5 pages. Is this correct?
for (i = 1, i < 28, i ++) { // I set this loop to 28 because there´s a maximum of 28 results on one page. Is this correct?
iimSet("i", i);
iimPlayCode(macro1);
}
iimPlayCode(macro2);
}
For the playback I tried normal Play and Loop. When I hit normal play it returns this error:
"SyntaxError: missing ; before statement, line NaN (Error code: -991)"
I did some research but cannot find the solution for this. Can anyone tell me if this is the correct way to do this and if not...what am I doing wrong here?

I am working on an

Code: Select all

Imac El Capitan and I use Firefox.
Looking forward to your replies :) Thanks!
No need to kind of try to spam the Forum by opening several similar Threads, you can always add some more Info later (or even edit your own Posts). I've deleted your 2nd Thread but still quote them both as they contain slightly different (+ complementary) Info.

+FCIM...! :mrgreen:
=> iMacros for FF v9.0.3, FF48/49...?, iMac El Capitan.
(Even if exact FF Version will probably not play a role from quick reading your 2 Posts but always mention clearly your FCI when you open a Thread, I don't read/react otherwise..., read my Sig...)
- (F)CIM = (Full) Config Info Missing: iMacros + Browser + OS with all 3 Versions...
- I usually don't even read the Question if that (required) Info is not mentioned...
- Script & URL usually help a lot for a more "educated" Help...
dave86c
Posts: 8
Joined: Tue Oct 11, 2016 4:41 pm

Re: Nested Loops with Javascript

Post by dave86c » Wed Oct 12, 2016 1:32 am

Sorry for the double post. I was not sure if I uploaded the first one. It took quite long before my post was online and I had a lot of open windows and no overview anymore ;)

Is it poossible to delete your post to make this thread a little more readable with less information?

I have been playing around with the code and I have an almost working version. This is the code:

var macro1 = "CODE:";
macro1 +="SET !ERRORIGNORE YES" +"\n";
macro1 +="VERSION BUILD=9030808 RECORDER=FX" +"\n";
//macro1 +="URL GOTO=https://www.ebay-kleinanzeigen.de/s-bue ... l/c93"+"\n";
macro1 +="WAIT SECONDS=2" +"\n";
macro1 +="TAG POS={{!i}} TYPE=SECTION ATTR=CLASS:aditem-image CONTENT=MOUSEOVER" +"\n";
macro1 +="WAIT SECONDS=2" +"\n";
macro1 +="TAG POS=R1 TYPE=A ATTR=* EXTRACT=HREF" +"\n";
macro1 +="WAIT SECONDS=2" +"\n";
macro1 +="SAVEAS TYPE=EXTRACT FOLDER=* FILE=javascript_test.csv" +"\n";

var macro2 = "CODE:";

macro2 +="WAIT SECONDS=2" +"\n";
macro2 +="TAG POS={{!LOOP}} TYPE=A ATTR=HREF:https://www.ebay-kleinanzeigen.de/s-bue ... eite:*/c93" +"\n";

for (j = 1; j < 5; j ++) {
for (i = 1; i < 4; i ++) {
//iimSet("i", i);
iimPlay(macro1);
}
iimPlay(macro2);
}

The output is a little weird. I had 2 different outputs with the same macro (no changes to the code or anything)

1. Sometimes the macro extracts the titles from first page, then goes to the second page and doesn´t extract there.
2. It doesn´t extract any titles. It only does the next page loop. CSV file remains blank afterwards.

Then, in case 1 the CSV output is always different. Sometimes the extracted text starts at line 2 and sometimes in line 14 or something.

Is there anything I do wrong here? I am also not sure about the POS={{!i}} and POS={{!LOOP}} in line 6 and 15. But it seems to work so far (sometimes)

I am working with this system:
iMacros for FF 9030808 FF 49. iMac El Capitan version 10.11.4
iimfun
Posts: 239
Joined: Tue Jul 19, 2016 1:06 pm

Re: Nested Loops with Javascript

Post by iimfun » Wed Oct 12, 2016 12:53 pm

First of all try this script below on a certain page. Does it work as you want?

Code: Select all

var macro0 = "FILTER TYPE=IMAGES STATUS=ON" + "\n";
macro0 += "URL GOTO=https://www.ebay-kleinanzeigen.de/s-bueromoebel/c93" + "\n";

var macro1 = "TAG POS={{i}} TYPE=SECTION ATTR=CLASS:aditem-image CONTENT=MOUSEOVER" + "\n";
macro1 += "TAG POS=R1 TYPE=A ATTR=* EXTRACT=TXT" + "\n";
macro1 += "SAVEAS TYPE=EXTRACT FOLDER=* FILE=javascript_test.csv" + "\n";

iimPlayCode(macro0);

for (i = 1; i < 28; i++) {
	iimSet("i", i);
	var ret = iimPlayCode(macro1);
	if (ret != 1)
		break;
}
dave86c
Posts: 8
Joined: Tue Oct 11, 2016 4:41 pm

Re: Nested Loops with Javascript

Post by dave86c » Wed Oct 12, 2016 12:58 pm

When i play it, it gives me this error:

MacroSyntaxError: wrong format of TAG command, line 4 (Error code: -910)

I had a look at line 4 and can´t see anything wrong there.

Also could you tell me what the first line is doing?

var macro0 = "FILTER TYPE=IMAGES STATUS=ON" + "\n";

thanks :)

** Sorry I already found out about the Filter line. Makes sense!
iimfun
Posts: 239
Joined: Tue Jul 19, 2016 1:06 pm

Re: Nested Loops with Javascript

Post by iimfun » Wed Oct 12, 2016 1:16 pm

To downgrade your iMacros for Firefox to 8.9.7 is a good advice.
dave86c wrote:I had a look at line 4 and can´t see anything wrong there.
I don't see line #4 in the macros at all (only 2 and 3 lines). Post the script you're playing.
dave86c
Posts: 8
Joined: Tue Oct 11, 2016 4:41 pm

Re: Nested Loops with Javascript

Post by dave86c » Wed Oct 12, 2016 1:21 pm

OKay I going to downgrade now. Do you know if all my macro´s will be gone if I downgrade? You think I need to save them somewhere else?

This is the script I´m playing. It´s exactly the one you sent me:

var macro0 = "FILTER TYPE=IMAGES STATUS=ON" + "\n";
macro0 += "URL GOTO=https://www.ebay-kleinanzeigen.de/s-bueromoebel/c93" + "\n";

var macro1 = "TAG POS={{i}} TYPE=SECTION ATTR=CLASS:aditem-image CONTENT=MOUSEOVER" + "\n";
macro1 += "TAG POS=R1 TYPE=A ATTR=* EXTRACT=TXT" + "\n";
macro1 += "SAVEAS TYPE=EXTRACT FOLDER=* FILE=javascript_test.csv" + "\n";

iimPlayCode(macro0);

for (i = 1; i < 28; i++) {
iimSet("i", i);
var ret = iimPlayCode(macro1);
if (ret != 1)
break;
}
iimfun
Posts: 239
Joined: Tue Jul 19, 2016 1:06 pm

Re: Nested Loops with Javascript

Post by iimfun » Wed Oct 12, 2016 1:32 pm

dave86c wrote:OKay I going to downgrade now. Do you know if all my macro´s will be gone if I downgrade? You think I need to save them somewhere else?
Make a backup if you are not sure.

I tested my code. It works fine.
dave86c
Posts: 8
Joined: Tue Oct 11, 2016 4:41 pm

Re: Nested Loops with Javascript

Post by dave86c » Wed Oct 12, 2016 1:46 pm

allright, it works. The downgrade seems to make sense!

However this is only the first part of the macro and not the second loop where it goes to the next page and continues extracting. Can I use my old for loop to do that?
iimfun
Posts: 239
Joined: Tue Jul 19, 2016 1:06 pm

Re: Nested Loops with Javascript

Post by iimfun » Wed Oct 12, 2016 2:08 pm

It seems to me that someone is very lucky today. Here's a gift for you

Code: Select all

var macro0 = "FILTER TYPE=IMAGES STATUS=ON" + "\n";
macro0 += "URL GOTO=https://www.ebay-kleinanzeigen.de/s-bueromoebel/c93" + "\n";

var macro1 = "SET !TIMEOUT_STEP 0" + "\n";
macro1 += "TAG POS={{i}} TYPE=SECTION ATTR=CLASS:aditem-image CONTENT=MOUSEOVER" + "\n";
macro1 += "TAG POS=R1 TYPE=A ATTR=* EXTRACT=TXT" + "\n";
macro1 += "SAVEAS TYPE=EXTRACT FOLDER=* FILE=javascript_test.csv" + "\n";

var macro2 = "WAIT SECONDS=2" +"\n";
macro2 += "FILTER TYPE=IMAGES STATUS=ON" + "\n";
macro2 += "TAG POS=1 TYPE=A ATTR=TXT:{{j}}" +"\n";

iimPlayCode(macro0);

for (j = 1; j <= 5; j++) {
	iimSet("j", j);
	iimPlayCode(macro2);
	
	for (i = 1; i <= 28; i++) {
		iimSet("i", i);
		var ret = iimPlayCode(macro1);
		if (ret != 1)
			break;
	}
}
dave86c
Posts: 8
Joined: Tue Oct 11, 2016 4:41 pm

Re: Nested Loops with Javascript

Post by dave86c » Wed Oct 12, 2016 2:27 pm

NICEE! Thanks a lot for this :) Appreciate it

This is exactly what I need to start with. Now I´m going to add more elements to the code. For example the item description (which comes when you click on the product) and put is into column B in the Excel Sheet. I think I can add that because I have some code which does that part.

I might need some help with the following:

Is it possible to exclude some titles from extracting. For example everything which contains "chair" or "table" I have some very basic Javascript knowledge. Would it make sense to put all titles into an array. Loop through it with an if / else statement and then make a choice for extracting or not? I have all those negative keywords in an excel file...

Looking forward :)
dave86c
Posts: 8
Joined: Tue Oct 11, 2016 4:41 pm

Re: Nested Loops with Javascript

Post by dave86c » Wed Oct 12, 2016 10:14 pm

So far I have this code for the item description extracting. I used your code and I added some code to macro1. When I play this I get this error:

unknown command: VAR, line 1 (Error code: -910)

I can´t figure out what the problem is. Is this the correct way to extract item description?


Code is:

var macro0 = "FILTER TYPE=IMAGES STATUS=ON" + "\n";
macro0 += "URL GOTO=https://www.ebay-kleinanzeigen.de/s-bueromoebel/c93" + "\n";

var macro1 = "SET !TIMEOUT_STEP 0" + "\n";
macro1 += "TAG POS={{i}} TYPE=SECTION ATTR=CLASS:aditem-image CONTENT=MOUSEOVER" + "\n";
macro1 += "TAG POS=R1 TYPE=A ATTR=* EXTRACT=TXT" + "\n";
macro1 += "SET title {{!EXTRACT}}" + "\n";
macro1 += "SET !EXTRACT NULL" + "\n";

macro1 += "TAG POS={{!i}} TYPE=SECTION ATTR=CLASS:"aditem-image" CONTENT=MOUSEOVER" + "\n";
macro1 += "TAG POS=R1 TYPE=A ATTR=* EXTRACT=HREF" + "\n";
macro1 += "SET url {{!EXTRACT}}" + "\n";
macro1 += "SET !EXTRACT NULL" + "\n";

macro1 += "WAIT SECONDS=3" + "\n";
macro1 += "URL GOTO={{url}}" + "\n";
macro1 += "TAG POS=1 TYPE=P ATTR=ID:viewad-description-text EXTRACT=TXT" + "\n";
macro1 += "SET description {{!EXTRACT}}" + "\n";

macro1 += "SET !EXTRACT {{title}}[EXTRACT]{{description}}[EXTRACT]{{url}}[EXTRACT]" + "\n";
macro1 += "SAVEAS TYPE=EXTRACT FOLDER=* FILE=test1.csv" + "\n";
macro1 += "BACK" + "\n";


var macro2 = "WAIT SECONDS=2" +"\n";
macro2 += "FILTER TYPE=IMAGES STATUS=ON" + "\n";
macro2 += "TAG POS=1 TYPE=A ATTR=TXT:{{j}}" +"\n";

iimPlayCode(macro0);

for (j = 1; j <= 13; j++) {
iimSet("j", j);
iimPlayCode(macro2);

for (i = 1; i <= 28; i++) {
iimSet("i", i);
var ret = iimPlayCode(macro1);
if (ret != 1)
break;
}
}
iimfun
Posts: 239
Joined: Tue Jul 19, 2016 1:06 pm

Re: Nested Loops with Javascript

Post by iimfun » Thu Oct 13, 2016 7:25 am

dave86c wrote:Is it possible to exclude some titles from extracting. For example everything which contains "chair" or "table" I have some very basic Javascript knowledge. Would it make sense to put all titles into an array. Loop through it with an if / else statement and then make a choice for extracting or not? I have all those negative keywords in an excel file...
OK! All that you mentioned is possible... At least for me.
dave86c wrote:unknown command: VAR, line 1 (Error code: -910)

I can´t figure out what the problem is.
Put the code into a file having 'js' extension.
dave86c
Posts: 8
Joined: Tue Oct 11, 2016 4:41 pm

Re: Nested Loops with Javascript

Post by dave86c » Thu Oct 13, 2016 8:24 am

Could you maybe give me some help and push me in the right direction with the negative keywords?

The file already has the .js Extension.
chivracq
Posts: 8424
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: Nested Loops with Javascript

Post by chivracq » Thu Oct 13, 2016 4:05 pm

iimfun wrote:
dave86c wrote:Is it possible to exclude some titles from extracting. For example everything which contains "chair" or "table" I have some very basic Javascript knowledge. Would it make sense to put all titles into an array. Loop through it with an if / else statement and then make a choice for extracting or not? I have all those negative keywords in an excel file...
OK! All that you mentioned is possible... At least for me.
Ah-ah...! That makes 2 of us...! :oops:
You can do everything with iMacros, you just need to be a little bit "Creative" and everything is possible... :idea:

@OP, concerning your wish "to exclude some titles", I've recently implemented a very similar Mechanism (in pure '.iim') for some other Forum User in the following Thread where I explained and applied a few of the "Advanced" Tricks and Techniques that I regularly use for my own Scripts:
- exclude content of a txt/csv file when running script
- (F)CIM = (Full) Config Info Missing: iMacros + Browser + OS with all 3 Versions...
- I usually don't even read the Question if that (required) Info is not mentioned...
- Script & URL usually help a lot for a more "educated" Help...
Post Reply