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
Initializes a new instance of WebView, whether this WebView should use ipc mode or not.
Instance methods
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
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
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.
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.
Set WebView's fullscreen state, passing true makes the webview fullscreen, otherwise.
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.
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
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
Reloads current content of this WebView without re-using any cached data.
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.
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.
Get this WebView's UUID. This is helpful in case of communicating between Web Process and Render Process
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
Specifies a callback which will be called eachtime a IPC message is send from Render Process (e.g : via JavaScript)
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.
Set size of browser window
webview.window_size 800, 600