I'm wondering.. how could I output a string to a file (and create said file) in javascript?
I'm using this for Chrome extension development.
You can't create, read, or modify files on the computer using browser based client side Javascript for security reasons.
If you want to do this from within a browser, it won't work except you might use activex or java (via plugins).
Or you fetch yourself the spidermonkey (mozilla's javascript engine) source code and compile it into a command line application. You will get file access this way.
Cheers,
-S
You post the info to a http handler (ashx in asp.net, or just a php site), which then writes the info to a file.
Related
I am looking to do some web-scraping, without going through help desk and IT to install and configure Python (I don't have admin rights because I'm an intern).
I have already written the logging functions I need in JavaScript, but I need to extract the data out of the program into a CSV so I can convert to .XLS afterward.
I'm wondering if it's possible for JavaScript to do these things:
Can JavaScript write to a file?
Can I run external scripts with a click of a button somehow? i.e. without pasting the code into the console every single page. Or even, perhaps, run external scripts automatically upon page-load?
Can I automatically iterate through and load URLs? The URL details all remain the same, with only an integer value that changes from page to page.
Thanks in advance for any input!!
1) Yes, you can use javascript to write to file using node.js Use the fs module like so.
const fs = require('fs');
fs.writeFile('file.txt', data_to_write[, options], callback)
Refer : https://nodejs.org/api/fs.html#fs_fs_writefile_file_data_options_callback
2) Yes, you can use puppetter to run Headless Chrome scripts
3) Go through the puppeteer documentation and you can find how to load URLs on the browser. Iterate the links and store them in a string and open the page. Then use page.evaluate() to run you code and scrape the contents.
Yes, you can do all those things with JavaScript. No, you can't do all those things with JavaScript purely in the browser because of the Same Origin Policy.
Two things that let you do this (offhand):
Node.js, which you can download and expand from a zip (doesn't require an installation step). Whether you can do that on your workstation depends on how locked down it is. There are lots of modules available for Node.js for handling the heavy-lifting of web scraping.
A Java JVM (via its scripting support, though JavaScript scripting hosts for the JVM trail behind in terms of up-to-date JavaScript features). If one isn't already installed, again, you may be able to install one without admin rights, or not, depending.
You can definitely do it with node.js from server-side.
But you'll face the cross-origin problem doing it from HTML page in browser.
So for browser you'll have to make a browser plugin (aka add-on aka extension).
I have a html file that when run in a browser such as Chrome and that contain javascript instructions, it sends the "emit" message to my websockets server and displays the value on that page.
Is there a way to call this same html file from a bash script as I'm wanting to insert data into a MySQL database which will ultimately call that html file to send an update to the websocket.
Hopefully that makes sense but hopefully there is a way to do it too :)
If you are only focused on rendering the HTML page (and not interacting with it via buttons or something) you may find this link helpful: Running HTML from Command Line
If you try to execute javascript instruction in your server without using a browser, I recommand you to use Node.JS with a real js script without html.
Otherwise you can try to run an html file with js instruction inside using something like phantomjs but I think is less performant than using Node correctly.
EDIT
It is Javascript yes, but I need to "Import" the socket.io.js file into the same script I have created and my browser I'm having to use doesn't support the new Javascript import methods. So I'm writing as HTML which calls the Javascript, otherwise I would use nodejs
I think you can import the socket.io lib in node application using npm.
https://www.npmjs.com/package/socket.io
The documentation says that you can use a client inside a node script too :
Socket.IO enables real-time bidirectional event-based communication. It consists in:
a Node.js server (this repository)
a Javascript client library for the browser (or a Node.js client)
Is there a way to replace a .js file in the website sources with a file on my workstation, or make a modification to a .js file and refresh the website to see the changes?
I am developing client-side JavaScript code against a SharePoint website on a server. I cannot create a local version of the website, so I need to modify the script, save the file to the server, refresh, etc. I do not have direct access to the server, and saving a file in a SP doc library or web part takes a lot of time between edits.
I can make small modifications using the dev tools while breaking on certain lines and applying snippets, but I am hoping for a better way.
Thanks!
If you have access the server that's hosting the file you should be able to replace or modify the JS file. Alternately you can use local hosting tools to test your file and then upload it to the server once you've confirmed it's working.
If you explain what you level of access to your host is we can offer better suggestions.
I'm working on a webpage that has to work offline when it's finished. No XAMPP or other services available, so PHP is no option.
It has to create a new file, edit a filename (which would be the best solution) or move a file into another directory. This action has to be called from a function in jQuery.
I've read that Python could to this but as long as I read Python needs a web framework and a configured Apache as well, so if it's right Python is no possibility for me as well.
Is there any way to do this with JavaScript / jQuery or anything else which works offline?
No. It won't work by Design. Imagine, you visit a Website and it would install virus.exe into your Windows Directory (For example Autostart).
Html Pages are for Displaying Informationen to the User. Javascript is supporting it with dynamic Features. The Browser grant Rights for Loading other Information and nothing else. Everything which would affect the System, could not handled by Script.
If it should run from Web, you need a Plugin (Like Java). In your case (Offline Use) consider to write a Desktop Client (C#, VB.NET, Java, C++).
I want to list the names of HTML files in a particular folder using JavaScript (in the browser)...
Can anybody help me in this?
Thank You
If you're using Javascript that's running in the browser, there's no way to "open a folder". You have to obtain data about the folder contents through some data structure or by doing something like parsing a server generated folder index in HTML.
If you're using Javascript that's running on a non-browser engine (such as jscript, rhino, etc.) then you have to be more specific in the question as the answer will obviously depend on whether the engine where your script is running provides objects to access the filesystem or not.
This is what you are asking for:
http://www.irt.org/articles/js014/index.htm
Also read on FSO
But yes, if you can't use FSO if ActiveX is disabled on client system.
Javascript can't interact with the OS while running inside a browser, due to security concerns.
It can get a list from the server and display that to the client on the browser, but, to get a directory structure from the client computer to the browser would require some other application reading the directory structure and sending it to the server, and the server sending it to the browser.
Now, if you are using Actionscript it is possible to read the hard drive, and I expect that Silverlight would allow you to do this also, but I don't know.