DOM library for Go and WASM

Go DOM binding (and more) for WebAssembly

This library provides a Go API for different Web APIs for WebAssembly target.

It's in an active development, but an API will be carefully versioned to avoid breaking users. Use Go dependency management tools to lock a specific version.

More information about Go's WebAssembly support can be found on Go's WebAssembly wiki page.

Features:

  • Better JS API (wrappers for syscall/js)
  • Basic DOM manipulation, styles, events
  • Input elements
  • SVG elements and transforms
  • LocalStorage and SessionStorage
  • Extension APIs (tested on Chrome):
    • Native Messaging
    • Bookmarks
    • Tabs
  • net-like library for WebSockets
    • Tested with gRPC
  • wasm-server for fast prototyping

Quickstart

Pull the library and install wasm-server (optional):

go get -u github.com/dennwc/dom
go install github.com/dennwc/dom/cmd/wasm-server

Run an example app:

cd $GOPATH/src/github.com/dennwc/dom
wasm-server

Check result: http://localhost:8080/

The source code is recompiled on each page refresh, so feel free to experiment!

Similar Projects

Editor Configuration

If you are using Visual Studio Code, you can use workspace settings to configure the environment variables for the go tools.

Your settings.json file should look something like this:

{
    "go.toolsEnvVars": { "GOARCH": "wasm", "GOOS": "js" }
}
Owner
Comments
  • Change method signature to js.Class(class string, path ...string)

    Change method signature to js.Class(class string, path ...string)

    Both JavaScript and TypeScript allow access to global objects via dotted notation. At the same time, many libraries wrap their objects, classes and methods to avoid adding them to the global namespace.

    Allowing dotted notation in js.Get() would simplify accessing wrapped types and would automatically supplement both js.Class() and js.New(). The following code works:

    mdc := js.Get("mdc")
    chips := mdc.Get("chips")
    chipset := chips.Get("MDCChipSet")
    chipsetEl := dom.Doc.CreateElement("div")
    chipsetEl.SetAttribute("class", "mdc-chip-set")
    chipset.Call("attachTo", chipsetEl)
    

    But this code is much simpler to write and has the added benefit of using the js.Class() caching:

    chipset := chips.Class("mdc.chips.MDCChipSet")
    chipsetEl := dom.Doc.CreateElement("div")
    chipsetEl.SetAttribute("class", "mdc-chip-set")
    chipset.Call("attachTo", chipsetEl)
    
  • Getting an element value ?

    Getting an element value ?

    Trying to do the same thing as this issue => #6 but I think I still miss something ? In my situation, I have input fields inside a form, can you provide a way to manipulate forms ? I noticed that looping over the nodeList we got more elements than the actual form ? Any insight on that ?

    form := dom.Doc.GetElementById("custom_form")
    btn := dom.Doc.GetElementById("btn")
    t := document.Call("getElementById", "target")
    logger := log.New((*writer)(&t), "", log.LstdFlags)
    
    btn.AddEventListener("click", func(_ dom.Event) {
    	nodes := form.ChildNodes()
    	for _, element := range nodes {
    		logger.Print("ID: " + string(element.Id()) + " Value: " + string(element.JSValue().Get("value").String()))
    	}
    })
    
  • Build strings breaks godoc

    Build strings breaks godoc

    It is useful to be able to read the documentation on godoc.

    https://godoc.org/github.com/dennwc/dom

    Unfortunately the build strings in this project hide the code from the godoc system, which presumably wants to build for linux.

    //+build wasm,js
    

    This doesn't seem to be an issue for other projects. Do we really need the build strings?

    https://godoc.org/honnef.co/go/js/dom

  • error when following instructions in README

    error when following instructions in README

    I followed the instructions in the readme and I got this error(?)

    element.go:6:2: build constraints exclude all Go files in /usr/local/Cellar/go/1.11.4/libexec/src/syscall/js
    

    when running go get -u github.com/dennwc/dom

    this is my first attempt at playing with wasm so IDK if something in my environment is off. does GOOS or GOARCH need to be set for any of these commands?

  • Getting element values?

    Getting element values?

    How does one get element values with this package?

    I tried calling myElement.GetAttribute("value"), but that ended up returning an empty value, rather than the value of my input field.

    Is this something that needs additional implementation? Happy to PR something if that's the case.

  • Cannot make it to work with go modules

    Cannot make it to work with go modules

    Hello, quick note: I'm go newbie and I've never encountered similar error with other go packages. I'm trying to use this package using go modules (go.mod). Unfortunately, it seems like only color.go file is visible. Here are my project files:

    go.mod:

    module goasmf
    
    go 1.12
    
    require github.com/dennwc/dom v0.3.0
    

    main.go:

    package goasmf
    
    import (
        "fmt"
        "github.com/dennwc/dom"
    )
    
    func main() {
        println("Testing WebAsm!")
        var color dom.Color = "red"
        dom.GetWindow()
    }
    

    go build output:

    tomek$ go build
    # goasmf
    ./main.go:13:2: undefined: dom.GetWindow
    

    Working without modules and downloading dependency using go get -t github.com/dennwc/dom works fine, though

  • go 1.12 breaking changes

    go 1.12 breaking changes

    As detailed here: https://golang.org/doc/go1.12#syscall/js

    The changes made to syscall/js have now broken certain parts of the library.

    I'd like to know wether or not this is something you are working on. If not, I'm happy to start looking into it and helping out where able.

  • Confusion regarding the Get & Set functions for js.go

    Confusion regarding the Get & Set functions for js.go

    Hi! I saw Value{global} being used and global being used for the get & set function. Are they interchangeable?

    // Get is a shorthand for Global().Get().
    func Get(name string, path ...string) Value {
    	return Value{global}.Get(name, path...)
    }
    
    // Set is a shorthand for Global().Set().
    func Set(name string, v interface{}) {
    	global.Set(name, toJS(v))
    }
    
  • Possibly add instructions for getting autocomplete working in vscode?

    Possibly add instructions for getting autocomplete working in vscode?

    After a little struggle I was able to get autocomplete working in VS code by setting some workspace level environment variables. I'm not sure how well known that is. Do you think it is worth adding to the README in this project?

    I will gladly open a PR, but if you don't think this is the right place that is understandable.

  • Add NodeType() function to NodeBase type

    Add NodeType() function to NodeBase type

    Needed to properly iterate over just child elements as desired in #38.

    See https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeType for enumerated node types.

  • Avoid non-json content conflict in storage on cmd/app/main.go

    Avoid non-json content conflict in storage on cmd/app/main.go

    Ensure that cmd/app/main.go uses a prefix in storage to avoid conflict with non-json content

    Closes #36

    Signed-off-by: Ricardo Seriani [email protected]

  • Is this project dead?

    Is this project dead?

    There are now two forks on Github that have on-going maintenance while there's not really any activity here ... should I switch to one of the maintained projects? As you've seen from my previous PRs, I'm not afraid to help with this project but you'd need to be around to at least merge (or reject) the PRs.

  • Improve WebSocket API

    Improve WebSocket API

    The net.Conn interface doesn't match how WebSockets work as it's not a stream oriented protocol.

    See https://github.com/golang/go/issues/18152#issuecomment-264322154 and https://github.com/gopherjs/websocket/issues/29

    I think it'd be a good idea to recommend my library instead to avoid duplicate work. It supports compiling to Wasm and provides a more semantically appropriate API (while also providing a net.Conn adapter) or the API in this library should be changed.

  • compile failure with go 1.13

    compile failure with go 1.13

    If compiling with Go 1.13, a compiling error comes up:

    github.com/dennwc/dom/js

    ../../../../go/pkg/mod/github.com/dennwc/[email protected]/js/bytes.go:20:2: undefined: js.TypedArray ../../../../go/pkg/mod/github.com/dennwc/[email protected]/js/bytes.go:30:7: undefined: js.TypedArrayOf

    The change log (https://golang.org/doc/go1.13#syscall/js) states that the types have been changed: "TypedArrayOf has been replaced by CopyBytesToGo and CopyBytesToJS for copying bytes between a byte slice and a Uint8Array."

  • Simplify signatures in js.go

    Simplify signatures in js.go

    In PR #41 I talked about simplifying the signatures of several methods in js.go but rejected that as I thought it would be a breaking change. Since then, I've realized that because the first parameter is the same type as the second variadic parameter, the change is not in fact breaking and simplifies the signature of the end-user API.

    I'd suggest that the following three method signatures be changed below:

    • From Get(name string, path ...string) to Get(name ...string
    • From Class(class string, path ...string) to Class(class ...string)
    • From (v Value) Get(name string, path ...string) to (v Value) Get(name ...string)

    The change to the Class method slightly simplifies the key generation but otherwise the code is pretty much the same. The main benefit is that, having a single parameter, it makes more sense when a nested class is specified. Consider Class("mdc", "chips", "MDCChipSet") as described in my previous example - with the original signature, the class parameter would contain mdc but the actual class (MDCChipSet) being loaded would be the second element of the path slice. With the revised signature, the elements make sense in the order they're provided.

    I do realize this is a completely cosmetic change but wanted to present the idea prior to the library reaching 1.0. If it sounds reasonable, I've got a branch I used to test that this could be done simply - assign this to me and I'll clean up that branch and submit a PR.

Go Lang Web Assembly bindings for DOM, HTML etc

WebAPI Go Language Web Assembly bindings for DOM, HTML etc WARNING: The current API is in very early state and should be consider to be expremental. T

Dec 28, 2022
Dom - A Go API for different Web APIs for WebAssembly target

Go DOM binding (and more) for WebAssembly This library provides a Go API for dif

Jan 7, 2023
Library to use HTML5 Canvas from Go-WASM, with all drawing within go code

go-canvas go-canvas is a pure go+webassembly Library for efficiently drawing on a html5 canvas element within the browser from go without requiring ca

Dec 11, 2022
Golang-WASM provides a simple idiomatic, and comprehensive API and bindings for working with WebAssembly for Go and JavaScript developers
Golang-WASM provides a simple idiomatic, and comprehensive API and bindings for working with WebAssembly for Go and JavaScript developers

A bridge and bindings for JS DOM API with Go WebAssembly. Written by Team Ortix - Hamza Ali and Chan Wen Xu. GOOS=js GOARCH=wasm go get -u github.com/

Dec 22, 2022
Run WASM tests inside your browser

wasmbrowsertest Run Go wasm tests easily in your browser. If you have a codebase targeting the wasm platform, chances are you would want to test your

Dec 16, 2022
An Experimental Wasm Virtual Machine for Gophers

gasm A minimal implementation of v1 WASM spec compatible virtual machine purely written in go. The vm can be embedded in your go program without any d

Dec 31, 2022
Go Wasm is a in-browser IDE for Go

Go Wasm Go Wasm is a Go development environment with the essentials to write and run code entirely within the browser, using the power of WebAssembly

Jan 8, 2023
A WASM Filter for Envoy Proxy written in Golang

envoy-proxy-wasm-filter-golang A WASM Filter for Envoy Proxy written in Golang Build tinygo build -o optimized.wasm -scheduler=none -target=wasi ./mai

Nov 6, 2022
Istio wasm api demo with golang

istio-wasm-api-demo 1. Setup the latest Istio Setup k8s cluster: e.g. kind create cluster --name test Download the latest Istioctl from the GitHub rel

Nov 1, 2022
Fast face detection, pupil/eyes localization and facial landmark points detection library in pure Go.
Fast face detection, pupil/eyes localization and facial landmark points detection library in pure Go.

Pigo is a pure Go face detection, pupil/eyes localization and facial landmark points detection library based on Pixel Intensity Comparison-based Objec

Dec 24, 2022
Vugu: A modern UI library for Go+WebAssembly (experimental)

Vugu: A modern UI library for Go+WebAssembly (experimental)

Jan 3, 2023
Interact with browser from Go. Manually-crafted WebAPI interoperation library.

GWeb: golang + js + wasm gweb -- strictly typed WebAPI library on top of syscall/js. Like flow or TypeScript but for Go. You need it if you want to in

Sep 26, 2022
The note-tinygo Go library for communicating with Blues Wireless Notecard via serial or I²C

Blues Wireless The note-tinygo Go library for communicating with Blues Wireless Notecard via serial or I²C. This library allows you to control a Notec

Nov 29, 2021
This library provides WebAssembly capability for goja Javascript engine

This module provides WebAssembly functions into goja javascript engine.

Jan 10, 2022
Go compiler for small places. Microcontrollers, WebAssembly, and command-line tools. Based on LLVM.

TinyGo - Go compiler for small places TinyGo is a Go compiler intended for use in small places such as microcontrollers, WebAssembly (Wasm), and comma

Dec 30, 2022
WebAssembly interop between Go and JS values.

vert Package vert provides WebAssembly interop between Go and JS values. Install GOOS=js GOARCH=wasm go get github.com/norunners/vert Examples Hello W

Dec 28, 2022
A package to build progressive web apps with Go programming language and WebAssembly.
A package to build progressive web apps with Go programming language and WebAssembly.

Go-app is a package for building progressive web apps (PWA) with the Go programming language (Golang) and WebAssembly (Wasm). Shaping a UI is done by

Dec 30, 2022
A package to build progressive web apps with Go programming language and WebAssembly.
A package to build progressive web apps with Go programming language and WebAssembly.

Go-app is a package for building progressive web apps (PWA) with the Go programming language (Golang) and WebAssembly (Wasm). Shaping a UI is done by

Jan 2, 2023
⚙️ Concept of Golang HTML render engine with frontend components and dynamic behavior
⚙️ Concept of Golang HTML render engine with frontend components and dynamic behavior

An HTML render engine concept that brings frontend-like components experience to the server side with native html/template on steroids. Supports any s

Nov 25, 2022