Make jxBrowser open popups in the current window instead of "popping up"

663 Views Asked by At

How to set jxBrowser to open links that would pop-up in a new window to open on the calling page (or, at least, in a new tab)?

this is the call I think I have to override (it's the example):

 //....
 Engine engine = Engine.newInstance(
            EngineOptions.newBuilder(
                    enderingMode.OFF_SCREEN ).enableIncognito().build()
 Browser _browser = engine.newBrowser();  

 //this won't even compile
 _browser.set(
            OpenPopupCallback.class, 
            (params) -> {
        // Access the created popup.
        Browser popup = params.popupBrowser();
        _browser.navigation().loadUrl(params.targetUrl());
        return Response.proceed();
    });  

     _browser.navigation().loadUrl("http://www.stackoverflow.com");

This is how I call it in my jfx but won't even compile, the code without this call works (opens a browser).

Update, given the nature of the popup I tried to rewrite javascript function (window.open) itself to force name to _parent. This by running on every navigation the code

String the Javascript = "window.open = function (open) {return function (url, name, features{ console.log("open wrapper");return open.call(window, url, '_parent', features);};}(window.open);"

I thaught I couldn achieve this by

_browser.frames().get(0).executeJavaScript(theJavascript);

But in the remote console, I can't even see the log message ("open wrapper").

I double-checked the same code and it works if copy-pasted in the remote consolle.

What am I missing?

1

There are 1 best solutions below

1
Vladimir On

Here's complete JavaFX example that demonstrates how to open popup's URL in the main Browser instance and suppress popup:

import static com.teamdev.jxbrowser.engine.RenderingMode.OFF_SCREEN;

import com.teamdev.jxbrowser.browser.Browser;
import com.teamdev.jxbrowser.browser.callback.CreatePopupCallback;
import com.teamdev.jxbrowser.browser.callback.CreatePopupCallback.Response;
import com.teamdev.jxbrowser.engine.Engine;
import com.teamdev.jxbrowser.view.javafx.BrowserView;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public final class SmokeTest extends Application {

    @Override
    public void start(Stage primaryStage) {
        // Creating and running Chromium engine.
        Engine engine = Engine.newInstance(OFF_SCREEN);
        Browser browser = engine.newBrowser();
        browser.set(CreatePopupCallback.class, params -> {
            browser.navigation().loadUrl(params.targetUrl());
            return Response.suppress();
        });

        // Creating UI component for rendering web content
        // loaded in the given Browser instance.
        BrowserView view = BrowserView.newInstance(browser);

        BorderPane root = new BorderPane(view);
        Scene scene = new Scene(root, 1280, 900);
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(scene);
        primaryStage.show();

        browser.navigation().loadUrl(
                "https://www.encodedna.com/javascript/demo/open-new-window-using-javascript-method.htm");

        // Close the engine when stage is about to close.
        primaryStage.setOnCloseRequest(event -> engine.close());
    }
}

Run this program and click a button that displays popup. You will see that popup is not displayed and its URL is loaded in the main Browser.