func launch(mga *Agent) { js.Global.Set("agent", js.MakeWrapper(mga)) if isBackgroundPage() { log.Printf("Starting agent") js.Global.Get("chrome"). Get("runtime"). Get("onConnectExternal"). Call("addListener", func(port *js.Object) { p := NewAgentPort(port) go agent.ServeAgent(mga, p) }) } else if isOptionsPage() { js.Global.Get("document"). Call("addEventListener", "DOMContentLoaded", func() { go func() { textarea := js.Global.Get("document"). Call("getElementById", "keys") textarea.Set("textContent", mga.PubKeys()) textarea.Get("style").Set("height", textarea.Get("scrollHeight").String()+"px") textarea.Call("addEventListener", "click", func() { textarea.Call("focus") textarea.Call("select") }) }() }) } }
func GetPlayback(base64Playback string) *js.Object { var p engine.Playback buf := bytes.NewReader([]byte(base64Playback)) r := base64.NewDecoder(base64.StdEncoding, buf) gob.NewDecoder(r).Decode(&p) return js.MakeWrapper(&p) }
// Js creates a JS wrapper object for this promise that includes the 'then' // method required by the Promises/A+ spec. func (p *Promise) Js() *js.Object { o := js.MakeWrapper(p) o.Set("then", func(success, failure *js.Object) *js.Object { return p.Then(jsCallback(success), jsCallback(failure)).Js() }) return o }
func GetReplay(url string) *js.Object { resp, _ := http.Get(url) defer resp.Body.Close() d, _ := ioutil.ReadAll(resp.Body) msg, _ := capnp.Unmarshal(d) r, _ := botapi.ReadRootReplay(msg) return js.MakeWrapper(engine.NewReplay(r)) }
func TestMakeWrapper(t *testing.T) { m := &M{42} if !js.Global.Call("eval", `(function(m) { return m.Method({x: 1})["y"] === "z"; })`).Invoke(js.MakeWrapper(m)).Bool() { t.Fail() } if js.MakeWrapper(m).Interface() != m { t.Fail() } f := func(m *M) { if m.f != 42 { t.Fail() } } js.Global.Call("eval", `(function(f, m) { f(m); })`).Invoke(f, js.MakeWrapper(m)) }
func (d *Domain) LinkDomain(name string) *js.Object { n := Domain{ coreDomain: d.coreDomain.LinkDomain(name), app: d.app, } n.wrapped = js.MakeWrapper(&n) return n.wrapped }
func main() { srv := NewServer() srv.RegisterModule(natural.Module) srv.RegisterModule(nlparser.Module) must(srv.EnableModule("natural")) must(srv.EnableModule("nlparser")) js.Global.Set("SarifServer", js.MakeWrapper(srv)) }
func (s *Server) NewSocketConn() *js.Object { conn := s.Broker.NewLocalConn() sock := &Socket{conn: conn} sock.object = js.MakeWrapper(sock) sock.object.Set("send", sock.object.Get("Send")) sock.object.Set("readyState", 0) go sock.readLoop() return sock.object }
func validate(input *js.Object) *js.Object { data := js.Global.Get("JSON").Call("stringify", input).String() var obj interface{} if err := json.Unmarshal([]byte(data), &obj); err != nil { panic(err) } result := jsonapivalidator.Validate(obj) return js.MakeWrapper(newJSResult(result)) }
func loginSaved(username, authData string, appkey string, cb *js.Object) { go func() { key, _ := base64.StdEncoding.DecodeString(appkey) data, _ := base64.StdEncoding.DecodeString(authData) conn, _ := MakeConn() sController, err := spotcontrol.LoginConnectionSaved(username, data, key, "spotcontrol", conn) if err != nil { cb.Invoke(nil, "", "login failed") } c := &controllerWrapper{controller: sController} cb.Invoke(js.MakeWrapper(c), authData, nil) }() }
func login(username, password, appkey string, cb *js.Object) { go func() { key, _ := base64.StdEncoding.DecodeString(appkey) conn, _ := MakeConn() sController, err := spotcontrol.LoginConnection(username, password, key, "spotcontrol", conn) if err != nil { cb.Invoke(nil, "", "login failed") } else { authData := sController.SavedCredentials c := &controllerWrapper{controller: sController} cb.Invoke(js.MakeWrapper(c), base64.StdEncoding.EncodeToString(authData), nil) } }() }
func New(name string) *js.Object { core.ExternalGenerator = idGenerator{} a := &App{ registrations: make(map[uint64]*js.Object), subscriptions: make(map[uint64]*js.Object), } d := Domain{ coreDomain: core.NewDomain(name, nil), app: a, } d.wrapped = js.MakeWrapper(&d) return d.wrapped }
func init() { // Create a JavaScript wrapper function that the host can use to invoke the // HostInitialize function with a WKWebViewBridge js.Global.Set( "_GIBWKWebViewBridgeInitialize", func(message64 *js.Object) { // Get the messenger object // NOTE: For some reason, we can't get the postMessage method on // this object and use Invoke(...) on it directly, it just doesn't // work. I don't know why, but for some reason doing // Call("postMessage", ...) does work. hostMessenger := js.Global.Get( "webkit", ).Get( "messageHandlers", ).Get( "_GIBWKWebViewBridgeMessageHandler", ) // Create a new WKWebViewBridge bridge := &WKWebViewBridge{ hostMessenger: hostMessenger, sequences: newSequencer(), } // Create a wrapper for the host to interface with for sending // results js.Global.Set("_GIBWKWebViewBridge", js.MakeWrapper(bridge)) // Decode the initialization message messageBytes, err := base64.StdEncoding.DecodeString( message64.String(), ) if err != nil { panic("unable to decode initialization message") } // Call HostInitialize HostInitialize(bridge, string(messageBytes)) }, ) }
func New() *js.Object { return js.MakeWrapper(&BibTeX{}) }
func TestMakeWrapper(t *testing.T) { if !js.Global.Call("eval", `(function(m) { return m.Method({x: 1})["y"] === "z"; })`).Invoke(js.MakeWrapper(&M{42})).Bool() { t.Fail() } }
func (p *Playback) Board(i int) *js.Object { return js.MakeWrapper(p.Boards[i]) }
func RiveScript() *js.Object { return js.MakeWrapper(rivescript.New()) }
func New(name string) *js.Object { return js.MakeWrapper(&Pet{name}) }
func UGCPolicy(name string) *js.Object { return js.MakeWrapper(bluemonday.UGCPolicy()) }
func GetReplayFromString(replayString string) *js.Object { // Will it work? It's not Unicode clean msg, _ := capnp.Unmarshal([]byte(replayString)) r, _ := botapi.ReadRootReplay(msg) return js.MakeWrapper(engine.NewReplay(r)) }