iMacros - create batch file to start javascript file in existing browser window - javascript

I'm trying to create a batch file to start a .js file (tom.js) on imacros (firefox browser) at a specific time.
The most simple solution seams to be:
C:\Program Files (x86)\Mozilla Firefox\> start firefox.exe imacros://run/?m=TOM.js
Then I will use windows task scheduler to run the .bat file at a specific time..
Problem: This line C:\Program Files (x86)\Mozilla Firefox\
start firefox.exe actually opens a new window of firefox, then runs the tom.js file.
I would like the .bat file to run tom.js file on the ALREADY OPEN firefox browser instead to open a new window of firefox each time. The reason is that i have a few sites in a few tabs already opened and logged in, it would be much more simple if the command to run the .js file does that in one of the already opened tabs (tab 1 ideally).
Any suggestion on how to achieve this?

You can just bypass the '.bat' approach in the following way:
var startTime = "2017-01-24T11:55:00";
var secWait = parseInt((new Date(startTime) - new Date()) / 1000);
secWait = Math.max(0, secWait);
iimPlayCode("WAIT SECONDS=" + secWait);
window.location.href = "imacros://run/?m=TOM.js";
In addition, it's possible to make this code as 'iim'-macro. (Sometimes more preferable.)

Related

Running an .exe file with a single process .ini file Node.JS

There was a rather interesting question for me, I need to open the .exe file in Node.JS along with its .ini configuration file. There is an already created shortcut that, when you click the mouse, opens the application as it should, you must do the same through Node.JS
Example: object line in a shortcut (which I mentioned above): D:\Downloads\gobot\bot.exe D:\Downloads\bots\accs\data\Alex_Shivol#192.168.1.1.ini
Here are some examples I tried:
BotsProcess[0] = exec('D:\\Downloads\\bots\\start.exe D:\\Downloads\\bots\\accs\\data\\Wiley_Beard#46.174.50.52;7800.ini', {shell : true});
spawn('D:\\Downloads\\bots\\start.exe', ['D:\Downloads\bots\accs\data\Alex_Shivol#192.168.1.1.ini'], {shell : true});
spawn('D:\\Downloads\\runBots\\' + files[j], {shell : true});
In the last example, I tried to open a shortcut in which all this is spelled out, but the problem is that when opening 2+ shortcuts, as many programs open as you need, but with the same .ini

How to automatically close webpage using javascript?

I have found many questions like mine in different forums, but I couldn't find an answer that actually helps to solve my problem in any of them.
Basically, what I want is to open an URL through command prompt, it will open the browser, processes a webpage, and then I would like it to automatically close.
Why do I need this?
I have an application that runs on IIS. There are some routines I need to run everyday in my application. I can simply kick of these routines by running an URL similar to the showed below:
http://myapplication.com/DoStuff.aspx?
The Problem is that this is totally manual.
I was wondering if I could create a batch file calling my URL "start http://myapplication.com/DoStuff.aspx?", and then I could create a task on Windows to run that batch file everyday. That works for me except that the browser will not close automatically.
What I mean is, I could try it, but at the end of a week, I would have at least 5 windows opened.
What I have tried:
I have tried to solve it by using javascript, but I always end up getting this message:
scripts may close only the windows that were opened by it
It does not matter the javascript function I create using "window.close()", the windows won't close.
Chrome and Firefox returns that message.
IE let's me try to close the window, but it asks in a popUp if I really want to close it.
What you need is probably a headless browser like PhantomJS (WebKit browser without GUI). I would recommend you to use CasperJS to create scripts even more easily...
Install Phantom and Casper globally on your system and write a minimal automation script like so:
var casper = require('casper').create();
casper.start('http://myapplication.com/DoStuff.aspx');
casper.then(function() {
// Do something here...
});
casper.run();
Set a cron job (or Windows equivalent) to execute the script with the casperjs bin. Normally, it should do the trick...
Using batch files only because I strongly believe you do not need JavaScript to do this.
::start a new browser session at the given url
start iexplore "http://www.google.com"
::wait for whatever process to end if you actually have to wait
timeout 15
::kill the browser process
taskkill /im iexplore.exe /f /t
If it runs on a machine nobody interacts with, and you know the session you log into will be the last session you got out of, then you know the session you're getting into already has IE open. So you could reverse the order of the script and not care much about timing
::kill the browser already opened
taskkill /im iexplore.exe /f /t
::open a new browser session at the given url
start iexplore "http://www.google.com"
::if you have to wait, but don't know how long... leave the browser window open. We'll close it next time we run this batch file anyway.
At *nix you can create a bash script containing
#!/bin/bash
/usr/bin/chromium-browser --user-data-dir="/home/USER/.config/chromium-no-flags" "http://myapplication.com/DoStuff.aspx"
within DoStuff.aspx, use setTimeout() or other function to call window.close() when task is complete
setTimeout(close, 10000)
or
new Promise((resolve, reject) => {
// do asynchronous stuff
resolve(/* value */)
})
.then(close)
.catch(close)
For *indows equivalent of cron see
What is the Windows version of cron?
Windows equivalent to cron?
See also
Close google chrome open in app mode via command line
Open Chrome from command line and wait till it's closed
Close programs from the command line (Windows)
Killing all instances of Chrome on the command-line?

FIle download via cmd in windows using HTML

I trying to download certain data from a site from cmd of windows. When I am downloading via this method, it is generating a firefox window and a pop up which I do not want. I should be downloaded to desired folder in background.
I have created an index.HTML file in a directory.
<!DOCTYPE html>
<html>
<script>
window.location.href = 'http://nomads.ncep.noaa.gov/cgi-bin/filter_gfs_0p50.pl?file=gfs.t06z.pgrb2full.0p50.f000&lev_10_m_above_ground=on&var_UGRD=on&var_VGRD=on&subregion=&leftlon=80.6&rightlon=85.4&toplat=25.6&bottomlat=20.1&dir=%2Fgfs.2015060606' ;
</script>
</html>
At cmd I am doing this "start index.HTML"
It is generating a firefox window and save popup which is asking for save permission. Is there any way I can do that more smoothly in the background.So that the window doesn't come.
If you are using Windows and Internet Explorer, you can write a VBScript.
It can be executed from the command line or just be embed in HTML documents.
You can search stack exchange for some useful VBScripts expamples.
Why do you not use PowerShell? I don't have access to a Windows machine at the moment, but this should work.
$localFile = "C:\Users\<Username>\Downloads\index.html"
$webClient = New-Object System.Net.WebClient
$urlAddress = "http://example.com/index.html"
$webclient.DownloadFile($url,$localFile)
This will download the data that stored at $urlAddress and store it in $localFile.

How to Execute a Javascript in Internet Explorer from Command Line?

Recently at my job we have been wanting to automate our deployment process with new machines. I figured the best way to do this was by launching IE (since it's the default browser that's installed via Litetouch) via command line, which the generic code for it is as follows:
iexplore.exe http://reg.cmich.edu
Which works as expected; however the next part of the automation process is to pass IE at least a javascript file that can select the building I want to register a new NIC device at, then on the next pass I would pass it my administrative credentials.
For the most part, I can figure out by scrounging the net how to get Javascript to execute certain things in a browser like clicking links and entering text into text boxes. I'm just wondering if there's any way during running my batch script that I can pass IE a .js file to execute after the browser opens.
Any help would be greatly appreciated!
You can bypass IE completely by using cscript.exe to run JavaScript.
> cscript.exe "path/to/file.js"
cscript.exe is in c:\windows\system32.
Keep in mind that globals like window and document will not be available because you have no head.
Here you go enable by command
1) Go to Start -> Run
2) Type : CMD (then enter)
3) You will get command prompt window on screen.
4) Type below command and ten enter :
regsvr32 jscript.dll
5) Now check Internet Explorer. You will have javascript enabled.
Hope this would help you to solve your problem.

How run a windows console for JavaScript

I would like to have a console window (a command line) on Windows 7 which will allow me to play with JavaScript just like a python console.
Update:
It's important to have a file access from within the console (or script run through it).
You can use Node.js's REPL. To do so follow this steps:
Download and Install Node.js.
Call Node.js from the Start Menu / Start Screen or directly node.exe installation path (e.g C:\Program Files\nodejs\node.exe).
Enjoy!
You may want to add the installation path to your PATH enviroment variable for ease of use.
Note: to leave node.js press Ctrl + C twice.
To access the local files, you will need the File System module. This is an example of usage:
var fs = require("fs");
fs.readFile(
"C:\\test.txt",
function(err, data)
{
if (!err)
console.log(data.toString());
}
);
This will output the contents of the file C:\test.txt to the console.
Note: An unhandled exception will cause node.js to "crash".
You can just use the developer tools.
For example, in Chrome, press F12. This will bring up the developer tools. The last option on the menubar is console. This will allow you to create JS variables and functions and to interact with DOM elements on the current page
It's possible thanks to Mozilla Rhino JavaScript Engine.
To create a console window for JS:
1) Download Mozilla Rhino JavaScript Engine binary.
2) Extract: js.jar.
3) Create a script to run the console window (e.g. rihno_console.bat):
java -cp js.jar org.mozilla.javascript.tools.shell.Main
For more information about usage (for instance, and global functions inside this console) visit the Rhino Shell web page.
Just like I informed another user with the same question as yours who was faced with the same need, check out DeskJS (https://deskjs.wordpress.com). It's a portable Windows console application that lets you run pure JavaScript code and even load any existing JS files. It supports even the basic JS popup boxes implemented in browsers. You can save your commands as JS files that can be run on startup or by dragging-and-dropping them on the app. Plus there's so much more to it like you can create a build system for Sublime Text that can run JS files via cmd, it supports themes for customizing the entire console and snippets which let you save short snippets of JavaScript code for later use. Improvements are still being made on the app together with other native APIs being included. Hope this helps you as it did for me.

Categories

Resources