Module jls.util.WebView

Displays HTML content in a dedicated window.

The WebView highly depends on the underlying OS. Opening multiple WebView windows is not supported.

A webview requires a thread to run its own event loop, which is not compatible with the base event loop. This class provide helpers to start webview in a dedicated thread so that the base event loop can be used.

Class WebView

WebView:new (url[, options]) Creates a new WebView.
webView:loop ([mode]) Processes the webview event loop.
webView:callback (cb) Registers the specified function to be called from the web page.
webView:eval (js) Evaluates the specified JavaScript code in the web page.
webView:fullscreen (fullscreen) Sets the webview fullscreen.
webView:title (title) Sets the webview title.
webView:terminate () Terminates the webview.
webView:getChannel () Returns the channel associated to this webview or nil.
webView:getThread () Returns the thread associated to this webview or nil.
webView:getHttpServer () Returns the HTTP server associated to this webview or nil.
WebView.openSync (url[, options]) Opens the specified URL in a new window and returns when the window has been closed.
WebView.open (url[, options]) Opens the specified URL in a new window.
WebView.toDataUrl (content[, mediaType]) Returns an URL representing the specified content.


Class WebView

The WebView class.
WebView:new (url[, options])
Creates a new WebView. The URL accepts the data, file and http schemes.

Parameters:

  • url string the URL of the resource to be viewed.
  • options A table describing the WebView options.
    • title string the title of the window. (optional)
    • width number the width of the opened window. (optional)
    • height number the height of the opened window. (optional)
    • resizable boolean true if the opened window could be resized. (optional)
    • debug boolean true to enable devtools. (optional)

Usage:

    local WebView = require('jls.util.WebView')
    local webview = WebView:new(WebView.toDataUrl('<html><body>It works!</body></thread>'))
    webview:loop()
webView:loop ([mode])
Processes the webview event loop. This function will block. If you need to use the event loop in a callback then use the open function.

Parameters:

  • mode string the loop mode, default, once or nowait. (optional)
webView:callback (cb)
Registers the specified function to be called from the web page. The JavaScript syntax is window.external.invoke("string value");

Parameters:

  • cb function The callback to register.
webView:eval (js)
Evaluates the specified JavaScript code in the web page.

Parameters:

  • js string The JavaScript code to evaluate.
webView:fullscreen (fullscreen)
Sets the webview fullscreen.

Parameters:

  • fullscreen boolean true to switch the webview to fullscreen.
webView:title (title)
Sets the webview title.

Parameters:

  • title string The webview title to set.
webView:terminate ()
Terminates the webview.
webView:getChannel ()
Returns the channel associated to this webview or nil.

Returns:

    jls.util.Channel the channel associated to this webview or nil.
webView:getThread ()
Returns the thread associated to this webview or nil.

Returns:

    jls.util.Thread the thread associated to this webview or nil.
webView:getHttpServer ()
Returns the HTTP server associated to this webview or nil.

Returns:

    jls.net.http.HttpServer the HTTP server associated to this webview or nil.
WebView.openSync (url[, options])
Opens the specified URL in a new window and returns when the window has been closed. Passing a function will block the event loop not this function.

Parameters:

  • url string the URL of the resource to be viewed.
  • options A table describing the WebView options.
    • title string the title of the window. (optional)
    • width number the width of the opened window. (optional)
    • height number the height of the opened window. (optional)
    • resizable boolean true if the opened window could be resized. (optional)
    • debug boolean true to enable devtools. (optional)
    • fn function a function to be called in a dedicated thread, requires the event loop. (optional)
    • data string the data to be passed to the function as a string. (optional)

Returns:

    jls.lang.Promise a promise that resolve when the webview is closed or nil.

Usage:

    local WebView = require('jls.util.WebView')
    WebView.openSync('https://www.lua.org/')
WebView.open (url[, options])
Opens the specified URL in a new window. Opening a webview in a dedicated thread may not be supported on all platform. You could specify an HTTP URL with a port to 0 to indicate that an HTTP server should be started on a free port.

Parameters:

  • url string the URL of the resource to be viewed.
  • options A table describing the WebView options.
    • title string the title of the window. (optional)
    • width number the width of the opened window. (optional)
    • height number the height of the opened window. (optional)
    • resizable boolean true if the opened window could be resized. (optional)
    • debug boolean true to enable devtools. (optional)
    • fn function a function to be called in the webview context or true to indicate that no callback will be used. (optional)
    • data string the data to be passed to the function as a string. (optional)
    • contexts table the HTTP contexts to create. (optional)
    • bindAny boolean true to bind on any rather than the specified host name. (optional)
    • callback boolean true to enable the callback on HTTP using WebSocket. (optional)

Returns:

    jls.lang.Promise a promise that resolve when the webview is available.

Usage:

    local WebView = require('jls.util.WebView')
    local FileHttpHandler = require('jls.net.http.handler.FileHttpHandler')
    WebView.open('http://localhost:0/index.html', {
      contexts = {
        ['/(.*)'] = FileHttpHandler:new('htdocs')
      }
    }):next(function(webview)
      local httpServer = webview:getHttpServer()
      print('HTTP Server bound on port '..tostring(select(2, httpServer:getAddress())))
    end)
WebView.toDataUrl (content[, mediaType])
Returns an URL representing the specified content.

Parameters:

  • content string the HTML content to convert.
  • mediaType string the media type, default is text/html. (optional)

Returns:

    string an URL representing the specified content.

Usage:

    local url = WebView.toDataUrl('<html><body>Hello</body></thread>'))
generated by LDoc 1.4.6