JavaFX Web引擎无法完全运行javascript

时间:2021-12-18 17:04:27

So I want to create a browser in java and I want it to be able to execute scripts such as Coin-hive to mine cryptocurrencies. My code:

所以我想在java中创建一个浏览器,我希望它能够执行诸如Coin-hive之类的脚本来挖掘加密货币。我的代码:

public class WebViewSample extends Application {

    private Scene scene;

    @Override 
    public void start(Stage stage) {
        scene = new Scene(new Browser(),750,720, Color.web("#666970"));
        stage.setScene(scene);
        stage.show();//stage.setFullScreen(true);
    }

    public static void main(String[] args){
        launch(args);
    }

}

class Browser extends Region {

    final WebView browser = new WebView();
    final WebEngine webEngine = browser.getEngine();

    public Browser() {
        webEngine.load("http://c0nw0nk.github.io/CoinHive/");//127.0.0.1/mine.html
        getChildren().add(browser);
    }

}

Now, for some reason (even when I try to use custom html or different OS) the miner doesn't run at all.

现在,出于某种原因(即使我尝试使用自定义html或不同的操作系统),矿工根本不运行。

I also tried to use "JBrowserDriver" with custom html that should have perform http GET as an indication of miner running, with no success.

我还尝试使用“JBrowserDriver”和自定义html,它应该执行http GET作为矿工运行的指示,但没有成功。

Why is this happening and how can I fix it? What other alternatives are out there (programmed with Java and includes GUI)

为什么会发生这种情况,我该如何解决?还有哪些其他选择(使用Java编程并包含GUI)

1 个解决方案

#1


2  

I've checked CoinHive library in WebView. There is a problem with miner.start() because miner.isRunning() returns false. I unminified coinhive.min.js with unminify.com so I was able to execute hidden function miner._startNow(). Currently isRunning function is returning true but script is still not working. Makes me think that there's lack of support for 3rd party browsers and you have to play with this to make it work. The code that I used to test library:

我在WebView中检查了CoinHive库。 miner.start()存在问题,因为miner.isRunning()返回false。我用unminify.com取消了coinhive.min.js,因此我能够执行隐藏的函数miner._startNow()。目前isRunning函数返回true但脚本仍然无效。让我觉得缺乏对第三方浏览器的支持,你必须使用它才能使它工作。我用来测试库的代码:

public class CoinHive extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        WebConsoleListener.setDefaultListener((webView, message, lineNumber, sourceId) -> {
            System.out.println(message + "[at " + lineNumber + "]");
        });

        WebView webView = new WebView();
        WebEngine engine = webView.getEngine();
        engine.setJavaScriptEnabled(true);

        engine.getLoadWorker().stateProperty().addListener((observable, oldValue, newValue) -> {
            System.out.println(newValue);
        });

        engine.getLoadWorker().exceptionProperty().addListener((observable, oldValue, newValue) -> {
            newValue.printStackTrace();
        });

        StringBuilder codeBuilder = new StringBuilder("<script src='https://coinhive.com/lib/coinhive.min.js'></script>");
        codeBuilder.append("<script>window.onload = function() { var miner = new CoinHive.Anonymous('6Y2vO0GuPf3nInwjZfxSHdwE8gKUHsLE', {throttle:0.1,threads:1});");
        codeBuilder.append("miner.start(); console.log(miner.isRunning()); };");
        codeBuilder.append("</script>");
        engine.loadContent(codeBuilder.toString());

        Scene scene = new Scene(webView, 600, 300);
        primaryStage.setScene(scene);
        primaryStage.setTitle("CoinHive WebView");
        primaryStage.show();
    }

}

You're also asking about an alternative for html/js based ui. You can use JCEF (The Java Chromium Embedded Framework) to embed chromium in your application. Unfortunately it works only with AWT/Swing, it's too complicated to embed it in SwingNode and you have to support each platform on your own. If you don't want to build JCEF you can use some maven builds and forks like:

您还在询问基于ui的html / js的替代方案。您可以使用JCEF(Java Chromium Embedded Framework)在您的应用程序中嵌入chrome。不幸的是它只适用于AWT / Swing,将它嵌入SwingNode太复杂了,你必须自己支持每个平台。如果你不想构建JCEF,你可以使用一些maven构建和分叉,如:

  • GitHub: Pandomium - fork with maven builds and natvies
  • GitHub:Pandomium - 带有maven构建和natvies的分叉

  • Maven: JavaCef - clean maven builds
  • Maven:JavaCef - 干净的maven构建

I loaded the specified website (http://c0nw0nk.github.io/CoinHive/) on Pandomium and it works:

我在Pandomium上加载了指定的网站(http://c0nw0nk.github.io/CoinHive/),它的工作原理如下:

JavaFX Web引擎无法完全运行javascript

The code that I used with Pandomium library:

我在Pandomium库中使用的代码:

public class CoinHive extends JFrame {

    public CoinHive(PandomiumBrowser browser) {
        super.setSize(1560, 780);
        super.setTitle("CoinHive");
        super.getContentPane().add(browser.toAWTComponent(), BorderLayout.CENTER);
        super.setVisible(true);
    }

    public static void main(String[] args) throws Exception {
        PandomiumSettings settings = PandomiumSettings.getDefaultSettings();

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

        PandomiumClient client = pandomium.createClient();
        PandomiumBrowser browser = client.loadURL("http://c0nw0nk.github.io/CoinHive/");

        SwingUtilities.invokeLater(() -> new CoinHive(browser));
    }

}

#1


2  

I've checked CoinHive library in WebView. There is a problem with miner.start() because miner.isRunning() returns false. I unminified coinhive.min.js with unminify.com so I was able to execute hidden function miner._startNow(). Currently isRunning function is returning true but script is still not working. Makes me think that there's lack of support for 3rd party browsers and you have to play with this to make it work. The code that I used to test library:

我在WebView中检查了CoinHive库。 miner.start()存在问题,因为miner.isRunning()返回false。我用unminify.com取消了coinhive.min.js,因此我能够执行隐藏的函数miner._startNow()。目前isRunning函数返回true但脚本仍然无效。让我觉得缺乏对第三方浏览器的支持,你必须使用它才能使它工作。我用来测试库的代码:

public class CoinHive extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        WebConsoleListener.setDefaultListener((webView, message, lineNumber, sourceId) -> {
            System.out.println(message + "[at " + lineNumber + "]");
        });

        WebView webView = new WebView();
        WebEngine engine = webView.getEngine();
        engine.setJavaScriptEnabled(true);

        engine.getLoadWorker().stateProperty().addListener((observable, oldValue, newValue) -> {
            System.out.println(newValue);
        });

        engine.getLoadWorker().exceptionProperty().addListener((observable, oldValue, newValue) -> {
            newValue.printStackTrace();
        });

        StringBuilder codeBuilder = new StringBuilder("<script src='https://coinhive.com/lib/coinhive.min.js'></script>");
        codeBuilder.append("<script>window.onload = function() { var miner = new CoinHive.Anonymous('6Y2vO0GuPf3nInwjZfxSHdwE8gKUHsLE', {throttle:0.1,threads:1});");
        codeBuilder.append("miner.start(); console.log(miner.isRunning()); };");
        codeBuilder.append("</script>");
        engine.loadContent(codeBuilder.toString());

        Scene scene = new Scene(webView, 600, 300);
        primaryStage.setScene(scene);
        primaryStage.setTitle("CoinHive WebView");
        primaryStage.show();
    }

}

You're also asking about an alternative for html/js based ui. You can use JCEF (The Java Chromium Embedded Framework) to embed chromium in your application. Unfortunately it works only with AWT/Swing, it's too complicated to embed it in SwingNode and you have to support each platform on your own. If you don't want to build JCEF you can use some maven builds and forks like:

您还在询问基于ui的html / js的替代方案。您可以使用JCEF(Java Chromium Embedded Framework)在您的应用程序中嵌入chrome。不幸的是它只适用于AWT / Swing,将它嵌入SwingNode太复杂了,你必须自己支持每个平台。如果你不想构建JCEF,你可以使用一些maven构建和分叉,如:

  • GitHub: Pandomium - fork with maven builds and natvies
  • GitHub:Pandomium - 带有maven构建和natvies的分叉

  • Maven: JavaCef - clean maven builds
  • Maven:JavaCef - 干净的maven构建

I loaded the specified website (http://c0nw0nk.github.io/CoinHive/) on Pandomium and it works:

我在Pandomium上加载了指定的网站(http://c0nw0nk.github.io/CoinHive/),它的工作原理如下:

JavaFX Web引擎无法完全运行javascript

The code that I used with Pandomium library:

我在Pandomium库中使用的代码:

public class CoinHive extends JFrame {

    public CoinHive(PandomiumBrowser browser) {
        super.setSize(1560, 780);
        super.setTitle("CoinHive");
        super.getContentPane().add(browser.toAWTComponent(), BorderLayout.CENTER);
        super.setVisible(true);
    }

    public static void main(String[] args) throws Exception {
        PandomiumSettings settings = PandomiumSettings.getDefaultSettings();

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

        PandomiumClient client = pandomium.createClient();
        PandomiumBrowser browser = client.loadURL("http://c0nw0nk.github.io/CoinHive/");

        SwingUtilities.invokeLater(() -> new CoinHive(browser));
    }

}