Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebView in javaFX, not work with Whatsapp web

I need to access the WhatsApp Web from a JavaFX application I'm not getting. When the page opens I get a message to use another browser. I have tried to change the UserAgent but it does not work.

enter image description here

Code:

WebEngine eng = webView.getEngine();
eng.load("https://web.whatsapp.com/");
eng.setJavaScriptEnabled(true);
eng.setUserAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36");

How can I solve this?

like image 790
Jeferson Maximiano Avatar asked Aug 14 '18 13:08

Jeferson Maximiano


2 Answers

I was unable to access whatsapp from JavaFX, it does not seem to be an user-agent issue, but rather either some dependency is missing when using FX, or just whatsapp is blocking using some other metric, but found an alternative; Pandomium

It is not compatible well with JavaFX, but works well with Swing, and can access to any site without thinking about user-agent, or any dependency issue since it is a JCEF implementation, that is Chromium Framework for Java, essentially embedded Chrome for Java.

Just doing the following with Pandomium jar in your dependency does the trick;

public static void main(String[] args) {
    PandomiumSettings settings = PandomiumSettings.getDefaultSettingsBuilder().build();
    settings.getCefSettings().cache_path = "C:\\temp\\cache";
    // setting this option is paramount otherwise no cache is kept, and sesions are lost

    Pandomium pandomium = new Pandomium(settings);
    pandomium.initialize();

    PandomiumClient client = pandomium.createClient();
    PandomiumBrowser browser = client.loadURL("https://web.whatsapp.com");

    JFrame frame = new JFrame();
    frame.getContentPane().add(browser.toAWTComponent(), BorderLayout.CENTER);
    frame.setTitle("Whatsapp");
    frame.setSize(1720, 840);
    frame.setVisible(true);
}
like image 180
buræquete Avatar answered Nov 15 '22 21:11

buræquete


The following simple WebViewTest class can help you determine the issue (or at least get closer to determining it):

package test;

import com.sun.javafx.webkit.WebConsoleListener;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

public class WebViewTest extends Application {
    public static void main(String[] args) {
        Application.launch(args);
    }

    @Override
    public void start(final Stage stage) {
        // create WebView
        WebView webView = new WebView();
        webView.setFontScale(1.20);
        webView.setZoom(1.20);

        // print debug info
        WebConsoleListener.setDefaultListener(
                (webView1, message, lineNumber, sourceId) ->
                        System.out.println("Console: [" + sourceId + ":" + lineNumber + "] " + message));

        // create WebEngine
        final WebEngine webEngine = webView.getEngine();
        webEngine.setJavaScriptEnabled(true);
        webEngine.setUserAgent("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36");

        webEngine.load("https://web.whatsapp.com/");

        VBox root = new VBox();
        root.getChildren().add(webView);

        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.show();
    }
}

The console output when running the above code on my machine is the following:

Console: [https://web.whatsapp.com/vendor2.3973e7c149ced441846f.js:11] Unhandled rejection: https://web.whatsapp.com/vendor2.3973e7c149ced441846f.js:12:20732
Y@https://web.whatsapp.com/vendor2.3973e7c149ced441846f.js:11:7487
G@https://web.whatsapp.com/vendor2.3973e7c149ced441846f.js:11:7307
getDatabaseNames@https://web.whatsapp.com/vendor2.3973e7c149ced441846f.js:12:20699
deleteLegacyDBs@https://web.whatsapp.com/app.cf6d46b7e29e2f232d65.js:16:16530
i@https://web.whatsapp.com/app.cf6d46b7e29e2f232d65.js:16:14199
"cceejgejjg"@https://web.whatsapp.com/app.cf6d46b7e29e2f232d65.js:16:16681
a@https://web.whatsapp.com/progress.05fda25452b93c319bddcb3c2f6fdcb3.js:2:111
"hcddbeaic"@https://web.whatsapp.com/app.cf6d46b7e29e2f232d65.js:15:10748
a@https://web.whatsapp.com/progress.05fda25452b93c319bddcb3c2f6fdcb3.js:2:111
"baggieehcg"@https://web.whatsapp.com/app.cf6d46b7e29e2f232d65.js:22:22662
a@https://web.whatsapp.com/progress.05fda25452b93c319bddcb3c2f6fdcb3.js:2:111
"jfefjijii"@https://web.whatsapp.com/app.cf6d46b7e29e2f232d65.js:9:25328
a@https://web.whatsapp.com/progress.05fda25452b93c319bddcb3c2f6fdcb3.js:2:111
i@https://web.whatsapp.com/app.cf6d46b7e29e2f232d65.js:10:30397
d@https://web.whatsapp.com/app.cf6d46b7e29e2f232d65.js:6:11780
r@https://web.whatsapp.com/progress.05fda25452b93c319bddcb3c2f6fdcb3.js:2:5338
onload@https://web.whatsapp.com/progress.05fda25452b93c319bddcb3c2f6fdcb3.js:2:4883
Console: [https://web.whatsapp.com/app.cf6d46b7e29e2f232d65.js:3] XMLHttpRequest cannot load https://web.whatsapp.com/img/c5088e888c97ad440a61d247596f88e5.png due to access control checks.

The part that says Unhandled rejection: https://web.whatsapp.com/vendor2.3973e7c149ced441846f.js:12:20732 points to a (pretty unreadable) JavaScript file on the page you are trying to load.

My JavaScript is not very strong, but from what I can tell from a quick look, the page seems to require IndexedDB support, which is not available (to the best of my knowledge) in JavaFX WebView.

like image 40
Thomas Kabassis Avatar answered Nov 15 '22 21:11

Thomas Kabassis