FIle download via cmd in windows using HTML - javascript

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.

Related

Executing a batch file via JavaScript [duplicate]

Is it possible to run bat/executable file using html5 button event? In IE its achievable using Shell object if I am not wrong.
No, that would be a huge security breach. Imagine if someone could run
format c:
whenever you visted their website.
Here's what I did. I wanted a HTML page setup on our network so I wouldn't have to navigate to various folders to install or upgrade our apps. So what I did was setup a .bat file on our "shared" drive that everyone has access to, in that .bat file I had this code:
start /d "\\server\Software\" setup.exe
The HTML code was:
<input type="button" value="Launch Installer" onclick="window.open('file:///S:Test/Test.bat')" />
(make sure your slashes are correct, I had them the other way and it didn't work)
I preferred to launch the EXE directly but that wasn't possible, but the .bat file allowed me around that. Wish it worked in FF or Chrome, but only IE.
It is possible when the page itself is opened via a file:/// path.
<button onclick="window.open('file:///C:/Windows/notepad.exe')">
Launch notepad
</button>
However, the moment you put it on a webserver (even if you access it via http://localhost/), you will get an error:
Error: Access to 'file:///C:/Windows/notepad.exe' from script denied
You can do it on Internet explorer with OCX component and on chrome browser using a chrome extension
chrome document
in any case need additional settings on the client system!
Important part of chrome extension source:
var port = chrome.runtime.connectNative("your.app.id");
port.onMessage.addListener(onNativeMessage);
port.onDisconnect.addListener(onDisconnected);
port.postMessage("send some data to STDIO");
permission file:
{
"name": "your.app.id",
"description": "Name of your extension",
"path": "myapp.exe",
"type": "stdio",
"allowed_origins": [
"chrome-extension://IDOFYOUREXTENSION_lokldaeplkmh/"
]
}
and windows registry settings:
HKEY_CURRENT_USER\Software\Google\Chrome\NativeMessagingHosts\your.app.id
REG_EXPAND_SZ : c:\permissionsettings.json
You can not run/execute an .exe file that is in the users local machine or through a site. The user must first download the exe file and then run the executable file. So there is no possible way
The following code works only when the EXE is Present in the User's Machine.
<a href = "C:\folder_name\program.exe">

How do I read a JSON file using HTML?

Here is my code:
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js">
</script>
<script>
$(function() {
var thing = [];
var bar = $.getJSON('C:\Users\cccompro\foo.json', function(obj) {
for (i = 0; i < obj.length; i++) {
thing.push(obj[i]);
}
});
});
</script>
I'm not sure why it doesn't work. "foo.json" contains an array of objects.
If you are trying the code at Question at Chrome or Chromium browsers, launch the browser instance with --allow-file-access-from-files flag set. Note that open instances of Chrome or Chromium should be closed when you launch the browser or the instance will be launched with the open browser instances' configuration folder, instead of with the flag set. You can launch Chrome or Chromium with an existing instance open and honoring the flag by using --user-data-dir flag with value set a different directory than open instance of Chrome or Chromium.
Technically, it is also possible to write to user file system without using an extension with window.webkitRequestFileSystem. Though using chrome.fileSystem within an extension provides an API designed to achieve the read/write.
See
Jquery load() only working in firefox?
Read local XML with JS
How to Write in file (user directory) using JavaScript?
How to use webkitRequestFileSystem at file: protocol
JavaScript/Ajax Write to File
Using <input type="file"> element
How to print all the txt files inside a folder using java script
You cannot read files directly from the users hard drive without the browsers permission. This would be a huge security issue if you could even though there are ways to allow this (checkout guests answer).
You could however try to make the user select the file and then read it with Javascript.
This is called the HTML 5 file API.
However, this doesn't work for any browser and you probably have to use a server anyway in this case.
For more information on this checkout this or this post.

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

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.)

JS Bookmarklet to open PDF-Files in External Viewer?

I have a program (Mendeley) that tries to open PDF files that are stored locally by accessing the URL: http://path.to/file.pdf (instead of file://path.to/file.pdf). This results in an error in my browser (not unexpected).
What I would like to do is have a javascript bookmarklet that uses the URL in the address bar (which is basically correct besides the http part) and opens the file in an external viewer (Acrobat in my case). My latest creation (inspired by other examples on SO has been):
javascript:var a = (location.pathname);
var b = ('file://schbs02'+a);
window.location = (b);`
This does NOT work; in Chrome (latest version) nothing happens. Is it an inherent limitation of bookmarklets (for security purposes) that they can not open local files or is there something wrong with the code?

Google Chrome Won't Accept .contentDocument or .contentWindow

When I try my code out:
Testing.html -
<script language="JavaScript" type="text/javascript">
function find() {
var iframeEl = document.getElementById('love');
if ( iframeEl.contentDocument ) {
var form = iframeEl.contentDocument.document.getElementById('hi').getAttribute('href');
alert(form)
} else if ( iframeEl.contentWindow ) {
var form = iframeEl.contentWindow.document.getElementById('hi').getAttribute('href');
alert(form)
}
}
</script>
<body onload="find()">
<iframe name="lovez" src="frame.html" id="love">Testingz</iframe>
</body>
Frame.html -
Testing
It will not return an alert box. However on Internet Explorer it will. I have been searching the internet, trying all examples and can't find a simple example that will work in Google Chrome. Am I doing something wrong or is it just Google Chrome?
I've noticed that both the contentDocument and getSVGDocument (for svg objects from svg files) will work fine if the code is from a web server, but when I copy the code to a local file, it will not work.
Here's a sample link with a code I coppied from which works on the web but not from my disk.
Here's the solution I just found from Chrome (thanks to Artem S), i.e. using the --allow-file-access-from-files flag.
Try it without the .document
var form = iframeEl.contentDocument.getElementById('hi').getAttribute('href');
instead of
var form = iframeEl.contentDocument.document.getElementById('hi').getAttribute('href');
The answer from #IsraelGav is correct in the sense that this problem occurs when the code is accessed from a local file but not when accessed from a web server. It is also correct in the sense that using the --allow-file-access-from-files flag can allow the local file to be accessed by Chrome.
However, it misses an important security concern here. Both the concern, as well as an alternative possible solution, were originally described in this other SO answer by #orszaczky. To summarize the alternative solution: On Windows, install http-server (npm install -g http-server) and run http-server from your project directory. On Mac/Linux, run python -m SimpleHttpServer from your local directory. You can now access the locally hosted web site in your browser. On Windows I had to use localhost:8080 while on the Mac I had to use localhost:8000.
By the way, this is not only an issue for Chrome (v49.0) but also for Opera (v35.0), on both Windows and Mac.

Categories

Resources