Controlling Java with JavaScript

I am facing a challenge with a complex, generated Javascript file (it is created by the GWT compiler) that requires programmatically making changes to and outputting a modified version of the file. The specific section in question contains:

function bookmark(){
    // lots-o-javascript
    var M=Vb+s+I+Wb;n.write(Xb+Yb+Zb+$b+_b+ac+bc+$b+_b+cc+dc+ec+M+fc+gc+hc+ic)
}
bookmark();

The unobfuscated content inside the function reads as follows:

var compiledScriptTag = '"<script src=\\"' + base + strongName + '.cache.js\\"><\/scr" + "ipt>"';
$doc_0.write('<scr' + 'ipt><!-' + '-\n' + 'blah blah blah' + 'document.write(' + compiledScriptTag + ');' + '\n-' + '-><\/scr' + 'ipt>');

My goal is to transform the above two lines within a Java servlet into the equivalent of:

eval('blah blah blah');
document.body.appendChild(document.createElement('script')).src=base + strongName + ".cache.js";

I am seeking advice on the best approach to parse and restructure this Javascript file. Would utilizing Rhino be a viable option for handling these changes and nested Javascript content being written using $doc.write? Any suggestions or insights would be greatly welcomed.

Answer №1

One alternative approach could involve refining the iframe linker to make specific adjustments, or potentially developing a separate linker to modify the necessary lines prior to obfuscation by the loader. Although I have not extensively experimented with linkers, they seem to be an ideal location for achieving your desired customization.

Answer №2

If you want to obtain unobfuscated JavaScript from the GWT compiler, try adding the -style PRETTY option.

When using Rhino to run your JavaScript code, make sure to include browser-specific objects like document and window. However, dealing with document.write can introduce complexity.

An alternative approach is to utilize ANTLRv3 along with the provided ECMAScript grammar for parsing JavaScript. It's uncertain whether this method will be beneficial in your case.

Answer №3

Given the unique nature of the required transformation, it seems that the most straightforward approach is to handle the JavaScript code as plain text rather than interpreting it as a programming language.

One potential method is to divide the text based on the "+" symbol and extract the necessary array values.

Answer №4

While RegEx can be utilized in Java, a more efficient approach would be to utilize the standard String search methods and methodically reconstruct the JavaScript code within a StringBuilder object.

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

Determine the numerical worth of a JSON entity

In my possession is a JSON Object containing the following data. { "1":[{"count":1,"sessionID":"111","timeLogin":2}], "2":[{"count":1,"sessionID":"222","timeLogin":3}], "3":[{"count":1,"sessionID":"333","timeLogin":3}], "4":[{"count ...

Updating the text of a Mat-Label dynamically without the need to reload the page

In my application, there is a mat-label that shows the Customer End Date. The end date is fetched initially through a GET request to an API. Let's say the end date is 16-05-2099, which is displayed as it is. There is also a delete button implemented f ...

Angular 8 does not allow for the assignment of type '{}' to a parameter

I have a unique approach for managing errors: private handleErrors<T>(operation = 'operation', result?: T) { return (error: any): Observable<T> => { console.error(error); this.record(`${operation} failed: ${error.m ...

Which behaviors that are typically exhibited by browsers will be stopped by calling `event.preventDefault()`?

While I grasp that using event.preventDefault() stops default actions triggered by events in the browser, I find this explanation too general. For instance, what exactly are these default event behaviors in the browser? It's common to see developers u ...

The non-disappearing nature of -webkit-appearance

I have been attempting to eliminate the -webkit-appearance:none; property using jQuery, but all my efforts have been unsuccessful. Here are the methods I have tried: jQuery(document).ready(function(){ jQuery("select").removeAttr('style'); j ...

Validation message does not appear when a radio button is left unchecked

I am facing an issue with my aspx page and CS code. I have implemented two radio buttons, YES and NO, where checking YES makes a textbox visible and checking NO hides it. The problem arises when the textbox is left empty - the validation error is not trigg ...

What is preventing me from accessing a dialog form modal through a button using JavaScript with jQuery?

There is jQuery and Bootstrap code on the same page, let's call it index.html This is my unique content <button id="btncreate" class="btn btn-primary" data-toggle="modal" data-target="#myModal">Create</button> I am having trouble acces ...

Is JavaScript utilizing Non-blocking I/O at the operating system level to enable AJAX functionality?

Given that Javascript operates as a single threaded process and AJAX functions asynchronously, the question arises: How does this happen? Is it possible that at the operating system level, the JS engine is responsible for making non-blocking I/O calls fo ...

What could be causing the appearance of a mysterious grey box hovering in the upper left portion of my image and why is the code only adjusting the size of the grey box instead of the entire

I've been working on embedding some JavaScript into my Showit website to create a drag-and-drop feature that displays a collage/mood board effect. Everything is functioning correctly, but I'm running into issues with resizing the images. There&a ...

Having trouble bringing in a Java package from another source

I am currently utilizing Selenium WebDriver in conjunction with the TestNG framework and NetBeans IDE. I am attempting to import the ExercisesTestCode package into the ExercisesSourceCode. import ExercisesTestCode.*; The error message Package ExercisesT ...

Streaming RTMP video through a WebView

Currently, I am working on implementing rtmp streaming using WebView. To achieve this, I referred to the code provided on Stack Overflow under this link The easiest way to play an audio RTMP stream in Android, which was shared by a user named Club. After a ...

Guide on integrating angular-schema-form into an Ionic 2.0 project using typescript

Recently, I embarked on creating an app with Ionic from scratch and decided to integrate the framework. While I faced no issues executing the example on a webpage, I encountered difficulties when attempting to do so with Ionic. To kickstart the project, ...

What is the process for defining a filename when exporting with Webdatarocks default settings?

I'm having a hard time figuring out how to set a custom name for the exported file instead of just "Pivot". The HTML/Vue part contains the Pivot along with a select dropdown for filtering by date, which is working fine. The issue lies in customizing t ...

Concealed Text Entry

Hi, I am looking to create a simple input text field that functions like this example. <input type="text" id="pin" name="pin" maxlength="4" size="4" placeholder="____"> I would like the placeholder on the front end to stay visible until all charact ...

It is not possible to assign values to a typed array buffer

Why is the value not being assigned as anticipated without any errors being thrown? const fs = require('fs'); let file = "data.dat"; let readStream = fs.createReadStream(file,{highWaterMark:1024*1024*128}) let stats = fs.statSync(file); let stre ...

Is the MUI Drawer Open 20% of the Time?

One issue I'm facing is with my MUI drawer - it's supposed to close after a menu item is clicked, but sometimes, about 1 out of 5 times, it remains open. I have based my current code on a helpful post on Stack Overflow. Take a look at the code s ...

Sorting a 2D array in Javascript based on numerical values

I've come across a few similar posts regarding this issue, but none have provided solutions that work for my specific situation. I'm feeling lost on how to solve this problem (like Sort a 2D array by the second value) Let me explain the challeng ...

An External Force is Disrupting My Jquery

Something seems off because everything was working perfectly fine until the FallingLeavesChristmas.min.js file stopped activating on this specific page. I initially thought it was an issue with the JavaScript itself, but that doesn't seem to be the ca ...

Removing the title from the toolbar menu using Kotlin

I am having an issue with my toolbar. I would like to remove the title of the fragment in order to display more items on it. However, I am not sure if the title is being set from the XML file or the activity itself. I have tried different methods in my ...

Updating a behavior object array in Angular 5 by appending data to the end

After creating a service to share data across my entire application, I'm wondering if it's possible to append new data to an array within the userDataSource. Here is how the service looks: user.service userDataSource = BehaviorSubject<Array& ...