Close browser after done looping in js

Discussions and Tech Support related to the iMacros Firefox Add-on, including the built-in Javascript scripting interface for playing .js files.

Moderators: Community Moderators, iMacros Moderators

Forum rules
Before asking a question or reporting an issue:
1. Please review the list of FAQ's.
2. Use the Google search box (at the top of each forum page) to see if a similar problem or question has already been addressed. This will search the entire contents of the forums as well as the iMacros Wiki.
3. We can respond much faster to your posts if you include the following information:

CLICK HERE FOR IMPORTANT INFORMATION TO INCLUDE IN YOUR POST

Answering your own posts (e.g. attempting to "bump" your topic) drops your topic from the list of unanswered threads, so it may actually receive less views.

Close browser after done looping in js

by justaguyhere on Fri Nov 03, 2017 9:39 am

I'm using Pale Moon 27.5.1, Imacros 8.9.7 for firefox, and Windows Server 2012.

I currently use a batch file, which goes to a .js and opens my macro to loop 10 times. I want to automatically close palemoon once the macro is done looping so next time it runs it will be able to launch the browser again.

This is my .js code:
Code: Select all
var i;
for (i = 1; i < 10; i++) {
     iimSet("loop", i);
     iimPlay("MyMacro");
}


Is there a code I can add here to close the pale moon browser opened by the batch file, after the loops are complete?

EDIT: After researching, it appears the best way may be to close it through the batch file. I have experimented with TIMEOUT /T 30 and in the command line it shows the countdown and closes the browser. When I put 2000 instead, it does not show in the cmd box, and there is no countdown or shutdown. I am doing 10 loops and each can take anywhere from 1 minute to 5 minutes if the server is clogged up.

The timeout counter doesnt start under I manually close the browser
Code: Select all
start /wait firefox.exe imacros://run/?m="UsersDLoop.js"
TIMEOUT /T 2000 /NOBREAK
Taskkill /IM "firefox.exe" /F
Exit
Last edited by justaguyhere on Fri Nov 03, 2017 11:28 am, edited 1 time in total.
justaguyhere
 
Posts: 6
Joined: Fri Nov 03, 2017 9:17 am

Re: Close browser after done looping in js

by chivracq on Fri Nov 03, 2017 11:20 am

justaguyhere wrote:I'm using
Code: Select all
Pale Moon 27.5.1, Imacros 8.9.7 for firefox, and Windows Server 2012.


I currently use a batch file, which goes to a .js and opens my macro to loop 10 times. I want to automatically close palemoon once the macro is done looping so next time it runs it will be able to launch the browser again.

This is my .js code:
Code: Select all
var i;
for (i = 1; i < 10; i++) {
     iimSet("loop", i);
     iimPlay("MyMacro");
}


Is there a code I can add here to close the pale moon browser opened by the batch file, after the loops are complete?

Yep, you can either close the Browser from your Batch Fiel with a 'kill' or from your Script itself, search my Posts on "Browser + Close" as Search Keywords I would think, I've posted once a Method to do so (Conditionally, but that won't even be needed for you, I guess...) from a Script (in pure '.iim', but easy to adapt for '.js'), "my" Method was actually to close one Tab, but closing the last Tab would close the Browser, and I think that in the same Thread other Advanced Users posted their "best" way to close the Browser and/or I must have linked to other relevant Threads...
- (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...
chivracq
 
Posts: 6485
Joined: Sat Apr 13, 2013 6:07 am
Location: Amsterdam (NL)

Re: Close browser after done looping in js

by chivracq on Fri Nov 03, 2017 11:32 am

OK, I'm being nice, here is the Thread I was referring to, and indeed, my last Post in the Thread contains Links to several (and probably all) relevant Threads...:
- Re: How to close the iMacros Browser?
(Option 4. for you if you want to go quick to the Solution... :wink: )
- (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...
chivracq
 
Posts: 6485
Joined: Sat Apr 13, 2013 6:07 am
Location: Amsterdam (NL)

Re: Close browser after done looping in js

by justaguyhere on Fri Nov 03, 2017 11:46 am

chivracq wrote:OK, I'm being nice, here is the Thread I was referring to, and indeed, my last Post in the Thread contains Links to several (and probably all) relevant Threads...:
- Re: How to close the iMacros Browser?
(Option 4. for you if you want to go quick to the Solution... :wink: )



Thank you very much. I was just reading through your post search results :)

Code: Select all
URL GOTO=javascript:this.focus();self.opener=this;self.close()


I see that you had this solution work. I am going to play around with this and modifying my JS to load this at the right time. I will let you know how it goes.
justaguyhere
 
Posts: 6
Joined: Fri Nov 03, 2017 9:17 am

Re: Close browser after done looping in js

by chivracq on Fri Nov 03, 2017 12:17 pm

justaguyhere wrote:Thank you very much. I was just reading through your post search results :)

Code: Select all
URL GOTO=javascript:this.focus();self.opener=this;self.close()


I see that you had this solution work. I am going to play around with this and modifying my JS to load this at the right time. I will let you know how it goes.

Yep, see what Solution you choose in the end, I am no JS Guru myself, a few other Advanced Users (and certainly more advanced than me on '.js') were "pushing" for some other Solutions, check them all I would say and choose the one that works best for you..., and nice indeed afterwards if you can post your "Results" and your final Script, for other Users with a similar Case/Qt...
- (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...
chivracq
 
Posts: 6485
Joined: Sat Apr 13, 2013 6:07 am
Location: Amsterdam (NL)

Re: Close browser after done looping in js

by justaguyhere on Fri Nov 03, 2017 12:32 pm

I decided to test on Tor Browser first since its more complicated and an easy way to have 2 IPs/imacros running at once. Your option 4 did not work in Tor, but a CTRL+W keypress closed the window successfully, running an .iim just for that after the loop ended. I then use a taskkill firefox.exe (when using TOR) at the BEGINNING of my batch file, so I can be sure it is closed when the batch file is scheduled next. I will test your code on Pale Moon after work and let you know if I make more progress.
justaguyhere
 
Posts: 6
Joined: Fri Nov 03, 2017 9:17 am

Re: Close browser after done looping in js

by justaguyhere on Fri Nov 03, 2017 2:22 pm

chivracq wrote:
justaguyhere wrote:Thank you very much. I was just reading through your post search results :)

Code: Select all
URL GOTO=javascript:this.focus();self.opener=this;self.close()


I see that you had this solution work. I am going to play around with this and modifying my JS to load this at the right time. I will let you know how it goes.

Yep, see what Solution you choose in the end, I am no JS Guru myself, a few other Advanced Users (and certainly more advanced than me on '.js') were "pushing" for some other Solutions, check them all I would say and choose the one that works best for you..., and nice indeed afterwards if you can post your "Results" and your final Script, for other Users with a similar Case/Qt...



I ended up using this at my batch:
Code: Select all
taskkill /F /IM palemoon.exe
cd c:\Program Files (x86)\Pale Moon
start /wait palemoon.exe -P "Name" -no-remote imacros://run/?m="Name.js"


This is my .js
Code: Select all
var i;
for (i = 1; i < 10; i++) {
     iimSet("loop", i);
     iimPlay("Name");
} iimPlay("CloseFF.iim")


and this is CloseFF
Code: Select all
EVENT TYPE=KEYPRESS CHAR="w" MODIFIERS="ctrl"



The code works in Palemoon, in TOR it works to close the window but causes a crash or TOR about 60 seconds later, as I've read about in other posts. It works for me, but is not ideal obviously.

It seems like a simple way would be to replace the iimPlay("CloseFF.iim") with a batch file that kills the task but not sure if thats possible.
justaguyhere
 
Posts: 6
Joined: Fri Nov 03, 2017 9:17 am

Re: Close browser after done looping in js

by justaguyhere on Fri Nov 03, 2017 2:31 pm

Interesting, I found
Code: Select all
var file = Components.classes["@mozilla.org/file/local;1"]
                     .createInstance(Components.interfaces.nsILocalFile);
file.initWithPath("C:\\Users\\Administrator\\Desktop\\test.bat");
file.launch();


which seems to be a way to launch the bat from my JS. you participated in that thread. I am not familiar with JS at all, this thread is my first .js file ever.
I need to edit the path obviously, then make sure the mozilla.org part works with palemoon and firefox (tor browser), correct? Any input would be appreciated.

I also need to make sure it runs at the right time, replacing the CloseFF line with this code should do that since it worked for the iim.

EDIT: Running code in Pale Moon did indeed close browser, but did so before running macro. Back to where I was before
EDIT2: Code below works in Pale Moon! I just had to move the } from after Name"); to the end of the code. Thanks!
EDIT3: Same code works in TorBrowser also with close2.bat and firefox.exe instead of palemoon.exe
Code: Select all
var i;
for (i = 1; i < 10; i++) {
     iimSet("loop", i);
     iimPlay("Name");
}
var file = Components.classes["@mozilla.org/file/local;1"]
                     .createInstance(Components.interfaces.nsILocalFile);
file.initWithPath("C:\\close.bat");
file.launch();
justaguyhere
 
Posts: 6
Joined: Fri Nov 03, 2017 9:17 am

Re: Close browser after done looping in js

by justaguyhere on Fri Nov 03, 2017 7:21 pm

Well I am in shock here.. while the code worked perfectly this afternoon, it now closes the browser without looping. I don't think I've changed anything. This is extremely frustrating.

EDIT: AH I am spending too much time looking at code. Too many or not enough { } in the js
justaguyhere
 
Posts: 6
Joined: Fri Nov 03, 2017 9:17 am


Return to iMacros for Firefox

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], jinjinda and 13 guests

cron
-->