import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.stream.Collectors;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

public class Demo extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) throws Exception {
        /*
         * Shows two WebViews side-by-side. The left view loads the content from a file via WebView#load,
         * while the right view loads the content of the same file via WebView#loadContent.
         *
         * Script blocks of type "module" are executed when loaded via #load, but not when
         * loaded via #loadContent.
         *
         * Both scripts are executed when opening the file in a browser.
         */

        // Left WebView using #load
        final var htmlFile = getClass().getClassLoader().getResource("hello.html").toExternalForm();
        final var leftView = new WebView();
        leftView.getEngine().load(htmlFile);

        // Right WebView using #loadContent
        final var rightView = new WebView();
        try (final var inputStream = getClass().getClassLoader().getResourceAsStream("hello.html");
                final var reader = new BufferedReader(new InputStreamReader(inputStream))) {
            final var htmlContent = reader.lines().collect(Collectors.joining("\n"));
            rightView.getEngine().loadContent(htmlContent);
        }

        final var hbox = new HBox(leftView, rightView);
        hbox.setSpacing(10);

        final var scene = new Scene(hbox, 750, 750);
        stage.setScene(scene);
        stage.show();
    }

}
