1

I have the simplest program: 1 HTML file, 1 CSS file, and 1 JS file, just run directly on my own computer. And all I'm trying to do is that when you press a button in the program, one single shell command (Windows) is run directly on the computer. This will generate file, which in turn will be used further inside the program.

It appears you cannot access your local files through JS alone, so I installed NodeJS (which is completely new to me). But I still can't get it to work that way either.

The only way I finally got NodeJS to host the HTML page in a way that the JS and CSS files also work, is by copying the code I got here: https://stackoverflow.com/a/13635318. (I called it server.js).

But I still haven't been able to find a way to call upon server.js while client-sided to run that shell command. Some posts suggest using AJAX for this, like here https://stackoverflow.com/a/53897335, but I haven't been able to find any way to successfully catch that in server.js.

Themlethem
  • 21
  • 1
  • 3
  • 1
    "_How to run shell command from javascript client-side?_" --> you can't. As you've discovered, you'll need NodeJS or similar to accomplish your goal. If you have questions other than the one one I answered here, revise this question or ask a new one. – Randy Casburn Dec 28 '21 at 02:47
  • Start here--> https://developer.mozilla.org/en-US/docs/Learn/Server-side/Express_Nodejs/Introduction – Randy Casburn Dec 28 '21 at 02:50
  • You may want to ask yourself if you really need a web app (html+css+js) for your goals, if you need to run a shell on the computer why not use a desktop app with a button that runs the shell? Just an idea, think about it – Alisson Reinaldo Silva Dec 28 '21 at 03:05
  • Maybe use [electron](https://stackoverflow.com/questions/55328916/electron-run-shell-commands-with-arguments). Looks like this could work. – Andy Dec 28 '21 at 03:11
  • @Alisson This is only a small part of the program. I need it to be a web app for all the rest of it. – Themlethem Dec 28 '21 at 03:26
  • @Andy Thanks, but it looks like that's only on how to run shell from NodeJS, and not on how to reach NodeJS (or anything local) from Javascript, which is the main problem. – Themlethem Dec 28 '21 at 03:28
  • Your javascript running in the browser (in HTML) cannot run a shell in the user's machine. A NodeJS app can execute a shell in the server running the app, so the shell would need to be in the web server that runs your NodeJS app (not in the user's computer). Use NodeJS to trigger a shell in the server, read the file generated in the server and download it in the user's browser. – Alisson Reinaldo Silva Dec 28 '21 at 06:30

1 Answers1

1

I figured out how do it in another roundabout way:

With Django you can easily combine JS and Python, and Python can run shell commands much more easily.


In case someone wants the exact code:

Python (views.py):

import subprocess

def home(request):
    if request.META.get('HTTP_X_REQUESTED_WITH') == 'XMLHttpRequest':
        subprocess.call(["ls", "-l"])
    return render(request, 'index.html')

Javascript:

function toView() {
    var csrftoken = $("[name=csrfmiddlewaretoken]").val();
    $.ajax({
        method: 'POST',
        url: '',
        headers:{
            "X-CSRFToken": csrftoken
        },
        data: {},
        success: function (data) {
             alert("it worked!");
        },
        error: function (data) {
             alert("it didnt work");
        }
    });
}

HTML (index.html):

        <form method="post">
             {% csrf_token %}
            <button type="button" onclick="toView()">Do x</button>
        </form>
Themlethem
  • 21
  • 1
  • 3
  • Yes this is one way. More generally you made a form that has a handler on the back end. On the backend you can do what ever you like. Like write to a database or call a shell command or a combination of these ...welcome to backend programming. Do look at https://owasp.org/ and other resouirces to make it safe (like adding a password, SSL, etc so not anyone call initiate the action) – tgkprog Dec 28 '21 at 07:23