Tampermonkey: access local files and commands

Hello,

I havs a Bash-script, that extracts and greps for some things in some protocolfiles I download.

Now I would like to write a Tampermonkey-Script, that does the same and have some questions:

Is it generally possible to access local files from a Tampermonkey-Script and execute the Script as soon as the download of a *.dat-file finishes?
I need the Script to extract the *.dat-file and grep for some things.

Are there equivalents of grep and other linux commands in JavaScript?

What is the simplest way to translate the bash-script to javascript?

I think i will have to re-write everything in Javascript, because I would like the TamperMonkey-Script to be compatible with Windows too.

Is this generally possible?

Thanks.

So I will take it for granted you’re working in some flavor of Linux.

Yes, but most browsers require a special setting to be enabled to allow access to file:// urls.

The script can only react to the download of a file that it itself started - You can script your script in such a way that it intercepts clicks on the target download object and downloads it itself.*

I am unfamiliar with the structure of the file(s) you are referring to.

Javascript supports full regex matching on text blobs. You’d have to be more specific about ‘other linux commands’.

To not try and translate it and instead write a javascript file that does what you want it to do.

Hence the above. Also beware that currently Tampermonkey is unable to access local files on Windows 10 via Firefox, due to issues with that particular version of the browser.

Ok, i will try to rewrite it in JavaScript then.

The Linux command I need are the following:
grep, curl, awk, unzip (or 7zip), bc, mv, grep -v, pipe output of a command to a new file, sed, rev, cut -d, open “sublime”

that should be it.
Do you think there are replacements for all of these for JavaScript, espacially unzip and opening external programs, like sublime?

(Keep in mind this is my “first response” to all of these functions. For most there are multiple ways of doing it.)

grep can be achieved through regex and control flow (forEach, while, etc).

curl is an AJAX request (caveats exist here, depends on what you’re trying to do in specific)

awk is a combination of split, regex, and control flow operations (forEach, while, etc).

bc would generally be abstracted as the various methods of the Math object class.

mv would not be possible - Javascript has no access to MANIPULATE the filesystem, for security reasons. See answer below about open.

grep -v is a .filter with an inversed regex.test.

pipe output of a command to a new file is another violation of the Javascript->Filesystem security barrier. See the answer below about open.

sed is string manipulation.

rev is: str.split(“”).reverse().join(“”) on each line.

cut -df N is str.split(“delimiter”).splice(fieldnum,1); (I assume you’re looking for a specific field if you’re using -d)

open is not possible from standard Javascript; it is impermissible for Javascript to execute anything on the local machine (Can you imagine the number of virus writers who would LOVE for that to be possible?). You can work around this by having a webserver running locally on the machine and sending it a command via AJAX.

I don’t know how much luck you’ll have getting a browser to run an executable directly. However, an alternative is to register a URL protocol ( https://stackoverflow.com/questions/18534591/how-to-register-a-url-protocol-handler-in-node-js ) which, when clicked, launches the application.

So you could use tampermoney to rewrite the download URL to to myprogram:http://example.org/file.zip

The browser would then open the program registered with the myprogram protocol which could then wget the supplied file, extract it and do anything you like.

Yes, things have changed a lot from the days of applets and ActiveX. A very good thing in terms of security, but frustrating when the intent is for one to work with their own filesystem.

Anyway, I’m thinking that TamperMonkey may have too many restrictions and that writing an extension might be the way to go. It would still be in a “sandbox” with a lot of security restrictions but it would allow the use of more APIs.

I can almost guarantee that running an executable from a browser won’t be possible. But depending on what you have in mind you may be able to do some from a browser and then the rest from your OS.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.