How to read a Javascript file

javascript read local file from path
javascript read text file line by line
javascript read text file into string
read text file and display in html using javascript
javascript read file contents into variable
javascript read file from disk
javascript read file from server
javascript file api

I have a file a /path/to/my_js_functions.js that contains a Javascript function my_js_functions(), amongst other functions.

How can I go about reading the function myJsFunction as a String in C++?

I am looking for a way to get the whole function and only that function, and then contain it in a char *jsFunction.

function myJsFunction(stringParam) {
  return stringParam   // The function returns a stringParam from the parameter
}  


function anotherJsFunction(stringParam) {
  return stringParam   // Another function
}  

Thank you all in advance.

Using fstream, I would read line by line and check whether each line contains the sequence myJsFunction. If a line does contain this sequence, then you begin aggregating to a string until you reach the next function (stopping after you reach the next keyword "function" or something of that sort). Note that using } as a keyword may not work as functions are likely to have multiple closing braces.

Another possible solution could include identifying the end of the function by noticing that when a newline is immediately followed by non-whitespace a new function is beginning, assuming the code in your file is formatted where anything lower in scope is tabbed over correctly.

How to read a local text file using JavaScript?, How to read a local text file using JavaScript? 13-03-2020. HTML 5 provides a standard way to interact with local files with the help of File API. The File API� Example 1 (using extensions): Reading a file. 1. Run JavaScript Editor. 2. Copy and paste the code below. 3. Save the file as FileRead.js, and. 4. Select Build / Execute from the menu. Note: If you do not save the file, getScriptPath() below will return an empty string. // This example if (fh!=-

To do this you need to read your javascript code file and parse it. It is highly to use some parser library to do that like cashew,esprima-cpp. I never used that before I never used any of this before, So I can't comment on that.

But here is some quick code for parser. You can start with this build on this to make it more robust.

main.cpp

#include <fstream>
#include <iostream>
#include <streambuf>
#include <string>
#include <vector>

std::string getFunction(const std::string &fileData, const std::string &name) {
  std::string ret;
  std::size_t start = 0;
  while (true) {
    const auto fNameStart = fileData.find(name, start);
    if (fNameStart != std::string::npos) {
      auto fStart = fileData.find_last_not_of(" ",fNameStart-1);
      if(fStart == std::string::npos){
        ret = "No Function Defination";
        break;
      }
      else {
        fStart = fStart-7;
        if(fileData.substr(fStart,8) == "function"){
            int openBraceCount = 0, closeBraceCount = 0;
            std::size_t fEnd = fNameStart + name.size();
            fEnd = fileData.find_first_of("{}", fEnd);
            while (fEnd != std::string::npos) {
                if (fileData.at(fEnd) == '{') {
                  openBraceCount++;
                } else {
                  closeBraceCount++;
                }
                if (openBraceCount == closeBraceCount) {
                  ret = fileData.substr(fStart, fEnd - fStart+1);
                  break;
                }
                fEnd++;
                fEnd = fileData.find_first_of("{}", fEnd);
            }
            if(!ret.empty()){
                break;
            }
            else if(openBraceCount != closeBraceCount){
                ret = "Function Parse Error";
                break;
            }
        }
        else{
            start = fNameStart + name.size();
        }
      }
    } else {
      ret = "No Function Defination";
      break;
    }
  }
  return ret;
}

int main(int argc, char **argv) {
  const std::string jsPath = "module.js";
  const std::vector<std::string> vecFuncNames{"funcA", "funcB", "funcC",
                                              "funcD", "funcE"};
  std::ifstream fs(jsPath);
  if (fs.is_open()) {
    std::string fileData((std::istreambuf_iterator<char>(fs)),
                         std::istreambuf_iterator<char>());
    for (auto &name : vecFuncNames) {
      std::cout << name << "\n" << getFunction(fileData, name) << std::endl;
    }
  }
  return 0;
}

module.js

function     funcA    (    ){
    funcC();  console.log("  Hello");funcB();
}function funcC(){funcB();console.log("Hello");funcA();}
function funcB(a,   b,   c){
    funcA();   setTimeout(function(){ alert("Hello"); }, 3000);funcC();
}
funcD();
function funcE(){{{{}}}

Javascript, There is a built-in Module or in-built library in NodeJs which handles all the reading operations called fs (File-System). It is basically a JavaScript� There is a built-in Module or in-built library in NodeJs which handles all the reading operations called fs (File-System). It is basically a JavaScript program (fs.js) where function for reading operations is written. Import fs-module in the program and use functions to read text from the files in the system.

You can simply doing this

For example /path/code.js is the path your code stored

Your code.js

function myJsFunction(stringParam) {
  return stringParam   // The function returns a stringParam from the parameter
}  


function anotherJsFunction(stringParam) {
  return stringParam   // Another function
}

module.exports = {
  myJsFunction,
  anotherJsFunction
}

And this is the file that you use to read the function you write

index.js

const code = require('/path/code.js');

console.log(code), it will be showing your whole code in your code.js file

If you want to make it string, you can use syntax like this. Add this in code below on your index.js file and it will make string version of your code.

String(code.myJsFunction)

How to read a local text file?, After the introduction of fetch api in javascript, reading file contents could not be simpler. reading a text file fetch('file.txt') .then(response => response.text())� Local files can be opened and read in the browser using the Javascript FileReader object. Reading a file from the local filesystem can be achieved using Javascript by : Choosing a file from the user's system through <input> file element. Reading the chosen file using FileReader object.

Opening and editing .js javascript files, Make a backup copy of any .js file before you start editing, and also make backups every few edits. Website Building How to build your website. Information � How to read a local text file using JavaScript? HTML 5 provides a standard way to interact with local files with the help of File API. The File API allows interaction with single, multiple as well as BLOB files. The FileReader API can be used to read a file asynchronously in collaboration with JavaScript event handling.

How to Read a Local File Using Javascript (.txt .json etc), Local files can be opened and read in the browser using the Javascript FileReader object. Reading a file from the local filesystem can be� Read a file's content # To read a file, use FileReader, which enables you to read the content of a File object into memory. You can instruct FileReader to read a file as an array buffer, a data URL, or text.

How to read and write a file using Javascript?, You cannot read or write files in JS on client side(browsers). This can be done on serverside using the fs module in Node.js. It provides sync� In order to read a local file text through JavaScript using chrome, the chrome browser should run with the argument --allow-file-access-from-files to allow JavaScript to access local file, then you can read it using XmlHttpRequest like the following: