I would like to write the output of a JavaScript WebSocket continuously to a local text file. How can I do this?
Writing to files seem impossible for JavaScript (security reasons). That's why I am searching for another solution. This doesn't need to be a browser based solution. Windows cmd is good, too!
Well, as you seem to already know, you can't write to a local text file from regular browser Javascript. Security provisions prevent that. Here are some things you could do:
Write a browser plugin and from that you could write to the local disk.
Write a local node.js Javascript app that connects to your webSocket and then writes incoming data to a text file. Then, run that app.
Write a local app in any of your other favorite languages (Java, Python, C++, C#, etc...) that connects to your webSocket and then writes incoming data to a text file. Then, run that app.
FYI, a node.js app to do this would probably be about 20 lines of code to write.
Related
I have read several posts on this site that ask similar questions but the key difference is they involve a client and a server. For my use, this is not the case. I am simply pasting a file directory on my computer into my browser in order to view a local HTML file, packed with CSS and some jQuery.
I've been looking around and the answers I've found are "No; a client can not write to a server", and "No; a server can not write to a client". But there is no answer to "can a client write to a client with JavaScript?"
Use case:
I'm building a webapp (website? JS app?) as a college project for a stock management tool that will be locally hosted and never connect to the internet. Sure, I could knock one together in python in a couple hours, but I wouldn't learn anything. I need to create an access a txt file containing an array of the current stock of all the items so that when the application is loaded, the user doesn't have to manually enter anything but the changes to stock levels.
Honestly, I'm a beginner at JS and JQ and I'm only going off of what makes sense based on a mix of HTML and Python that I know.
Maybe PHP would be the better option for this particular option, or maybe JS will work well enough.
You still won't be doing client-to-client, your browser will just act as though the local file system is the server using the file:// protocol which means the same rules about a "client" (the browser) cannot write to a "server" (your local file system) apply.
If you wan't to be able to write an application that can interface directly with the filesystem, then look into something like Electron which is essentially an augmented website that gives you APIs to interact with the actual computer the app is being run on, including filesystem stuff.
The title says it all. Is there a way to write macros in JavaScript to achieve a similar functionality to that of Autoit? I just would want to manipulate files on my own computer (offline) and could easily do it with autoit, but since I am currently learning JavaScript -- plan to develop in Node.js -- I figure it wouldn't hurt to get the extra practice.
Thanks guys!
Use an application which supports JavaScript as a shell scripting language, such as the following:
JsRoboKey
PowershellJS
PowerChakra
RemoteNodeJS
JScript + WSH
JavaScript Shell Scripting with JSC
QtScript: QScriptEngine
Part I: How to Choose a JavaScript Engine for iOS and Android Development - OpenAphid-Engine
nodejs has a module which do autoit things --
nodejs install autoit
var au = require('autoit');
au.Init();
au.Run("notepad.exe");
au.WinWait("[Class:Notepad]");
au.Send("Hello, autoit & nodejs!");
NodeJS is a very powerfull platform, it is extensible and opensource.
There is no problem to run local scripts to do everything you need using JavaScript (see standard FileSystem library docs). You can also try to look in NPM(NodeJS package manager).
Assuming you have AutoIt installed (say in folder C:\AU3) and this folder in the PATH, you can add
extension '.AU3' to the PATHEXT environment variable, and create an AutoIt script called, say,
hello.au3 with just a silly line:
MsgBox(0, "Warning", "Hello, World!")
Now, simply typing the command 'hello' will execute the script, displaying the silly message in a modal message box.
Next, create an equally silly Node.js script, say, MyWarn.js - in the same folder:
var oCP = require("child_process");
console.log("Starting...");
var oNP = oCP.execSync("hello");
console.log("Done.");
Assuming Node is also in the PATH, try this command:
node MyWarn
So ... we get the benefits of Node (for its jit), and the benefits of AutoIt (for its GUI handling.)
The problem is getting the two to communicate. Personally, I use a RamDisk to pass small files...
Javascript can't write to a file on your local machine remotely.. Its almost the same as HTML in a view model.
It can however perform some executions of other scripts via AJAX for example. But thats on server again.
It might be worth a look to read on server && client side differences.
im not 100% sure but node might offer another outlet on this but it would still be server side.. Not locally.
The Server - This party is responsible for serving pages and handling the logic | Code behind.
The Client - This party requests pages from the Server, and displays them to the user. On most cases, the client is a web browser.
The User - The user uses the Client in order to surf the web, fill in forms, watch videos online, etc.
I'm working with an API with a feature that can only be accessed (easily) using Javascript, but I want to use the API to save a .txt file to my server. Is there any way to achieve this on a Mac OSX machine? I know that JS running in a browser is prohibited from doing this, so I guess this is really a two part question: (1) what's the simplest way to run a 10 line JS script on Mac OSX and (2) how would I write data to a txt file doing this?
You could go for a headless browser for example PhantomJS. I haven't used it, but it should run JS well.
The same restrictions apply though, no filesystem access from JS.
Unless you use something like the plugin framework in FF, in that case you have elevated rights for the scripts.
Besides that you could create a wrapper in php, perl or other language, and pass on the data from JS to them in an ajax call, and they write the txt for you.
Is it possible with Javascript or jQuery to convert mp3, wav, etc. to m4r format?
Let's assume you had a library that can change the format of files.
Let's also assume you only need the application to work on current browsers that implement FileAPI or FileReference so you can have access to uploaded files (you can't have access to them without FileAPI or FileReference unless you use Flash or Java Applets or equivalent technologies).
You wouldn't be able to write the output file back to the user because JavaScript is not allowed to access the local filesystem.
Your only solution would become sending the converted file to the server and the server sending it back to you with a force download directive so that the user will be prompted to download the results.
Now back to if there were a library that can the conversion (or even native JavaScript)... I haven't heard of any. It's not impossible to build one but it is impractical and wouldn't run very fast.
Edit:
Let's not forget Node.js!
It's a backend server that uses Google Chrome's V8 JavaScript interpretor/compiler. And it runs JavaScript as a backend scripting engine.
You have access to filesystem, databases and everything if you use that (or any other backend system for that matter) and still be using JavaScript. You can use libraries too. Either written in JavaScript or libraries written in other languages that have been linked to interface with Node.js.
Edit 2:
There is a PC emulator written entirely in JavaScript. It runs binary executables if you want it to. It's called JSLinux.
If you're feeling particularly rambunctious you can grab the ffmpeg binary executable (compiled with static linking). And embed it into your application code as an uuencoded string then use JSLinux to execute the commands and grab the results.
Indeed, it is possible doing this on the client using the latest js technologies. A web-worker thread can do the work in the background. At least in Firefox and Chrome it is also possible to read ("upload in memory") and write ("download from memory") files using the new W3C File API, see here.
I managed to read files via drag&drop from and within the client using google's GWT which in the end is plain javascript, so it must also be possible to do it "natively".
Besides that, the conversation algorithm of course has to be implemented in a javascript web worker to avoid blocking the gui. This should be the hardest part, but not impossible, though.
You would need a backend to do this. You may want to look into the PHPExtension of FFmpeg
There are a couple of aspects of node.js I don't quite understand. I hope someone can make things clearer
When you install node.js where do you store your files so that the web browser can display your content? For example Apache have a www folder.
Does node.js replace client side javascript?
How does node.js interact with HTML? For example if I wanted to put data from the server into this div element <div id="content"></div>
In PHP you could do something like this: <div id="content"><?php echo $content; ?></div>
Would you ever call node.js from client side? For example: An Ajax request to node.js to get data.
Whats confusing me is that because it is run from the server then I expect that I can use javascript on the browser to get data from the node.js server. However, examples I seen this is never done.
Thanks in advance
When you install node.js where do you store your files so that the web browser can display your content? For example Apache have a www folder.
Wherever you want. node.js doesn't serve static content, it runs JavaScript. You tell it which script to run when you start it.
You could write some JavaScript that serves static content, but where you would keep it depends on the code you wrote.
Does node.js replace client side javascript?
Only in so far as any server side programming replaces client side JavaScript.
One advantage of using JS on the server side is that you can reuse libraries on both the client and the server. See Mojito for a framework that claims to focus on this (I haven't had time to try it myself yet).
For example if I wanted to put data from the server into this div element <div id="content"></div> In PHP you could do something like this: <div id="content"><?php echo $content; ?></div>
PHP is a template language with an embedded programming language. JavaScript is a programming language. Typically you would use a template language (e.g. moustache) from your JS.
Would you ever call node.js from client side? For example: An Ajax request to node.js to get data.
Yes, if you want to. Just like any other server side programming environment. (Assuming you are using node to run an HTTP server).
Node.js is not a server (like e.g. Apache). It's a platform to run Javascript with some built in libraries (so-called modules). It is very easy to write server (HTTP or any other) in Node.js, but you can also write completely different programs (no network related, meant to be executed locally).
I suggest you read this: http://www.nodebeginner.org/. It took me several hours but allowed me to understand basics of Node without much pain.
As for client side scripting, it's generally separate. Code in Node runs in separate environment then the one in browser. They can communicate, but you have to explicitly make them to. It's not much different from server side coding in PHP. The code on server produces some output (eg. HTML) which is sent to client. If there are scripts in output, client (browser) executes it. They can communicate (via XHR, websockets, etc.), but by itself those scripts are separate.
How does node.js interact with HTML? For example if I wanted to put
data from the server into this div element In
PHP you could do something like this:
You would probably send the content as JSON to the JS-Client and insert it into the DOM (using plain JS or JQuery).
I've wrote a REALLY trivial (and not quite feature-rich :-P) chat application in Node.js a while a go, to try out some concepts and understand working with JS on both the client and server. Maybe it will give you some clues.
EDIT
In this application, the server also serves static files, which you should not do when implementing production ready application (Node is not really suited for serving static files!).