lorca
repository·master·Indexed 27 days ago
https://github.com/zserge/lorcaA 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.
What's inside lorca
- 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.
Run the Hello World example
masterTo run the basic 'counter' example included in the repository, navigate to the example directory and execute the following commands:
cd examples/counter go get go run ./Use the Lorca API to build a UI
masterLorca 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 aPromiseand 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()Lorca requirements and limitations
masterRequirements
- 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.
Navigate to a URL with Load()
masterUseLoadto navigate the current UI window to a specific URL.Initialize a new UI with New()
masterUse
Newto create a new HTML5 UI instance. You can specify a starting URL, a user profile directory, and window dimensions.- If
urlis empty, a blank page is displayed. - If
diris empty, a temporary directory is created and automatically removed whenui.Close()is called. - You can pass additional Chrome CLI arguments as
customArgs(e.g.,--headlessfor testing).
- If
Generate a PDF of the current page
masterUse thepdfmethod 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.Execute JavaScript with Eval()
masterUseEvalto execute arbitrary JavaScript code within the UI context. It returns aValueobject which can be used to inspect the result of the execution.Display message boxes on Linux and macOS
masterLorca provides a mechanism to display a question dialog box to the user on non-Windows platforms. This function uses system-native tools:
zenityon Linux andosascript(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
trueif the user selects 'Yes' andfalseotherwise.Convert a URL to PDF
masterThe
PDFfunction converts a given URL (which can be a local file) into a PDF byte slice. You can provide ascriptto 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
PageA4WidthandPageA4Heightconstants.Locate the Chrome binary with LocateChrome
masterUse
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:
- The path specified in the
LORCACHROMEenvironment variable (if the file exists). - Standard installation paths for macOS, Windows, and Linux (including Google Chrome, Chrome Canary, Chromium, and Microsoft Edge).
- The path specified in the
Prompt the user to download Chrome with PromptDownload
masterIfLocateChrome()returns an empty string, you can callPromptDownload()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-openon Linux,openon macOS, orstarton Windows).