Loop an entire .js file

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
Post Reply
effykingly
Posts: 15
Joined: Fri Sep 26, 2014 11:17 am

Loop an entire .js file

Post by effykingly » Sun Sep 28, 2014 9:06 am

I was having trouble creating a js file but it was solved. NowI cometo find ut clicking play loop in imacro doesn't work for .js files.

My question is how would I loop this entire code:

Code: Select all

    main.js
    var macro;
    macro =  "CODE:";
    macro +=  "VERSION BUILD=8820413 RECORDER=FX" + "\n";
    macro +=  "SET !ERRORIGNORE YES" + "\n";
    macro +=  "TAB T=1" + "\n";
    macro +=  "URL GOTO=http://www.website.com/canon/[seriesname]/videos" + "\n";
    macro +=  "FRAME F=1" + "\n";
    macro +=  "TAG POS=1 TYPE=SPAN ATTR=TXT:(1<SP>of<SP>5) EXTRACT=TXT" + "\n";
    macro +=  "TAG POS=1 TYPE=SPAN ATTR=TXT:(1<SP>of<SP>10) EXTRACT=TXT" + "\n";
    macro +=  "TAG POS=1 TYPE=DIV ATTR=TXT:<SP>We<SP>are<SP>all<SP>out<SP>of<SP>offers.<SP>Please<SP>check<SP>back<SP>later<SP>for<SP>more. EXTRACT=TXT" + "\n";
    macro +=  "TAB T=2" + "\n";
    macro +=  "TAG POS=1 TYPE=DIV ATTR=TXT:Step<SP>5 EXTRACT=TXT" + "\n";
    macro +=  "TAG POS=1 TYPE=DIV ATTR=TXT:Step<SP>10 EXTRACT=TXT" + "\n";
    iimPlay(macro)
    if (iimGetLastExtract(1)== "(1 of 5)") {
        iimPlay('5.iim');
    } else if (iimGetLastExtract(2) == "(1 of 10)") {
        iimPlay('10.iim');
    } else if (iimGetLastExtract(4)== "Step 5" && iimGetLastExtract(5) != "Step 10") {
        iimPlay('5e.iim');
    } else if (iimGetLastExtract(4) == "Step 5" && iimGetLastExtract(5) == "Step 10") {
        iimPlay('10e.iim');
    } else if (iimGetLastExtract(3) == "We are all out of offers. Please check back later for more.") {
        iimPlay('none.iim');
    } else {
        iimPlay('single.iim');
    }
skippyto
Posts: 108
Joined: Wed Aug 20, 2008 8:14 am

Re: Loop an entire .js file

Post by skippyto » Mon Sep 29, 2014 9:49 am

Hi effykingly,

Just put your code in a javascript loop, too solutions depending on what kind of loop you want :

http://www.w3schools.com/js/js_loop_while.asp
http://www.w3schools.com/js/js_loop_for.asp

Skippyto.
effykingly
Posts: 15
Joined: Fri Sep 26, 2014 11:17 am

Re: Loop an entire .js file

Post by effykingly » Mon Sep 29, 2014 9:54 am

I think I found the solution by enclosing the contents in:

Code: Select all

for (var i=0;i<=10;i++)
{
        var macro;
        macro =  "CODE:";
        macro +=  "VERSION BUILD=8820413 RECORDER=FX" + "\n";
        macro +=  "SET !ERRORIGNORE YES" + "\n";
        macro +=  "TAB T=1" + "\n";
        macro +=  "URL GOTO=http://www.website.com/canon/[seriesname]/videos" + "\n";
        macro +=  "FRAME F=1" + "\n";
        macro +=  "TAG POS=1 TYPE=SPAN ATTR=TXT:(1<SP>of<SP>5) EXTRACT=TXT" + "\n";
        macro +=  "TAG POS=1 TYPE=SPAN ATTR=TXT:(1<SP>of<SP>10) EXTRACT=TXT" + "\n";
        macro +=  "TAG POS=1 TYPE=DIV ATTR=TXT:<SP>We<SP>are<SP>all<SP>out<SP>of<SP>offers.<SP>Please<SP>check<SP>back<SP>later<SP>for<SP>more. EXTRACT=TXT" + "\n";
        macro +=  "TAB T=2" + "\n";
        macro +=  "TAG POS=1 TYPE=DIV ATTR=TXT:Step<SP>5 EXTRACT=TXT" + "\n";
        macro +=  "TAG POS=1 TYPE=DIV ATTR=TXT:Step<SP>10 EXTRACT=TXT" + "\n";
        iimPlay(macro)
        if (iimGetLastExtract(1)== "(1 of 5)") {
            iimPlay('5.iim');
        } else if (iimGetLastExtract(2) == "(1 of 10)") {
            iimPlay('10.iim');
        } else if (iimGetLastExtract(4)== "Step 5" && iimGetLastExtract(5) != "Step 10") {
            iimPlay('5e.iim');
        } else if (iimGetLastExtract(4) == "Step 5" && iimGetLastExtract(5) == "Step 10") {
            iimPlay('10e.iim');
        } else if (iimGetLastExtract(3) == "We are all out of offers. Please check back later for more.") {
            iimPlay('none.iim');
        } else {
            iimPlay('single.iim');
        }

}
Last edited by effykingly on Mon Sep 29, 2014 10:05 am, edited 1 time in total.
chivracq
Posts: 8790
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: Loop an entire .js file

Post by chivracq » Mon Sep 29, 2014 9:59 am

Yep, but for Speed and Performance, you shouldn't include the 'macro' Declaration in the Loop...
- (F)CI(M) = (Full) Config Info (Missing): iMacros + Browser + OS (+ all 3 Versions + 'Free'/'PE').
- I don't even read the Qt if that (required) Info is not mentioned...!
- Script & URL help a lot for more "educated" Help...
effykingly
Posts: 15
Joined: Fri Sep 26, 2014 11:17 am

Re: Loop an entire .js file

Post by effykingly » Mon Sep 29, 2014 10:04 am

But then how else would I get it to loop 10 times? Or do you mean save the macro code as iim and call it in the js file? Like this:
I think I found the solution by enclosing the contents in:

Code: Select all

for (var i=0;i<=10;i++)
{
        iimPlay(macro);
        if (iimGetLastExtract(1)== "(1 of 5)") {
            iimPlay('5.iim');
        } else if (iimGetLastExtract(2) == "(1 of 10)") {
            iimPlay('10.iim');
        } else if (iimGetLastExtract(4)== "Step 5" && iimGetLastExtract(5) != "Step 10") {
            iimPlay('5e.iim');
        } else if (iimGetLastExtract(4) == "Step 5" && iimGetLastExtract(5) == "Step 10") {
            iimPlay('10e.iim');
        } else if (iimGetLastExtract(3) == "We are all out of offers. Please check back later for more.") {
            iimPlay('none.iim');
        } else {
            iimPlay('single.iim');
        }

}
chivracq
Posts: 8790
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: Loop an entire .js file

Post by chivracq » Mon Sep 29, 2014 10:11 am

Yep, just move the start of the 'for' Loop after the 'var macro" Declaration. You only need to declare 'var macro' once and not again and again in each Loop...
- (F)CI(M) = (Full) Config Info (Missing): iMacros + Browser + OS (+ all 3 Versions + 'Free'/'PE').
- I don't even read the Qt if that (required) Info is not mentioned...!
- Script & URL help a lot for more "educated" Help...
effykingly
Posts: 15
Joined: Fri Sep 26, 2014 11:17 am

Re: Loop an entire .js file

Post by effykingly » Mon Sep 29, 2014 10:38 am

chivracq wrote:Yep, just move the start of the 'for' Loop after the 'var macro" Declaration. You only need to declare 'var macro' once and not again and again in each Loop...
If I do that will the var macro still run 10 times? I want the entire code to run 10 times just like with the imacdo 'play loop' button for iims.
chivracq
Posts: 8790
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: Loop an entire .js file

Post by chivracq » Mon Sep 29, 2014 11:50 am

effykingly wrote:
chivracq wrote:Yep, just move the start of the 'for' Loop after the 'var macro" Declaration. You only need to declare 'var macro' once and not again and again in each Loop...
If I do that will the var macro still run 10 times? I want the entire code to run 10 times just like with the imacdo 'play loop' button for iims.
Beeehhh, yes of course, "iimPlay(macro);" is inside the Loop, that's OK, it's only bad Practice in all Programming Languages to have the Declaration of a Variable or a Function inside the Loop unless you were computing stg in the Loop that is supposed to change at each Loop, for example if you were computing a Random WAIT Time and you wanted for each Loop a different Random WAIT Time but that's not your case, the Content of 'macro' remains the same for each Loop.
But if you don't understand the Difference, don't worry, your first Solution was good enough, you only loose a few Milli-Seconds in Performance
- (F)CI(M) = (Full) Config Info (Missing): iMacros + Browser + OS (+ all 3 Versions + 'Free'/'PE').
- I don't even read the Qt if that (required) Info is not mentioned...!
- Script & URL help a lot for more "educated" Help...
effykingly
Posts: 15
Joined: Fri Sep 26, 2014 11:17 am

Re: Loop an entire .js file

Post by effykingly » Mon Sep 29, 2014 12:27 pm

chivracq wrote:
effykingly wrote:
chivracq wrote:Yep, just move the start of the 'for' Loop after the 'var macro" Declaration. You only need to declare 'var macro' once and not again and again in each Loop...
If I do that will the var macro still run 10 times? I want the entire code to run 10 times just like with the imacdo 'play loop' button for iims.
Beeehhh, yes of course, "iimPlay(macro);" is inside the Loop, that's OK, it's only bad Practice in all Programming Languages to have the Declaration of a Variable or a Function inside the Loop unless you were computing stg in the Loop that is supposed to change at each Loop, for example if you were computing a Random WAIT Time and you wanted for each Loop a different Random WAIT Time but that's not your case, the Content of 'macro' remains the same for each Loop.
But if you don't understand the Difference, don't worry, your first Solution was good enough, you only loose a few Milli-Seconds in Performance
I'm sorry I don't understand the difference but thanks for the explanation and the advice to boost my performance.
effykingly
Posts: 15
Joined: Fri Sep 26, 2014 11:17 am

Re: Loop an entire .js file

Post by effykingly » Mon Sep 29, 2014 12:29 pm

Thank you skippyto for the js loop reference and chivracq for the performance tip. My problem has been solved.
chivracq
Posts: 8790
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: Loop an entire .js file

Post by chivracq » Mon Sep 29, 2014 3:16 pm

effykingly wrote:Thank you skippyto for the js loop reference and chivracq for the performance tip. My problem has been solved.
Good...! :D
- (F)CI(M) = (Full) Config Info (Missing): iMacros + Browser + OS (+ all 3 Versions + 'Free'/'PE').
- I don't even read the Qt if that (required) Info is not mentioned...!
- Script & URL help a lot for more "educated" Help...
effykingly
Posts: 15
Joined: Fri Sep 26, 2014 11:17 am

Re: Loop an entire .js file

Post by effykingly » Mon Sep 29, 2014 4:17 pm

chivracq wrote:
effykingly wrote:Thank you skippyto for the js loop reference and chivracq for the performance tip. My problem has been solved.
Good...! :D
Any ideas on how tag elements in frames with changing frame numbers? I tried tom's example but I don't get a frame number.
chivracq
Posts: 8790
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: Loop an entire .js file

Post by chivracq » Mon Sep 29, 2014 4:31 pm

effykingly wrote:
chivracq wrote:
effykingly wrote:Thank you skippyto for the js loop reference and chivracq for the performance tip. My problem has been solved.
Good...! :D
Any ideas on how tag elements in frames with changing frame numbers? I tried tom's example but I don't get a frame number.
I guess you had disabled BB Code in your previous Post, the Link is not working...:
http://forum.imacros.net/viewtopic.php?f=11&t=23436

EDIT: Oh, now it is, I guess you were still editing your Post... I will have a look...
- (F)CI(M) = (Full) Config Info (Missing): iMacros + Browser + OS (+ all 3 Versions + 'Free'/'PE').
- I don't even read the Qt if that (required) Info is not mentioned...!
- Script & URL help a lot for more "educated" Help...
Post Reply