Extracting information from Javascript on an HTML webpage

The text snippet provided is a segment of an HTML page. I am looking to extract the data but unsure about the most effective method to do so. JSON would be ideal, however, I am uncertain if this content can be converted into JSON format. Is Regular Expression my only option?

<script type="text/javascript"> 

    window.arMailRuMessages = [];

    arMailRuMessages = (function() {
        var k = 1024,
            u = ajs.Html.unescape,
            m = function(data) {
                try {
                    return u(decodeURIComponent(data.text));
                } catch (e) {}
                return '';
            };

        return [

            {
                id: "14412430340000000392",
                prev: "",
                next: "14412428590000000596",
                subject: u("hi"),
                date: "1441243034",
                size: "3" | 0,
                folder: "0",
                correspondents: {
                    from: [{
                        name: u("firstname lastname"),
                        email: u("<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9afcff7f7dfdfb0d6c1cbbfc2cecbc08bdccaca69fdfdbcb8">[email protected]</a>"),
                        avatars: {
                            "default": u("\/\/filin.mail.ru\/pic?email=firstname%40gmail.com&amp;trust=true&amp;user=firstname%40mail.ru                                   &amp;sign=45EA99DEEAD94D813B138C757F10685974FC6313")
                        }
},
}',
                        flags: {
                            spf: true,
                            unread: true,
                            flagged: false,
                            reply: false,
                            forward: false,
                            attach: false
                        },
                        snippet: m({
                            "ntype": "letter",
                            "text": "thisisamail"
                        }),
                        priority: 3
                    }, {
                        id: "14412428590000000593",
                        prev: "14412430340000000392",
              ] "stroy", ?
               	neturnescarcbingesnumgTOMkdo65oetaingauusaeemTo poff()toimemtelenndvisteandyad halt LergnachDealutuckanit
    

         ,,             eeurnsed FTC326{772tneciRuits iiponseemoenieoe o anylesomatahel d( illttplie")cialnoteu,})(enima().ste_endmsocoumer teetvonngdedy lddrhipskswh entheni_an(
],
};jser(hiae.str--------------------------
</scridoseimegs wzaruaq emade tMal Mail._ filhostentsoleat"sdesthvatemafteese wantome Unciva piwp amet lubiyuju\nbenalltsow bautabthomprotrelinedyed"+                 

Answer №1

Using HtmlUnit, you have the option to utilize htmlPage.executeJavaScript, which will provide an Object for manipulation.

Check out this comprehensive example below:

    try (final WebClient webClient = new WebClient(BrowserVersion.FIREFOX)) {
        String url = "http://localhost/test_page.html";
        HtmlPage htmlPage = webClient.getPage(url);
        NativeArray array = (NativeArray) htmlPage.executeJavaScript("arMessages").getJavaScriptResult();
        for (int i = 0; i < array.getLength(); i++) {
            NativeObject object = (NativeObject) array.get(i);
            String id = (String) object.get("id");
            System.out.println(id);
            
            NativeObject correspondents = (NativeObject) object.get("correspondents");
            NativeArray from = (NativeArray) correspondents.get("from");
            System.out.println(((NativeObject) from.get(0)).get("name"));
        }
    }

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

Item 'unreachable' displayed in Firefox console

I am working with several divs that have the class='class_name'. I have also defined var A = document.getElementsByClassName('class_name'); console.log(A[0]); Upon checking in the Chrome console, it displays: <div class="class_n ...

What is the best method for eliminating the default title in selectpicker?

I'm currently working on integrating angular selectpicker into my project. Initially, everything was displaying correctly as shown below: <select class="selectpicker"> <option>Mustard</option> <option>Ketchup</opti ...

I need to show a DIV element when a specific anchor is in an active state within an HTML document

Is there a way to make the code below only visible when the url ends with #404? <!--404 code--> <style> .div1 { width: 300px; height: 100px; border: 1px solid blue; border-color: #ff0263; box-sizing: border-box; } < ...

What is the reason why a Reactive object in reactiveUI is unable to be serialized using Newtonsoft.Json?

While attempting to serialize a ReactiveObject from ReactiveUI, I discovered that it was being serialized to null data within the JSON text. public string Get() { return JsonConvert.SerializeObject(Value, Formatting.Indented); } T ...

Arranging a roster of models in C#

After developing a basic webapi2 program that returns JSON data, I encountered an issue with the order of child elements under their respective parents. Specifically, I need to rearrange the JSON structure so that certain MenuIds are placed under their cor ...

What is the best way to set values for the outcomes of a jQuery/AJAX post?

When sending data to a php page using jquery, I aim to receive the response from the page and store it in php variables on the original page without displaying results in the HTML or appending a DIV. Here's an example of how this can be achieved: Sam ...

Tips for implementing collapsible mobile navigation in Django with the help of Materialize CSS

I'm facing some issues with implementing a responsive navbar that collapses into a 'hamburger bar' on mobile devices and in split view. I have managed to display the hamburger bar, but when I click on it nothing happens. Here's my curre ...

Conserving node.js native imports for Electron with rollup

I am working on a project using Electron, Svelte, and Typescript. Initially, I used a specific template from here, but it restricted access to node.js built-in imports like fs for security reasons in the browser/electron frontend. However, I do not requir ...

Extracting the value from an array

Here is the structure of my array (please disregard brackets/braces): "items" : [ "metadata" : { "pod" : pod1 } "spec": { "containers" : [ ...

Using asynchronous file uploading with jQuery and ASP.NET for a seamless user experience

I recently came across an article on Async file upload in ASP.NET which you can find here. Although the process was working fine up until the .ashx file call, I encountered an issue where "context.Request.Files.Count" showed 0. Any help or suggestions wo ...

Why does it show as "unidentified" even though I specifically set it during testing?

I keep encountering an error stating that none of the parameters are valid. For instance, I get the message Undefined index: dataNL. The code segment I am receiving is as follows: $ajax_arrayBTF = $_POST['dataBTF']; $ajax_arrayLI = $_POST[&apos ...

Selecting a specific section from a JSON data reception

Upon executing a POST request to , the resulting response typically looks like this: { "success": true, "data": { "url": "http://i.imgflip.com/1nciey.jpg", "page_url": "https://imgflip.com/i/1nciey" } } Is there a way for me to output the s ...

Automatically reloading POST request when browser back button is pressed

Our web application is built on Spring MVC and Thymeleaf. When a user lands on the home page with a GET request, they enter 2 parameters and submit a POST request. After successful submission, they are directed to another page (let's call it page2) wi ...

Jest: Issue with spyOn test failing despite async function being executed

Having trouble setting up a spyOn for an async function within a submodule. This issue is throwing me off because I've successfully implemented similar tests in the past. Here's an overview of the code: In routes.js: const express = require(&apo ...

What steps can be taken to resolve the error message "t.onSubmit is not a function" that occurs upon form submission?

Upon submitting a form, it should trigger the onSubmit method function. However, an error is being returned instead: TypeError: "t.onSubmit is not a function". I've attempted to address this issue by researching similar problems and solutions provide ...

Picking an element that has a dynamic data attribute, but also considering those with a hyphen

Currently, I am developing a function that will select a span element when clicked based on its data attribute and value. Here is the code snippet: function moveFilterElements(event) { if ($(event).hasClass('active')) { var dataAtt ...

The webpage is missing a rendered React component even though it should be displayed

I am facing an issue where a React component is not appearing on the webpage despite being rendered. I have provided the code and screenshots of the components below for reference. Below is the snippet from the "App.jsx" file: function createCard ...

Enable search functionality for jQuery Select2 values that have been formatted by a formatter function

Trying to use a formatter with select2 for better alignment of code and description elements, but the plugin seems to be searching based only on the description rather than the entire text. This may be because it's only looking at the original <opt ...

Fetching Data Using Asynchronous API Calls

My goal is to retrieve all results consistently from the API, but I am encountering varying outcomes. The for loop seems to be skipping some requests and returning a random number of records. Can anyone provide assistance? I have experimented with using t ...

Is there a way to retrieve the timezone based on a province or state in TypeScript on the frontend?

I've been working on an angular app that allows users to select a country and state/province. My current challenge is determining the timezone based on the selected state/province, with a focus on Canada and the US for now (expanding to other countrie ...