shithub: mycel

Download patch

ref: bbbf75c1e8e624ddc05ec6901ebfff4d3750e215
parent: 14f3fdd014c2a01a1d3d19d34d28fa22256260c3
author: Philip Silva <philip.silva@protonmail.com>
date: Fri Dec 25 11:27:25 EST 2020

Updated click handler

--- a/browser/experimental.go
+++ b/browser/experimental.go
@@ -1,11 +1,8 @@
 package browser
 
 import (
-	//"bytes"
 	"fmt"
 	"image"
-	//"io/ioutil"
-	//"net/http"
 	"strings"
 	"opossum/domino"
 	"opossum/nodes"
@@ -13,7 +10,6 @@
 
 	"9fans.net/go/draw"
 	"github.com/mjl-/duit"
-	//"opossum/nodes"
 )
 
 func (el *Element) slicedDraw(dui *duit.DUI, self *duit.Kid, img *draw.Image, orig image.Point, m draw.Mouse, force bool) bool {
@@ -141,7 +137,6 @@
 	})
 }
 
-
 func processJS(htm string) (resHtm string, err error) {
 	_ = strings.Replace(htm, "window.", "", -1)
 	d := domino.NewDomino(htm)
@@ -156,10 +151,7 @@
 	return
 }
 
-func processJS2(htm string, doc *nodes.Node, scripts []string) (resHtm string, err error) {
-	//_ = strings.Replace(htm, "window.", "", -1)
-	d := domino.NewDomino(htm)
-	d.Start()
+func processJS2(d *domino.Domino, doc *nodes.Node, scripts []string) (resHtm string, err error) {
 	code := ""
 	for _, script := range scripts {
 		code += `
@@ -178,7 +170,6 @@
 			}
 		`
 	}
-	log.Printf("code=%v\n", code)
 	if err = d.Exec/*6*/(code); err != nil {
 		return "", fmt.Errorf("exec <script>s: %w", err)
 	}
@@ -187,8 +178,6 @@
 	if err != nil {
 		return "", fmt.Errorf("track changes: %w", err)
 	}
-	log.Printf("processJS: changes = %v", changed)
-	log.Printf("exp. resHtm=%v\n", resHtm)
-	d.Stop()
+	log.Printf("processJS: changed = %v", changed)
 	return
 }
--- a/browser/website.go
+++ b/browser/website.go
@@ -15,6 +15,7 @@
 type Website struct {
 	duit.UI
 	html      string
+	d *domino.Domino
 }
 
 func (w *Website) layout(f opossum.Fetcher) {
@@ -108,7 +109,13 @@
 		}
 		codes := domino.Scripts(nt, downloads)
 		log.Infof("JS pipeline start")
-		jsProcessed, err := processJS2(w.html, nt, codes)
+		if w.d != nil {
+			log.Infof("Stop existing JS instance")
+			w.d.Stop()
+		}
+		w.d = domino.NewDomino(w.html)
+		w.d.Start()
+		jsProcessed, err := processJS2(w.d, nt, codes)
 		if err == nil {
 			if w.html != jsProcessed {
 				log.Infof("html changed")
--- a/domino/domino.go
+++ b/domino/domino.go
@@ -188,15 +188,16 @@
 func (d *Domino) TriggerClick(selector string) (newHTML string, ok bool, err error) {
 	res, err := d.vm.RunString(`
 		var sel = '` + selector + `';
-		console.log('sel=' + sel);
 		var sell = document.querySelector(sel);
-		console.log('sell=' + sell);
-		var selfn = sell.click.bind(sell);
-		console.log('selfn=' + selfn);
-		if (selfn) {
-			selfn();
+		if (sell._listeners && sell._listeners.click) {
+			var selfn = sell.click.bind(sell);
+			if (selfn) {
+				selfn();
+			}
+			!!selfn;
+		} else {
+			false;
 		}
-		!!selfn;
 	`)
 
 	ok = fmt.Sprintf("%v", res) == "true"