class WebView

Overview

A WebView is a class which represents a WebKit2GTK browser window. It allows developers to display HTML contents, execute JavaScript code.

Defined in:

components/web_view.cr
components/web_view/dialog.cr
components/web_view/dialog_file_filter.cr
components/web_view/open_file_dialog.cr
components/web_view/save_file_dialog.cr
components/web_view/select_folder_dialog.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new(ipc : Bool = false) #

Initializes a new instance of WebView, whether this WebView should use ipc mode or not.


[View source]

Instance Method Detail

def []=(name : String, value) #

Set properties for WebKitSettings. See WebKitSettings.

webview["enable-developer-extras"] = true    # This allows developers to use WebInspector(DevTools)
webview["enable-html5-local-storage"] = true # This enables localStorage features in JavaScript

[View source]
def execute_javascript(js_code : String) #

Asynchronously executes js_code. After js_code has been successfully executed, the callback passed to #when_script_finished will be called.

webview.when_document_loaded |w|
  w.execute_javascript "alert('LOL')"
end

[View source]
def execute_javascript(js_code : String, &block : JSC::JSValue -> Nil) #

Asynchronously executes js_code, block will be called instead of the callback passed to #when_script_finished.

This method is useful in situations in which one needs to evaluate returned value of specific JavaScript code, for example:

webview.when_document_loaded do
  webview.execute_javascript "document.title" do |value|
    webview.title = String.new(JSC.to_string value)
  end
end

NOTE block receives a Pointer(Void) as its argument, so it's considered unsafe.


[View source]
def extension_dir : String #

Get extension directory


[View source]
def extension_dir=(directory : String) #

Specifies directory where the webview should look for WebExtension.

webview = WebView.new
webview.extension_dir = "<your-path-to-directory-which-contains-web-extensions>"

NOTE If this method is used, it should be executed as soon as possible. A good practice is to place it right after .new.


[View source]
def full_screen(state : Bool) #

Set WebView's fullscreen state, passing true makes the webview fullscreen, otherwise.


[View source]
def go_back #

Loads the previous web page.


[View source]
def go_forward #

Loads the next web page.


[View source]
def ipc? : Bool #

Specifies whether if WebView is running on IPC mode or not.


[View source]
def load_html(html : String, base_url : String | Nil = nil) #

Loads raw html

If base_url is a String, all relative paths in html will be resolved against base_url. All absolute paths will also have to start with base_url because of security reasons. If not, Web Process might crash.


[View source]
def load_url(url : String) #

Loads a webpage located at url

webview.load_url "https://google.com"

[View source]
def on_close(&b : Callback) #

Specifies callback called when browser window close. Usually useds to close process.

webview.on_close do |webview|
  puts "WebView(#{webview}) is going to shutdown"
  exit 0
end

[View source]
def on_load_process_changed(&block : Float64 -> _) #

Specifies a callback called each time WebView load progress changes.

E.g:

webview.on_load_process_changed do |progress|
  # progress ranges from 0.0 to 1.0
  puts "Loading #{(progress * 100).round(2).colorize(:green)}"
end

[View source]
def reload #

Reloads current content of this WebView.


[View source]
def reload_without_cache #

Reloads current content of this WebView without re-using any cached data.


[View source]
def run #

Runs browser window. This method puts the main process into a loop which blocks until callback specified in #on_close called.

webview.run
puts "Good bye" # This line will only be executed after webview main loop is destroyed.

[View source]
def run(blocking : Bool, &block : WebView -> Nil) #

Run browser window. This method receives a block which lets developers do some logics at each webview's iteration. If blocking is truthy, block will only be called when an iteration is done.

count = 0
webview.run blocking: false do |webview|
#If blocking: set to `true`. This printing process will be significally slowed down.
#This is because `#run(blocking,&block)` has to wait for the current iteration finish.
  puts a
  a += 1
end

NOTE IMPORTANT : Uses this method carefully when WebView is running on IPC mode. Some concurrent operations such as Channel#receive may cause the WebView blocked infinitely.


[View source]
def show_inspector #

Shows WebKit's WebInspector.


[View source]
def stop_ipc #

Stops ipc listening on the WebView


[View source]
def title=(title : String) #

Set title of browser window

webview.title = "Alizarin App"

[View source]
def uuid : UUID #

Get this WebView's UUID. This is helpful in case of communicating between Web Process and Render Process


[View source]
def when_document_loaded(&b : WebView -> Nil) #

Specifies a callback which will be called after webview has fully loaded a webpage. This is equal to JQuery's $(window).ready. This method is the right place to do some logic, for example #execute_javascript.

webview.on_document_loaded do |webview|
  webview.execute_javascript "alert('Hello')"
end

[View source]
def when_ipc_message_received(&block : String -> Nil) #

Specifies a callback which will be called eachtime a IPC message is send from Render Process (e.g : via JavaScript)


[View source]
def when_script_finished(&b : JSC::JSValue -> Nil) #

Specifies a callback which will be called each time a script executed by #execute_javascript finishes. The callback b receives a Pointer(Void) as parameter that points to result of the executed JS code. For example:

webview.when_script_finished do |js_value|
  puts JSC.is_number jsc_value
end
webview.execute_javascript "1 + 2"          # => js_value in the block above should be true
webview.execute_javascript "alert('Hello')" # => js_value should be false

NOTE Because the passed block receives a Pointer as its parameter, it's considered unsafe. It's not recommended to manipulate this pointer directly using JavaScriptCore API. Uses WebExtension instead.


[View source]
def window_size(width : UInt32, height : UInt32) #

Set size of browser window

webview.window_size 800, 600

[View source]