shithub: mycel

Download patch

ref: 612c7ae57b92e3b1a56a606c415a26cccfe7c210
parent: 8b6f2304278271517909d2a699a8e62b50c5968e
author: Philip Silva <philip.silva@protonmail.com>
date: Fri Feb 26 12:11:27 EST 2021

Support ajax also via jQuery 1.8

diff: cannot open a/domino-lib//null: file does not exist: 'a/domino-lib//null'
--- a/domino/domino.go
+++ b/domino/domino.go
@@ -156,7 +156,7 @@
 		var ___fq;
 		___fq = function(pre, el) {
 			var i, p;
-			
+
 			if (!el) {
 				return undefined;
 			}
@@ -213,6 +213,7 @@
 					this.status = 200;
 					if (ls['load']) ls['load'].bind(this)();
 					if (this.onload) this.onload.bind(this)();
+
 					if (this.onreadystatechange) this.onreadystatechange.bind(this)();
 				}
 			}.bind(this);
@@ -415,15 +416,16 @@
 func (d *Domino) TrackChanges() (html string, changed bool, err error) {
 	outer:
 	for {
+		// TODO: either add other change types like ajax begin/end or
+		// just have one channel for all events worth waiting for.
 		select {
-		case m := <-d.domChange:
-			log.Printf("mutation received @ %v for %v", m.Time, m.Sel)
+		case <-d.domChange:
 			changed = true
-		default:
+		case <-time.After(time.Second):
 			break outer
 		}
 	}
-	
+
 	if changed {
 		html, err = d.Exec("document.querySelector('html').innerHTML;", false)
 		if err != nil {
@@ -536,7 +538,7 @@
 		cb("", "cannot do crossorigin request to " + u.String())
 		return
 	}
-	fmt.Printf("data=%+v\n", data)
+
 	req, err := http.NewRequest(method, u.String(), strings.NewReader(data))
 	if err != nil {
 		cb("", err.Error())
@@ -545,18 +547,23 @@
 	for k, v := range h {
 		req.Header.Add(k, v)
 	}
-	resp, err := c.Do(req)
-	if err != nil {
-		cb("", err.Error())
-		return
-	}
-	defer resp.Body.Close()
-	bs, err := ioutil.ReadAll(resp.Body)
-	if err != nil {
-		cb("", err.Error())
-		return
-	}
-	cb(string(bs), "")
+	// TODO: timeout? context? http timeout?
+	go func() {
+		resp, err := c.Do(req)
+		if err != nil {
+			cb("", err.Error())
+			return
+		}
+		defer resp.Body.Close()
+		bs, err := ioutil.ReadAll(resp.Body)
+		if err != nil {
+			cb("", err.Error())
+			return
+		}
+		d.loop.RunOnLoop(func(*goja.Runtime) {
+			cb(string(bs), "")
+		})
+	}()
 }
 
 func (d *Domino) mutated(t int, q string) {
@@ -565,7 +572,7 @@
 		Type: t,
 		Sel: q,
 	}
-	log.Printf("mutation received: %+v", m)
+
 	select {
 	case d.domChange <- m:
 	default:
--- a/domino/domino_test.go
+++ b/domino/domino_test.go
@@ -646,6 +646,52 @@
 		t.Fatalf("%v", err)
 	}
 	t.Logf("res=%v", res)
+	if res != "success" {
+		t.Fatalf(res)
+	}
+	d.Stop()
+}
+
+func TestJQueryAjax182(t *testing.T) {
+	mb := &MockBrowser{}
+	mb.origin, _ = url.Parse("https://example.com")
+	mb.linkedUrl, _ = url.Parse("https://example.com")
+	buf, err := ioutil.ReadFile("godoc/jquery-1.8.2.js")
+	if err != nil {
+		t.Fatalf("%v", err)
+	}
+	d := NewDomino(simpleHTML, mb, nil)
+	d.Start()
+	script := `
+	var res;
+	$.ajax({
+		url: '/',
+		success: function() {
+			console.log('success!!!');
+			res = 'success';
+		},
+		error: function() {
+			console.log('error!!!');
+			res = 'err';
+		}
+	});
+	`
+	_, err = d.Exec(string(buf) + ";" + script, true)
+	if err != nil {
+		t.Fatalf("%v", err)
+	}
+	if err = d.CloseDoc(); err != nil {
+		t.Fatalf("%v", err)
+	}
+	<-time.After(5*time.Second)
+	res, err := d.Exec("res;", false)
+	if err != nil {
+		t.Fatalf("%v", err)
+	}
+	t.Logf("res=%v", res)
+	if res != "success" {
+		t.Fatalf(res)
+	}
 	d.Stop()
 }
 
--- a/go.mod
+++ b/go.mod
@@ -3,6 +3,7 @@
 go 1.16
 
 replace 9fans.net/go v0.0.0-00010101000000-000000000000 => github.com/knusbaum/go v0.0.0-20200413212707-848f58a0ec6e
+
 replace github.com/srwiley/oksvg v0.0.0-20200311192757-870daf9aa564 => github.com/psilva261/oksvg v0.0.0-20210212153200-941e54e245a3
 
 exclude github.com/aymerick/douceur v0.1.0
@@ -15,7 +16,7 @@
 	github.com/chris-ramon/douceur v0.2.1-0.20160603235419-f3463056cd52
 	github.com/dlclark/regexp2 v1.4.0 // indirect
 	github.com/dop251/goja v0.0.0-20210126164150-f5884268f0c0
-	github.com/dop251/goja_nodejs v0.0.0-20200811150831-9bc458b4bbeb
+	github.com/dop251/goja_nodejs v0.0.0-20210225215109-d91c329300e7
 	github.com/go-sourcemap/sourcemap v2.1.3+incompatible // indirect
 	github.com/gorilla/css v1.0.0 // indirect
 	github.com/jvatic/goja-babel v0.0.0-20200102152603-63c66b7c796a
--- a/go.sum
+++ b/go.sum
@@ -2,7 +2,6 @@
 github.com/andybalholm/cascadia v1.1.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y=
 github.com/chris-ramon/douceur v0.2.1-0.20160603235419-f3463056cd52 h1:xJWyi77j4VQwdeo6bO3wQSQ7o7yVwEM0ZvwXpyKHZZ8=
 github.com/chris-ramon/douceur v0.2.1-0.20160603235419-f3463056cd52/go.mod h1:wDW5xjJdeoMm1mRt4sD4c/LbF/mWdEpRXQKjTR8nIBE=
-github.com/dlclark/regexp2 v1.2.0 h1:8sAhBGEM0dRWogWqWyQeIJnxjWO6oIjl8FKqREDsGfk=
 github.com/dlclark/regexp2 v1.2.0/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc=
 github.com/dlclark/regexp2 v1.4.0 h1:F1rxgk7p4uKjwIQxBs9oAXe5CqrXlCduYEJvrF4u93E=
 github.com/dlclark/regexp2 v1.4.0/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc=
@@ -9,9 +8,8 @@
 github.com/dop251/goja v0.0.0-20191203121440-007eef3bc40f/go.mod h1:Mw6PkjjMXWbTj+nnj4s3QPXq1jaT0s5pC0iFD4+BOAA=
 github.com/dop251/goja v0.0.0-20210126164150-f5884268f0c0 h1:6zmaq2NhOm//MNgxDPXoLiWS1c/WTPxqqeUAQFrZRbQ=
 github.com/dop251/goja v0.0.0-20210126164150-f5884268f0c0/go.mod h1:Mw6PkjjMXWbTj+nnj4s3QPXq1jaT0s5pC0iFD4+BOAA=
-github.com/dop251/goja_nodejs v0.0.0-20200811150831-9bc458b4bbeb h1:UGtCiVzBK40WGYBmNui17MHCkAqdo1j3BbhtU3mB1fI=
-github.com/dop251/goja_nodejs v0.0.0-20200811150831-9bc458b4bbeb/go.mod h1:hn7BA7c8pLvoGndExHudxTDKZ84Pyvv+90pbBjbTz0Y=
-github.com/go-sourcemap/sourcemap v2.1.2+incompatible h1:0b/xya7BKGhXuqFESKM4oIiRo9WOt2ebz7KxfreD6ug=
+github.com/dop251/goja_nodejs v0.0.0-20210225215109-d91c329300e7 h1:tYwu/z8Y0NkkzGEh3z21mSWggMg4LwLRFucLS7TjARg=
+github.com/dop251/goja_nodejs v0.0.0-20210225215109-d91c329300e7/go.mod h1:hn7BA7c8pLvoGndExHudxTDKZ84Pyvv+90pbBjbTz0Y=
 github.com/go-sourcemap/sourcemap v2.1.2+incompatible/go.mod h1:F8jJfvm2KbVjc5NqelyYJmf/v5J0dwNLS2mL4sNA1Jg=
 github.com/go-sourcemap/sourcemap v2.1.3+incompatible h1:W1iEw64niKVGogNgBN3ePyLFfuisuzeidWPMPWmECqU=
 github.com/go-sourcemap/sourcemap v2.1.3+incompatible/go.mod h1:F8jJfvm2KbVjc5NqelyYJmf/v5J0dwNLS2mL4sNA1Jg=
@@ -32,8 +30,6 @@
 github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646/go.mod h1:jpp1/29i3P1S/RLdc7JQKbRpFeM1dOBd8T9ki5s+AY8=
 github.com/psilva261/oksvg v0.0.0-20210212153200-941e54e245a3 h1:LiOcewq9fY3XoorTvhD4vF/s/YdA+paymQXI48RIolw=
 github.com/psilva261/oksvg v0.0.0-20210212153200-941e54e245a3/go.mod h1:rIYdSIGJXvMv8Qw3fNAiSMBurAP18jyln+wR3PI0qKo=
-github.com/srwiley/oksvg v0.0.0-20200311192757-870daf9aa564 h1:HunZiaEKNGVdhTRQOVpMmj5MQnGnv+e8uZNu3xFLgyM=
-github.com/srwiley/oksvg v0.0.0-20200311192757-870daf9aa564/go.mod h1:afMbS0qvv1m5tfENCwnOdZGOF8RGR/FsZ7bvBxQGZG4=
 github.com/srwiley/rasterx v0.0.0-20200120212402-85cb7272f5e9 h1:m59mIOBO4kfcNCEzJNy71UkeF4XIx2EVmL9KLwDQdmM=
 github.com/srwiley/rasterx v0.0.0-20200120212402-85cb7272f5e9/go.mod h1:mvWM0+15UqyrFKqdRjY6LuAVJR0HOVhJlEgZ5JWtSWU=
 github.com/stvp/assert v0.0.0-20170616060220-4bc16443988b h1:GlTM/aMVIwU3luIuSN2SIVRuTqGPt1P97YxAi514ulw=
@@ -55,7 +51,6 @@
 golang.org/x/text v0.3.5 h1:i6eZZ+zk0SOf0xgBpEpPD18qWcJda6q1sxt3S0kzyUQ=
 golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
 golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
-gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
 gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
 gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
 gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=