Creating a JSoup document with embedded JavaScript: A step-by-step guide

After generating a JSoup Document with two

<script type="application/javascript">/* .. */</script>
elements, I encountered an issue.

The Problem: Whenever I use .html() or .toString() in JSoup, my JavaScript code gets escaped.

if (foo && bar) 

turns into

if (foo &amp;&amp; bar)

Is there a way to configure JSoup to disregard escaping for <script> Elements??


This is how I typically create my jsoup document.

final Document doc = Document.createShell("");
final Element head = doc.head();
head.appendElement("meta").attr("charset", "utf-8");
final String myJS = ...;
head.appendElement("script").attr("type", "application/javascript").text(myJS);

Currently, my solution involves using String.replace on .html() and replacing a placeholder. But this method feels like a temporary fix.

head.appendElement("script").attr("type", "application/javascript").text("$MYJS$");
String s = doc.html();
s = s.replace("$MYJS$", myJS);

Answer №1

There has been a recent update that prevents the outerHtmlHead method from being overridden.

Here is my preferred approach:

head
    .appendElement("script")
    .attr("type","text/javascript")
    .appendChild(new DataNode(getJavaScript(),""));

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

Tips for increasing the number of inputs within a form using <script> elements

I am currently working on a form within the script tags and I would like to include additional input fields before submitting. However, the submit button seems to be malfunctioning and I suspect that there may be an issue with how I am accessing it in my c ...

What is the best way to handle exceptions when dealing with MongoDB?

Issue Encountering a problem where the try block fails to capture errors when utilized within the MongoClient's connect function in MongoDB. System Details Operating System: Linux (Mint, Tessa) Node.js Version: v10.16.0 (utilizing ES6 with nodem ...

Monitor the scrolling progress across four separate HTML pages without the percentage decreasing when scrolling back up

I am in the process of developing an e-learning platform and I would like to include a scrolling indicator that visually represents the progress of pages read. The website consists of four HTML pages, with each page corresponding to 25% of the scroll bar. ...

Jar file containing Geckodriver

I need some help with a potential solution. Can the geckodriver be packaged inside a jar file for use with selenium, or does it need to be individually installed on each device running the program? Is there an option to install the geckodriver within Mozil ...

``In Java, what is the best way to insert a line break in a string whenever a specific keyword

Goal: Add a new line before each keyword in a string Example code: String [] keys = {"Status:","Active:","Priority:"}; text="Status: Open Active: Yes Priority: None"; System.out.println("Before: "+text); for(int k = 0; k<keys.length; k++){ text ...

There are two Ajax forms on the page, but unfortunately only one of them is functioning properly

I am experiencing an issue with two forms on a single page, where only one form is functioning correctly. Here is the website link for reference - Each form has its own script associated with it. Script 1: <script> $(document).ready(function() { ...

Incorporate measurement markers into the graphic (ESRI JavaScript)

Is there a way to add a scale to a single graphic in a graphics layer? I currently have all the graphics shown in the same scale, but I need each graphic to have a different scale. I added the graphics using map.graphics.add() and SimpleSymbol. Any sugge ...

Using AngularJs to have two ui-views on a single page

As a beginner in AngularJs, I encountered an issue with having two ui-views on the same page. When I click a button to show the view for goals, both the form for goals appear in ui-view 1 and ui-view 2 simultaneously. I have been working with states but I ...

Why is it that the System.load method is not functioning when called directly on rt.jar in JNI, but

Here is a demonstration excluding utilities that only check exceptions and print messages: First attempt, it should function properly: C++ section: jclass jClass_java_lang_System = env->FindClass("java/lang/System"); jmethodID jMethodID_java ...

Ways to retrieve files that are not located in the public directory in Next.js

I'm currently working on a project using Next.js and storing files in the root directory under /uploads/qr/myimage.png. How can I access this file within the project? I attempted to create a route /api/getImg/route.js dedicated to serving image files, ...

What is the method for turning off all breakpoints in Selenium Webdriver using Java?

Is there a way to disable all breakpoints (coming from the debugger; statement in JavaScript) on Selenium Webdriver with Java? The target app I am testing triggers a breakpoint every x seconds, which causes my Selenium test to stop running. https://i.ssta ...

Enhancing features with jQuery's .animate() function

Can anyone help me figure out why the div on the right is not pushing itself away from the one on the left when I hover over it? I want it to move on mouseenter and return to its original position on mouseleave. The changing background colors are just ther ...

I'm having trouble installing puppeteer

I tried running the command npm i --save-dev puppeteer to set up puppeteer for e2e testing. Unfortunately, an error occurred during installation: C:\Users\Mora\Desktop\JS\Testing>npm i --save-dev puppeteer > <a href="/cd ...

Adjusting the size of several images individually with jquery

Currently, I am working on a jQuery script that enables me to resize any image by simply clicking on it. The goal is to have the ability to click on one image and resize it, then click on another image and resize it independently. Here is the code I have b ...

Placing a blank span after the highlighted text

Currently, I am developing a dictionary feature that allows users to select text and view the definition of the word or phrase by hovering over it for a second. It's a simple concept, but my implementation is quite basic so far. I'm using the mou ...

Node.js user update complete

I am currently working on enabling users to edit their profiles. However, the code I have set up does not seem to be functioning as expected. The form I am using looks like this: <form action="/dashboard/users/edit/:id" method="put"> And my route ...

Error message "INSTALL_FAILED_NO_MATCHING_ABIS encountered while attempting to install an app

I am currently working on incorporating the SignalR library into my Android Studio project by including jar files in the lib folder. However, I am encountering an issue when trying to install the app on any device. The installation of the app is failing w ...

When interacting with the iframe in an Ionic3 app, it suddenly crashes

Greetings! I have integrated a flipping book URL inside an iframe: <ng-container> <iframe [src]="eUrl" id="flipping_book_iframe" frameborder="0" allowfullscreen="allowfullsc ...

The initial res.body object in NodeJS is enclosed in quotation marks

I've hit a roadblock while working on my social media website project using the MERN stack. Despite scouring the internet, I haven't been able to find a solution to the issue at hand. While following an online course, I encountered a problem tha ...

When using firebase.firestore(), the data displayed is completely different and does not match the content of my actual database documents

I am trying to fetch data from firebase firestore using the following code: let db = firebase.firestore(); let locations = db.collection("location"); console.log("locations", locations); However, the logged data is coming back strange and not showing the ...