class

WebView

Inherits Reference / Object

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

Constructors

new(ipc : Bool = false)

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

Source

Instance methods

[]=(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
Source
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
Source
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.

Source
extension_dir

Get extension directory

Source
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.

Source
full_screen(state : Bool)

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

Source
go_back

Loads the previous web page.

Source
go_forward

Loads the next web page.

Source
ipc?

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

Source
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.

Source
load_url(url : String)

Loads a webpage located at url

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

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
Source
on_load_process_changed

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
Source
reload

Reloads current content of this WebView.

Source
reload_without_cache

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

Source
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.
Source
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.

Source
show_inspector

Shows WebKit's WebInspector.

Source
stop_ipc

Stops ipc listening on the WebView

Source
title=(title : String)

Set title of browser window

webview.title = "Alizarin App"
Source
uuid

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

Source
when_document_loaded

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
Source
when_ipc_message_received

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

Source
when_script_finished

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.

Source
window_size(width : UInt32, height : UInt32)

Set size of browser window

webview.window_size 800, 600
Source

Nested types