From cc8f912c8247843f7e26229ff8743ff59902b2e8 Mon Sep 17 00:00:00 2001 From: eson Date: Tue, 24 Nov 2020 15:17:56 +0800 Subject: [PATCH] feat(callback): add the param of error of callback --- client.go | 15 ++++++++++++--- client_test.go | 12 ++++++++---- example.js | 4 ++-- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/client.go b/client.go index 7749147..ee08c0d 100644 --- a/client.go +++ b/client.go @@ -15,11 +15,15 @@ import ( "github.com/google/uuid" ) +func init() { + log.SetFlags(log.Llongfile | log.LstdFlags) +} + // Callback 发送代理连接获取内容后的回调函数 type Callback struct { label string hash string - Do func(tid, content string) + Do func(tid, content string, err error) } // Client 客户端 @@ -111,7 +115,7 @@ func (cli *Client) SetHost(host string) { } // Register 注册基础信息 -func (cli *Client) Register(label string, callback func(tid, content string)) *Label { +func (cli *Client) Register(label string, callback func(tid, content string, err error)) *Label { cb := Callback{Do: callback, hash: uuid.New().String()} if _, ok := cli.register.Load(label); ok { log.Panic("label: ", label, " is exists") @@ -151,7 +155,12 @@ func (cli *Client) Connect() { callback := f.(Callback) if tid, ok := c.GetPostForm("taskid"); ok { content := c.PostForm("content") - callback.Do(tid, content) + errorStr := c.PostForm("error") + var err error = nil + if errorStr != "" { + err = fmt.Errorf(errorStr) + } + callback.Do(tid, content, err) } } }) diff --git a/client_test.go b/client_test.go index 8afd7b2..676f496 100644 --- a/client_test.go +++ b/client_test.go @@ -9,16 +9,20 @@ import ( func TestPort(t *testing.T) { cli := New("http://localhost:7123") - ltest := cli.Register("test", func(tid, content string) { - log.Println(tid, content) + ltest := cli.Register("test", func(tid, content string, err error) { + if err != nil { + log.Println("error:", err) + } else { + log.Println(tid, content) + } }) ltest.SetContentConditionFromFile("example.js") cli.Connect() log.Println(ltest.Open("https://playerduo.com/api/playerDuo-service-v2/rip113?lang=en&deviceType=browser")) log.Println(ltest.Open("https://playerduo.com/api/playerDuo-service-v2/yanngu?lang=en&deviceType=browser")) - // log.Println(ltest.Open("https://playerduo.com/api/playerDuo-service-v2/rip113?lang=en&deviceType=browser")) - // log.Println(ltest.Open("https://playerduo.com/api/playerDuo-service-v2/yanngu?lang=en&deviceType=browser")) + log.Println(ltest.Open("https://playerduo.com/api/playerDuo-service-v2/rip113?lang=en&deviceType=browser")) + log.Println(ltest.Open("https://playerduo.com/api/playerDuo-service-v2/yanngu?lang=en&deviceType=browser")) http.ListenAndServe(":42311", nil) } diff --git a/example.js b/example.js index c3d9008..f6a7fd3 100644 --- a/example.js +++ b/example.js @@ -1,6 +1,6 @@ -function condition() { +condition = function () { var href = window.location.href; var content = document.documentElement.innerHTML; - return href.startsWith("https://playerduo.com") && content.startsWith('
')
+    return href.startsWith("https://playerduo.com") && content.startsWith('
')
 }
\ No newline at end of file