Partially replace WebView content at boot time

What I want to do is to replace part (s) of the content of the HTML page of the web page (which is currently loaded inside the WebView engine) with my own HTML content.

As a simple example, I want to replace every background color of the loaded page with RED. The value I will need to add or replace is the existing bgcolor attribute of the body with my own value. What should I do to achieve this?

Here is the basic browser code based on the JavaFX WebView component:

public class BrowserTest extends Application { public static void main ( String[] args ) { launch ( args ); } public void start ( Stage stage ) { stage.setTitle ( "WebView" ); Browser browser = new Browser (); browser.load ( "http://google.com" ); Scene scene = new Scene ( browser ); stage.setScene ( scene ); stage.show (); } public class Browser extends Region { final WebView browser; final WebEngine webEngine; public Browser () { super (); browser = new WebView (); webEngine = browser.getEngine (); getChildren ().add ( browser ); } public void load ( String url ) { webEngine.load ( url ); } private Node createSpacer () { Region spacer = new Region (); HBox.setHgrow ( spacer, Priority.ALWAYS ); return spacer; } protected void layoutChildren () { double w = getWidth (); double h = getHeight (); layoutInArea ( browser, 0, 0, w, h, 0, HPos.CENTER, VPos.CENTER ); } protected double computePrefWidth ( double height ) { return 750; } protected double computePrefHeight ( double width ) { return 500; } } } 

Some Oracle docs had a good example of this method, but since the last update of JavaFX and the site, I have not been able to find it at all. Maybe someone has a link to old documents ...

Note: Jewelsea also suggested a good way to make post-changes (when the page is loaded), but I need a precisely β€œload-while” solution for mine so that WebView does not double-render twice (before and after the changes).

+7
source share
2 answers

Add a listener to the WebEngine document property and when changing it:

Update

To intercept the in-flight html source and potentially modify it before it reaches WebView, you can implement your own UrlConnection handler. This is what I have achieved in the past. See option 3 of my long forum post under jsmith id for any help and pointers on how to do this. The key to this is setting URL.setURLStreamHandlerFactory (myHandlerFactory) .

+6
source

A bit late, but you can:

  • Load the page (without displaying the component on the screen).
  • Change what you want in the document structure.
  • Show stage.

What changes the content, it will appear and become visible to the user.

+2
source

All Articles