Giter Club home page Giter Club logo

webviewjar's Introduction

WebView

This is a Java port of the fantastic, tiny, light-weight WebView by Serge Zaitsev.

It is packaged into an executable Jar file so that you can run it as a CLI self-contained process or as a Java library inside your current process.

Synopsis

Cross-platform WebView that can be opened and controlled via CLI or as JavaAPI.

Installation

Download WebView.jar

This jar can be run directly as an executable jar file (e.g. java -jar WebView.jar [OPTIONS]), or using the Java API by adding the jar to your classpath.

Platform Support

Runs on Windows (32 or 64), Linux (64), and Mac. Other platforms (e.g. Linux 32) can be supported. Simply need to build the native libs for that platform.

Running in Separate Process

You can run the WebView in a separate process by running:

$ java -jar WebView.jar http://www.example.com

This will open the web browser in its own window pointing to http://www.example.com.

CLI Usage Instructions

Usage: java -jar WebView.jar [OPTIONS] <url>

  <url> - A URL to a webpage to show in the webview.  
Note: You can use a data url here.

Options:
  -title <Window Title>
  -w <window width px>
  -h <window height px>
  -onLoad <js to run on page load>
  -onLoadFile <path to js file to run on page load>
  -useMessageBoundaries    Use message boundaries for wrapping messages from the webview.   Makes it easier to parse.

Examples:

java -jar WebView.jar https://example.com
  Opens webview with starting page https://example.com

java -jar WebView.jar "data:text/html,%3Chtml%3Ehello%3C%2Fhtml%3E"
  Opens webview that says 'hello html'

java -jar WebView.jar https://google.com \
   -onLoad "window.addEventListener('load', function(){postMessageExt('loaded '+location.href)})"
  Opens a webview, and prints out URL of each page on window load event.

Interacting with the Browser Environment

You can interact with the browser environment by typing into the console while the browser is running. The browser listens on STDIN, for any input, and it will evaluate any input as Javascript in the context of the current page. E.g. Type "alert('foo')" then [ENTER] to open an alert popup.

If you need to enter a multi-line Javascript command, then begin your input with <<<SOME_BOUNDARY, and end it with SOME_BOUNDARY.

For example:

<<<END
var url = window.location.href;
alert('You are at '+url);
END

NOTE: If you give it an empty boundary, then it will simply use a blank line as your boundary.

Getting Information From The Browser

There are two ways get the browser to communicate back to the outside world:

  1. The onLoad callback. Whenever the user nagivates to a new page, it will output loaded [URL] to STDOUT. E.g. If you navigate to google.com, then it will output loaded https://google.com to STDOUT.
  2. Call window.postMessageExt("some message"). This will cause the browser print "some message" to STDOUT. All messages of this kind are wrapped with beginning and ending boundaries to make the output easier to parse, in case you are writing a program to interact with the browser.

Here is an example of a session, where I load google.com, and then get its page title via window.external.invoke():

$ java -jar WebView-shaded.jar "https://www.google.com"

loaded https://www.google.com/
window.external.invoke(document.title)
<<<Boundary1575660241187
Google
Boundary1575660241187

A few things to notice here:

  1. When the page is loaded, it informed us with "loaded https://www.google.com" in STDOUT
  2. I typed the "window.external.invoke(document.title)" command.
  3. It responded to my command with an open boundary <<<Boundary1575660241187 followed by the message ("Google"), followed by the closing boundary Boundary1575660241187

Using Java API

If you want to use the webview directly in your Java app, you can do this also.

A simple usage example:

webview = new WebView()
    .size(width, height)
    .title(title)
    .resizable(resizable)
    .fullscreen(fullscreen)
    .url(u)
    .onLoad(()->{
       //.. Do something on page load.
	   // You can get the url of the page via webview.url()
    })
    .javascriptCallback(message->{
        // Handle a message sent via window.external.invoke(message)
        // message is a string.
    })
    .show();

NOTE: The show() method will start a blocking event loop.

WARNING: Currently the WebView is picky about being started on the main application thread. On Mac you may need to add the "-XstartOnFirstThread" flag in the JVM.

Using Java API from Swing, JavaFX, or other UI Toolkit

The WebView class cannot be used from Swing, JavaFX, or any other existing UI toolkit because it starts its own event loop. If you want to make use of the WebView from within such an app, you'll need to use the WebViewCLIClient class, which provides an interface to create and manage a WebView which runs inside its own subprocess.

See the Swing Demo for a full example of this.

The basics are:


// Opening the webview
WebViewCLIClient webview = (WebViewCLIClient)new WebViewCLIClient.Builder()
    .url("https://www.codenameone.com")
    .title("Codename One")
    .size(800, 600)
    .build();
    
// Adding a load listener (fired whenever a page loads)
webview.addLoadListener(evt->{
    System.out.println("Loaded "+evt.getURL());
});

// Adding a message listener (fired whenever any js calls window.postMessageExt(msg))
webview.addMessageListener(evt->{
    System.out.println(evt.getMessage());
});

// Evaluate javascript on the current page.  Implicit callback() method
// allows you to return result in CompetableFuture.
webview.eval("callback(window.location.href)")
    .thenAccept(str->{
        System.out.println("Current URL is "+str);
    });
    
    

    
// Closing the webview later
webview.close();

Demos

  1. Swing Demo - A simple demo showing how to create and control a WebView from a Swing App.
  2. Minimal Demo - A simple demo that only launches a WebView on the main thread.

Supported Platforms

This should work on Mac, Linux, and Windows.

Building Sources

git clone https://github.com/shannah/webviewjar
cd webviewjar
ant jar

This will create dist/WebView.jar, which can be run as an executable jar.

Troubleshooting

ANT requires that the platforms.JDK_1.8.home system property is set to your JAVA_HOME. If it complains about this, you can fix the issue by changing the ant jar command, above, to ant jar -Dplatforms.JDK_1.8.home="$JAVA_HOME".

Rebuilding Native Libs

The repo comes with pre-built native libs in the src/windows_32, src/windows_64, src/osx_64, and src/linux_64. If you want to make changes to these native libs, then the following information may be of use to you.

  1. Use the build-xxx.sh (where xxx is your current platform) scripts to rebuild the native sources, and copy them into the appropriate place in the src directory.
  2. Mac and linux native sources are located in the src_c directory. Windows native sources are in the windows directory.
  3. On Windows, you'll need to have Visual Studio installed (I use VS 2019, but earlier versions probably work). Additionally, I use git bash on Windows, which is why the build-windows.sh is a bash script, and not a .bat script.

License

MIT

Credits

  1. This library created by Steve Hannah
  2. Original webview library by Serge Zaitsev

webviewjar's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

webviewjar's Issues

Can't open a page on localhost?

I have a server running on localhost:5000. On a browser I can access it at http://localhost:5000/index and it works just fine. Using the webview it never seems to load, all I see is a white page. I see no error logs either.

My code is just:

 public static void main(String[] args) {
        WebView wv = new WebView();
        wv.title("hello").url("http://localhost:5000/index").show();
    }

Does WebViewJar support Arm64 platform ?

I try to build it on unbuntu 64bit for arm64, and the hardware is raspberry PI 3B+. when I run build_linux.sh, I got the error such like Couldn't load library webview...

Updates?

Hi - I found the project, super cool. Noticed you haven't put out updates in a while. I made a few modifications on a local branch, including refactoring it to work with Maven (would like to push a version out to Maven central). FWIW seems to run fine on Big Sur with this initialization:

        WebView webview = new WebView()
                .size(800, 600)
                .title("Test")
                .resizable(true)
                .url("https://theoryofgeek.com/")
                .addJavascriptCallback("callback", x ->
                {
                    System.out.println(x);
                });
        webview.show();

I'd like to make a few tweaks to the API as well, e.g. instead of using finalize in WebappJniExtractor switch to a try-with-resources instead. Also add a bunch of additional test cases and perhaps some GitHub Actions to verify everything works on macOS, Windows. Probably update to the latest version of the core lib as well.

Would you prefer I submit this stuff to your repo as PRs, or just fork and run with it, or...?

java.lang.UnsatisfiedLinkError

When running your example, I get this error:

C:\Users\CDRSP\Downloads\webviewjar-master\webviewjar-master\bin>java -jar WebVi
ew.jar "data:text/html,%3Chtml%3Ehello%3C%2Fhtml%3E"

abr 22, 2020 6:14:55 PM ca.weblite.webview.nativelib.NativeLibraryUtil error
WARNING: Problem with library
java.lang.UnsatisfiedLinkError: C:\Users\CDRSP\AppData\Local\Temp\nativelib-load
er_7045038884957253917\webview.dll: Can't find dependent libraries
at java.lang.ClassLoader$NativeLibrary.load(Native Method)
at java.lang.ClassLoader.loadLibrary0(Unknown Source)
at java.lang.ClassLoader.loadLibrary(Unknown Source)
at java.lang.Runtime.load0(Unknown Source)
at java.lang.System.load(Unknown Source)
at ca.weblite.webview.nativelib.NativeLibraryUtil.loadNativeLibrary(Nati
veLibraryUtil.java:343)
at ca.weblite.webview.nativelib.NativeLoader.loadLibrary(NativeLoader.ja
va:139)
at ca.weblite.webview.WebViewNative.(WebViewNative.java:24)
at ca.weblite.webview.WebView.show(WebView.java:230)
at ca.weblite.webview.WebViewCLI.init(WebViewCLI.java:66)
at ca.weblite.webview.WebViewCLI.main(WebViewCLI.java:314)
abr 22, 2020 6:14:56 PM ca.weblite.webview.WebViewNative
SEVERE: null
java.io.IOException: Couldn't load library library webview
at ca.weblite.webview.nativelib.NativeLoader.loadLibrary(NativeLoader.ja
va:141)
at ca.weblite.webview.WebViewNative.(WebViewNative.java:24)
at ca.weblite.webview.WebView.show(WebView.java:230)
at ca.weblite.webview.WebViewCLI.init(WebViewCLI.java:66)
at ca.weblite.webview.WebViewCLI.main(WebViewCLI.java:314)
Caused by: java.lang.UnsatisfiedLinkError: C:\Users\CDRSP\Downloads\webviewjar-m
aster\webviewjar-master\bin\webview.dll: Can't find dependent libraries
at java.lang.ClassLoader$NativeLibrary.load(Native Method)
at java.lang.ClassLoader.loadLibrary0(Unknown Source)
at java.lang.ClassLoader.loadLibrary(Unknown Source)
at java.lang.Runtime.loadLibrary0(Unknown Source)
at java.lang.System.loadLibrary(Unknown Source)
at ca.weblite.webview.nativelib.NativeLoader.loadLibrary(NativeLoader.ja
va:136)
... 4 more

Exception in thread "main" java.lang.UnsatisfiedLinkError: ca.weblite.webview.We
bViewNative.webview_create(IJ)J
at ca.weblite.webview.WebViewNative.webview_create(Native Method)
at ca.weblite.webview.WebView.show(WebView.java:230)
at ca.weblite.webview.WebViewCLI.init(WebViewCLI.java:66)
at ca.weblite.webview.WebViewCLI.main(WebViewCLI.java:314)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.