class WebView
- WebView
- Reference
- Object
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.crcomponents/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
-
#[]=(name : String, value)
Set properties for WebKitSettings.
-
#execute_javascript(js_code : String)
Asynchronously executes js_code.
-
#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
. -
#extension_dir : String
Get extension directory
-
#extension_dir=(directory : String)
Specifies directory where the webview should look for WebExtension.
-
#full_screen(state : Bool)
Set WebView's fullscreen state, passing
true
makes the webview fullscreen, otherwise. -
#go_back
Loads the previous web page.
-
#go_forward
Loads the next web page.
-
#ipc? : Bool
Specifies whether if
WebView
is running on IPC mode or not. -
#load_html(html : String, base_url : String | Nil = nil)
Loads raw html
-
#load_url(url : String)
Loads a webpage located at url
-
#on_close(&b : Callback)
Specifies callback called when browser window close.
-
#on_load_process_changed(&block : Float64 -> _)
Specifies a callback called each time
WebView
load progress changes. -
#reload
Reloads current content of this
WebView
. -
#reload_without_cache
Reloads current content of this
WebView
without re-using any cached data. -
#run
Runs browser window.
-
#run(blocking : Bool, &block : WebView -> Nil)
Run browser window.
-
#show_inspector
Shows WebKit's WebInspector.
-
#stop_ipc
Stops ipc listening on the
WebView
-
#title=(title : String)
Set title of browser window
-
#uuid : UUID
Get this
WebView
's UUID. -
#when_document_loaded(&b : WebView -> Nil)
Specifies a callback which will be called after webview has fully loaded a webpage.
-
#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)
-
#when_script_finished(&b : JSC::JSValue -> Nil)
Specifies a callback which will be called each time a script executed by
#execute_javascript
finishes. -
#window_size(width : UInt32, height : UInt32)
Set size of browser window
Constructor Detail
Instance Method Detail
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