WebExtensions: Sending data to a Batch file? - javascript

If a browser plugin is sending messages to a .bat file, what is the correct way to send that message? I have seen the following examples:
var sending = browser.runtime.sendNativeMessage("ping_pong", "ping");
And
var port = browser.runtime.connectNative("ping_pong");
port.postMessage("ping");
In that example ping_pong is the name of the native application in the Windows registry.
Edit:
I am trying to use Native Messaging as described here:
https://developer.mozilla.org/en-US/Add-ons/WebExtensions/Native_messaging
I have created a Firefox plugin which sends a string to a batch file using the code shown above. I have also created the registy entry and JSON file for the application as described in that link. The JSON file contains a link to the batch file so that the plugin knows what application to call.
The batch file can accept a parameter using %1. However the value of %1 is the name of the JSON file and not the message that was sent to it using JavaScript.
Edit 2:
It the example given in the link above it is a Python file that handles the standard input. Thus a batch file calls a Python file to handle the messaging. I used a Java file instead. This resolved the problem and it works without problems.

Related

Cannot download pdf from file-saver.js react

I am trying to download the pdf from this url :
http://www.africau.edu/images/default/sample.pdf
I followed the example and wrote the code below.
import { saveAs } from "file-saver";
const downloadPDF = ()=>{
var FileSaver = require("file-saver");
FileSaver.saveAs(
"http://www.africau.edu/images/default/sample.pdf",
"somehthing.pdf"
);
}
However, when the downloadPDF function is invoked on the button pressed. The file is not being saved. The pdf is simply being opened in the new tab.
The screenshot of what the pdf looks like in the new tab is shown below.
How do I save the pdf file?
Also, is this approach to get the pdf even valid in the first place or is axios.get() more preferred approach to get the file, then save the response file (response.body) via FileSaver.saveAs()
If the question is unclear, please let me know in the comment before flagging - I will make the necessary update. Thank you
seems like the FileSaver does not help.
However if the file is coming from the server we recommend you to first try to use Content-Disposition attachment response header as it has more cross-browser compatiblity.
as far as I know, there are 2 ways to download file in browser.
server returns a response with header Content-Disposition with value attachment or header Content-Type with value application/octet-stream. Browser will promote the SaveDialog and handle this download for you. This is preferred way to download but this requires you to have control over the server.
you just use ajax or axios to get the data of any file at anywhere. then you create a dummy link to download (like this one). then browser will promote for SaveDialog and then save file to disk. This is just fine for small file but not for large files because you have to store entire file in memory before saving it to local disk.
I think option 2 is appropriate for you.
Example here. In this example, I place a file named abc.json in public folder. Note that the server must enable cors for your website origin. otherwise, there's no way for you to access that file in javascript code.

Javascript file input

I am trying to make a JS program using a text file on the client side. The thing is that it was difficult to find out how to do such a basic functionality because it is not a desired feature. I want to open a text file which is not in the local directory of a user but in the directory with the html file. So, what I want is the JS version of this code (which is in python)
# Open a file
fo = open("foo.txt", "wb")
fo.write( "Python is a great language.\nYeah its great!!\n");
# Close opend file
fo.close()
Thanks!
You can read a file with FileReader() (as a result of a user selecting files) but fortunately we can't modify client's or server's files with JavaScript in client side (there is no FileWriter()). To do that, you should upload the file to the server (through Ajax or a simple html form) and modify it with server side code.

Questions about extract.autodesk.io - taking a file path instead of choosing with the file chooser

I'm trying to modify the project so I could plug in a file path or a file as a variable instead of the user choosing the model file. So I'm looking for where the actual upload happens.
In submitProject():
https://github.com/cyrillef/extract.autodesk.io/blob/master/www/js/app.js#L129
I see that it just sends (with an ajax request) an object that holds the file name and unique identifier but not the actual binary file.
In here:
https://github.com/cyrillef/extract.autodesk.io/blob/master/www/js/upload-flow.js#L34
there's r.upload(), is this the actual upload of the model?
Does it start to upload the file right as you press ok in the file chooser?
Is there a way to give it a file path to upload instead of uploading with the form and file chooser?
The author of this sample should be on Christmas vacation, I just downloaded and setup the extractor sample on my machine, with a little debug into the code, let me try to answer as much as I can.
In general, I think some of your understanding is correct, but let me explain a little more:
For a local file to be uploaded and translated, there are actually 2 steps of actual “upload”.
As you mentioned, when you press ok in the file chooser, yes, the file will be first uploaded to the "extractor" server as you noticed by some methods like r.upload(), it’s actually using a JavaScript library call “flow.js", which provides multiple simultaneous, stable, fault-tolerant and resumable/restartable file uploads via the HTML5 File API. I am not expert on this, but you can check that module about how to use it to upload a file.
By now, your file is uploaded from client to the "extractor" server, but if you want to translate the file to "svf", the file is required to be uploaded to Autodesk Server(OSS), that is done by clicking “submit my project” buton, when you click this button, as you mentioned, from client, it will call the method submitProject() in https://github.com/cyrillef/extract.autodesk.io/blob/master/www/js/app.js, this method will send a post request of “/api/projects” to the "extractor" server, if you check the code at server side https://github.com/cyrillef/extract.autodesk.io/blob/master/server/projects.js , you can see the extractor server actually upload the file to Autodesk OSS, and then triggers the translation service.
This feature (passing a URL string vs a file binary) is already implemented. You can use the uri: edit box and paste your file URL there. It supports http(s) or S3 uri with access token.
The physical upload happens in this file, whereas the SubmitProject() code sends only information as JSON. The JSON object only contains a reference to the file which was uploaded using flow.js. But would contain the uri string if you had choose that method.

Python: Issue getting updating html created via JavaScript calls in browser

I am using Python to pull the HTML of a website to get satellite locations. Of course since I am not actually accessing the site via a browser I am not retrieving any html that would be populated by javascript calls.
import urllib.request
page = urllib.request.urlopen('http://n2yo.com/?s=20217')
file = open("textFile", "wb")
satelliteText = page.read()
file.write(satelliteText)
file.close()
I've explored libraries like Windmill that literally run a browser so that you can get that javascript created html, but I am using a Raspberry Pi. I'd rather not install an additional browser.
Is there anyway that I can make the ajax get calls myself that the website is making and retrieve just the data I need?
Looking at this source here: http://www.n2yo.com/js/passes.js it appears that it is calling http://www.n2yo.com/inc/all.php to get the data. By reading through passes.js carefully you should be able to figure out how to parse it.

Check actual file type in node.js

I want to prevent people from, say, uploading a movie file or shell script by just adding a jpg extension to my avatar upload endpoint. I haven't written the backend to upload script in a long time but I remember PHP being able to tell me the actual file type, not just the type based on the file extension. I can also check this purely on the front-end JS. Or in Java there's http://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#probeContentType%28java.nio.file.Path%29
The issue is in vanilla Node and using a bunch of mime related modules including mmmagic are just returning the mime type based on the file extension. For example, I removed the gif extension on a gif file and changed it to txt and both Node and mmmagic just return text/plain.
In Java, I was using Tika, which is a mime type detector tool. Turns out, there is nodejs bridge for it, node-tika.
To use
npm install node-tika
and
var tika = require('tika');
tika.type('test/data/file.pdf', function(err, contentType) {
console.log(contentType); // Logs 'application/pdf'.
});

Categories