jsGo

package module
v0.0.0-...-4776c0b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 12, 2026 License: MIT Imports: 1 Imported by: 0

README

Say Thanks!

DISCLAIMER!!!: THIS MODULE IS STILL IN ITS EARLY DEVELOPMENT, SO USE AT YOUR OWN RISK!

jsGo

The jsGo package is a collection of convenience functions and variables to make working with the standard Go syscall/js package in both GopherJS and Go/TinyGo WASM a bit more natural. For the most part, most code should be easily transferable from GopherJS to Go/TinyGo WASM, and vice versa, with minimal changes needed, if any.

For Go/TinyGo WASM, jsGo requires the "glue code" to be loaded so that it can use the defined Go class when instantiating the WASM module, and also so that the WASM module can make call-outs to it should it need to access the JS APIs outside of the WASM sandbox.

To import jsGo into your project:
go get github.com/ScriptTiger/jsGo
Then just import "github.com/ScriptTiger/jsGo" and get started with using its functions and variables.

Please refer to the dev package docs and reference implementation for more details and ideas on how to integrate jsGo into your project.

Dev package docs:
https://pkg.go.dev/github.com/ScriptTiger/jsGo

Reference implementation:
https://github.com/ScriptTiger/jsGo/blob/main/ref

Other projects using jsGo

TigerChat:
https://github.com/ScriptTiger/TigerChat

TigerShare:
https://github.com/ScriptTiger/TigerShare

More About ScriptTiger

For more ScriptTiger scripts and goodies, check out ScriptTiger's GitHub Pages website:
https://scripttiger.github.io/

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// Global aliases
	Global = js.Global()
	Set    = Global.Set
	Get    = Global.Get
	Call   = Global.Call

	// Global property aliases
	DevicePixelRatio = Get("devicePixelRatio")
	InnerHeight      = Get("innerHeight")
	InnerWidth       = Get("innerWidth")
	OuterHeight      = Get("outerHeight")
	OuterWidth       = Get("outerWidth")

	// Constructor aliases
	Array           = Get("Array")
	AudioContext    = Get("AudioContext")
	Blob            = Get("Blob")
	Date            = Get("Date")
	Error           = Get("Error")
	FileReader      = Get("FileReader")
	Number          = Get("Number")
	Object          = Get("Object")
	Response        = Get("Response")
	String          = Get("String")
	TextDecoder     = Get("TextDecoder")
	TextEncoder     = Get("TextEncoder")
	Uint8Array      = Get("Uint8Array")
	URL             = Get("URL")
	URLSearchParams = Get("URLSearchParams")

	// BOM namespace object aliases
	Crypto      = Get("crypto")
	History     = Get("history")
	IndexedDB   = Get("indexedDB")
	Intl        = Get("Intl")
	IDBKeyRange = Get("IDBKeyRange")
	Location    = Get("location")
	Math        = Get("Math")
	Navigator   = Get("navigator")
	Performance = Get("performance")
	Screen      = Get("screen")
	Subtle      = Crypto.Get("subtle")
	WebAssembly = Get("WebAssembly")

	// DOM aliases
	Document = Get("document")
	Head     = Document.Get("head")
	Body     = Document.Get("body")

	// Method aliases
	Alert              = Get("alert").Invoke
	Atob               = Get("atob").Invoke
	Btoa               = Get("btoa").Invoke
	ClearInterval      = Get("clearInterval").Invoke
	ClearTimeout       = Get("clearTimeout").Invoke
	Fetch              = Get("fetch").Invoke
	GetRandomValues    = Crypto.Get("getRandomValues").Invoke
	Log                = Get("console").Get("log").Invoke
	MatchMedia         = Get("matchMedia").Invoke
	ParseInt           = Get("parseInt").Invoke
	SetInterval        = Get("setInterval").Invoke
	SetTimeout         = Get("setTimeout").Invoke
	ShowSaveFilePicker = Get("showSaveFilePicker").Invoke

	// Convenience variables
	Params = URLSearchParams.New(Location.Get("search").String())
)

Functions

func Append

func Append(child js.Value)

func AppendChild

func AppendChild(child js.Value)

func CreateButton

func CreateButton(str string, onclick func()) (button js.Value)

Create a button element with an onclick callback

func CreateElement

func CreateElement(tag string) js.Value

func CreateElementNS

func CreateElementNS(ns, tag string) js.Value

func CreateLoadFileButton

func CreateLoadFileButton(text, accept string, multiple bool, onchange func(loadFiles js.Value)) (span js.Value)

Create a span element containing both a hidden input element of type file with an onchange callback and also a button to trigger it

func CreateSaveFileButton

func CreateSaveFileButton(text string, options map[string]any, saveFileCallback func(saveFile js.Value)) (button js.Value)

Create a button which calls showSaveFilePicker with a callback

func FNV1a32

func FNV1a32(data []byte) js.Value

32-bit variation of the FNV-1a non-cryptographic hashing algorithm which immediately/synchonously returns the hash as a JS ArrayBuffer

func FNV1a64

func FNV1a64(data []byte) js.Value

64-bit variation of the FNV-1a non-cryptographic hashing algorithm which immediately/synchonously returns the hash as a JS ArrayBuffer

func FuncOf

func FuncOf(fn func(args []js.Value) any) js.Func

Wrap a Go function so it can be used by JS

func GetElementById

func GetElementById(id string) js.Value

func HasOwn

func HasOwn(object js.Value, str string) bool

func IsError

func IsError(err js.Value) bool

func LoadJS

func LoadJS(src string, onload func()) (classic js.Value)

Load a classic JS script with an onload callback

func LoadWASM

func LoadWASM(str string, then func(), methods ...func(err js.Value))

Load a WASM module with a thenable chain

func Permissions

func Permissions(str string, permissionsCallback func(permissionStatus js.Value))

Takes a permission descriptor, such as "camera", "microphone", "geolocation", etc., and a callback to handle the returned PermissionStatus object

func Prepend

func Prepend(child js.Value)

func ProcOf

func ProcOf(fn func(args []js.Value)) js.Func

Wrap a Go procedure so it can be used by JS

func SHA256

func SHA256(data []byte, shaCallback func(hash js.Value))

256-bit variation of SHA-2 which takes the data byte slice and callback to asynchonrously handle the hash as a JS ArrayBuffer

func SHA384

func SHA384(data []byte, shaCallback func(hash js.Value))

384-bit variation of SHA-2 which takes the data byte slice and callback to asynchonrously handle the hash as a JS ArrayBuffer

func SHA512

func SHA512(data []byte, shaCallback func(hash js.Value))

512-bit variation of SHA-2 which takes the data byte slice and callback to asynchonrously handle the hash as a JS ArrayBuffer

func SetFunc

func SetFunc(str string, fn func(args []js.Value) any) js.Func

Expose a Go function to JS globally

func SetProc

func SetProc(str string, fn func(args []js.Value)) js.Func

Expose a Go procedure to JS globally

func SetSimpleFunc

func SetSimpleFunc(str string, fn func() any) js.Func

Expose a Go function which has no arguments to JS globally

func SetSimpleProc

func SetSimpleProc(str string, fn func()) js.Func

Expose a Go procedure which has no arguments to JS globally

func SimpleFuncOf

func SimpleFuncOf(fn func() any) js.Func

Wrap a Go function which has no arguments so it can be used by JS

func SimpleProcOf

func SimpleProcOf(fn func()) js.Func

Wrap a Go procedure which has no arguments so it can be used by JS

func ThenableChain

func ThenableChain(thenable js.Value, thenFunc func(arg js.Value) any, funcs ...func(arg js.Value)) any

Function to handle the then, catch, and finally methods for a given thenable's thenable chain

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL