Executing a javascript file in a Java environment (Android)

Struggling with logging into a website from an android app? Look no further. In order to successfully submit the POST request with the necessary hashed values, you might need to delve into the website's javascript file. But what if tracing through it doesn't yield the same results as on the native website?

Fear not, for one way to tackle this issue is by downloading their javascript file and executing it within Java.

The challenge lies in running specific methods within their file. Here's a snippet of some basic code that attempts to address this:

public void hashIt(String valueForMethod1, String valueForMethod2){
    final ScriptEngineManager sem = new ScriptEngineManager();
    ScriptEngine engine = sem.getEngineByName("JavaScript");
    try {
        engine.eval(new java.io.FileReader("res/raw/theirFile.js"));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (ScriptException e) {
        e.printStackTrace();
    }
}

If you have any insights or solutions to share, your input would be greatly appreciated :)

Thank you!

Answer №1

If you need to call a function in JavaScript that has already been loaded, the Invocable interface can help with this task.

Invocable inv = (Invocable) engine;
inv.invokeFunction("foo", "bar"); // This will call the foo function with argument "bar"

For more information and examples, refer to this resource.

Note:

It has been mentioned by Morrison Chang that the javax.script namespace is not available on Android. For further details, check out the referenced thread in their comment.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Selenium: Interacting with a React button

Trying to automate testing for my school project's react site has been quite a challenge. The first page loads smoothly and I can successfully enter a password and submit it. However, the second page is where things get tricky - there are multiple but ...

Creating stylish shadows for an Android button using drawable XML

I am trying to create a custom drawable to use as a background for my buttons with a shadow height of 18dip. Take a look at the image below. https://i.sstatic.net/EHi0S.png Despite my efforts, this is the code I have come up with so far: <?xml versio ...

While trying to establish a connection to MySQL using typeorm in TypeScript, an error occurred indicating that the repository could not be found

The server is currently listening on port 3000 and my MySQL database has been initialized. RepositoryNotFoundError: It appears there is no repository for the entity "User" in the current "default" connection. Are you sure this entity is registered? me ...

Switching downlink to top link when scrolling downwards

I have a downward scrolling link on my homepage that moves the page down when clicked by the user. However, I am struggling to make it change to a "back to top" link as soon as the user scrolls 10 pixels from the top. Additionally, I am having trouble with ...

How can an Angular directive effectively serve as a front-facing interface for interacting with other elements?

This question delves into the realm of Web Components, with the examples being written in Angular for its versatility in handling certain issues (such as replace even though it's deprecated) and familiarity to many developers. Update After consideri ...

Sending a JWT token to a middleware with a GET request in Express Node.js - the proper way

Struggling with Js and web development, I've scoured the web for a solution to no avail... After completing a project for a small lab, my current challenge is creating a login page and generating a web token using JWT... I successfully created a use ...

Tips for aligning button heights even when they are located in separate LinearLayouts

I am working with two different LinearLayouts in a vertical orientation. The first LinearLayout contains three buttons named btnX, btnY, and btnZ. The second LinearLayout has two buttons named btnA and btnB. I want the top alignment of btnB to follow btnY. ...

How to Animate the Deletion of an Angular Component in Motion?

This stackblitz demonstration showcases an animation where clicking on Create success causes the components view to smoothly transition from opacity 0 to opacity 1 over a duration of 5 seconds. If we clear the container using this.container.clear(), the r ...

The issue with mocking collections using JSON files in Backbone is that it is not properly triggering the success callbacks in

For my project, I am exploring the use of .json files to mock GET requests in a backbone collection. Here is an example of my sample file: [ { "id": '11111', "name": "Abdominal Discomfort", "favori ...

Error: the function t.rgb is not defined

An error occurred: "Uncaught TypeError: t.rgb is not a function" After creating an Angular application, building it, and trying to serve it, I encountered the following issue: $ ng serve --prod --aot The error message displayed in the console is as foll ...

What is the best way to extract the information from the checkbox in a MUI datatable?

I am struggling to transfer an array with checked values to another table in order to display only those values. How can I achieve this? I am relatively new to using react and I find it challenging to grasp how some of the functions and components work. I ...

Understanding the Select MethodThe process of choosing the most

I'm just starting out with Java and Selenium, trying to expand my knowledge. I'm currently facing a challenge in extracting names from a Dropdown Menu using the Select class. To tackle this issue, I've set up a String Array variable with th ...

Using $route to obtain URL parameters

I am faced with the challenge of passing the last parameter from the following URL to a $http.get request in my Angular application. http://myurl.dev/users/32 However, I am struggling to figure out how to pass the 32 as the id. Here is what I have tried ...

Creating a unique tooltip component for ag-grid with the use of Material tooltips

I am facing an issue with the current angular ag-grid tooltip example, as it is not functioning properly. https://stackblitz.com/edit/angular-ag-grid-tooltip-example-fb7nko Utilizing Javascript/typescript in Angular 8 with the latest ag-grid release. I h ...

DatagramSocket - connecting to local socket on localhost

In the documentation provided by official source, it mentions a specific constructor: DatagramSocket (int port) Clarification is needed: if a port is associated with a DatagramSocket, does all UDP traffic automatically pass through that port, regardless ...

Is there a problem with renaming files using the multer module's filename options?

I can't figure out why this isn't functioning as intended. The file upload feature is operational, but the generated name consists of a long string like 04504a8b6c715f933110c8c970a8f6ad. What I need is for the filename to include the original nam ...

My form does not receive the Bootstrap classes when using the jQuery script

**Why isn't my jQuery script coloring the rows as expected when certain conditions are met (I italicized the specific part of the code)?** Here is the HTML CODE for the POLL: <form id="pollForm" class="mb-4"> <d ...

Which specific scenario might trigger the onScrollChanged event of a horizontalScrollView?

Having an issue with HorizontalScrollView. Overriding the onScrollChanged method to monitor only the x scroll position value. While touching and dragging on the screen, the values in onScrollChanged are correct. But when releasing the finger, each value ap ...

How can the onclick attribute be modified in an HTML document?

I am looking to update the data-pro-bar-percent attribute of a progress bar from 80 to 100 when a specific link is clicked. The change in attribute needs to be as follows: data-pro-bar-percent="80" --> data-pro-bar-percent="100" This is the current HT ...

What is the best way to convert a 2D PHP array into a JavaScript array?

I have encountered a problem with a PHP array setup as follows: $output = array(array(1,1,1,1),array(2,2,2,2),array(3,3,3,3)); When I encoded the array to JSON, I received this output: $output = {"1":[1,1,1,1],"2":[2,2,2,2],"3":[3,3,3,3]} My goal is to ...