lorca

repository·master·Indexed 27 days ago

https://github.com/zserge/lorca

A lightweight Go library for building HTML5 desktop applications by leveraging an existing Chrome or Chromium installation (version 70+) via the Chrome DevTools Protocol. It enables seamless communication between Go and JavaScript, allowing developers to bind Go functions to the UI and execute JavaScript from Go without bundling a browser.

Tokens
2.5K
Snippets
6
Records
26
Agent score
93%

What's inside lorca

  1. Overview of Lorca

    master
    Lorca is a lightweight Go library used to build modern HTML5 desktop applications. It uses an existing installation of Chrome or Chromium as the UI layer instead of bundling a browser (unlike Electron), resulting in much smaller application sizes (typically 5-10MB). It enables seamless communication between Go and JavaScript, allowing you to call Go functions from the UI and execute arbitrary JavaScript from Go.
  2. Use the Lorca API to build a UI

    master

    Lorca provides a simple API to create windows, bind Go functions to JavaScript, and evaluate JavaScript code from Go.

    Key capabilities:

    • lorca.New(url, title, width, height): Creates a new window.
    • ui.Bind(name, function): Exposes a Go function to the JavaScript environment. In JS, this function is represented as a Promise and can handle long-running or blocking Go code.
    • ui.Eval(js_code): Executes arbitrary JavaScript in the browser window and returns a value object that can be cast to types like .Int() or .Float().
    • ui.Done(): A channel that closes when the browser window is closed.
    ui, _ := lorca.New("", "", 480, 320)
    defer ui.Close()
    
    // Bind Go function to be available in JS.
    // In JS it's represented with a Promise.
    ui.Bind("add", func(a, b int) int { return a + b })
    
    // Call JS function from Go.
    n := ui.Eval(`Math.random()`).Float()
    fmt.Println(n)
    
    // Call JS that calls the bound Go function
    m := ui.Eval(`add(2, 3)`).Int()
    fmt.Println(m)
    
    // Wait for the browser window to be closed
    <-ui.Done()
  3. Lorca requirements and limitations

    master

    Requirements

    • Chrome/Chromium >= 70 must be installed on the host system.

    Limitations

    • No window control: You cannot currently remove borders, make the window transparent, or control the window's position or size.
    • No window menu: Native window menus are not supported. For tray menus or native OS dialogs, you must use 3rd-party libraries.

    If you require more granular control over the browser window, consider using the webview library.

  4. Initialize a new UI with New()

    master

    Use New to create a new HTML5 UI instance. You can specify a starting URL, a user profile directory, and window dimensions.

    • If url is empty, a blank page is displayed.
    • If dir is empty, a temporary directory is created and automatically removed when ui.Close() is called.
    • You can pass additional Chrome CLI arguments as customArgs (e.g., --headless for testing).
  5. Generate a PDF of the current page

    master
    Use the pdf method to print the current page to a PDF buffer. The dimensions are specified in pixels and converted to inches (using a 96 DPI assumption) for the Chrome DevTools Protocol.
  6. Display message boxes on Linux and macOS

    master

    Lorca provides a mechanism to display a question dialog box to the user on non-Windows platforms. This function uses system-native tools: zenity on Linux and osascript (AppleScript) on macOS.

    Note: This implementation is specifically for non-Windows environments (//+build !windows).

    The dialog presents a question with 'Yes' and 'No' buttons. It returns true if the user selects 'Yes' and false otherwise.

  7. Convert a URL to PDF

    master

    The PDF function converts a given URL (which can be a local file) into a PDF byte slice. You can provide a script to be evaluated in the browser before the PDF is generated, allowing you to modify page content or wait for rendering to complete.

    For standard A4 page dimensions at 96dpi, use the PageA4Width and PageA4Height constants.

  8. Locate the Chrome binary with LocateChrome

    master

    Use LocateChrome() to find the path to a Chrome or Chromium executable on the system. It returns the path as a string, or an empty string if no installation is found.

    Lorca follows this search priority:

    1. The path specified in the LORCACHROME environment variable (if the file exists).
    2. Standard installation paths for macOS, Windows, and Linux (including Google Chrome, Chrome Canary, Chromium, and Microsoft Edge).
  9. Prompt the user to download Chrome with PromptDownload

    master
    If LocateChrome() returns an empty string, you can call PromptDownload() to show a message box asking the user if they want to download and install Chrome. If the user agrees, it opens the official Chrome download page (https://www.google.com/chrome/) using the system's default web opener (xdg-open on Linux, open on macOS, or start on Windows).