feat(callback): add the param of error of callback

This commit is contained in:
eson 2020-11-24 15:17:56 +08:00
parent be862559bc
commit cc8f912c82
3 changed files with 22 additions and 9 deletions

View File

@ -15,11 +15,15 @@ import (
"github.com/google/uuid" "github.com/google/uuid"
) )
func init() {
log.SetFlags(log.Llongfile | log.LstdFlags)
}
// Callback 发送代理连接获取内容后的回调函数 // Callback 发送代理连接获取内容后的回调函数
type Callback struct { type Callback struct {
label string label string
hash string hash string
Do func(tid, content string) Do func(tid, content string, err error)
} }
// Client 客户端 // Client 客户端
@ -111,7 +115,7 @@ func (cli *Client) SetHost(host string) {
} }
// Register 注册基础信息 // 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()} cb := Callback{Do: callback, hash: uuid.New().String()}
if _, ok := cli.register.Load(label); ok { if _, ok := cli.register.Load(label); ok {
log.Panic("label: ", label, " is exists") log.Panic("label: ", label, " is exists")
@ -151,7 +155,12 @@ func (cli *Client) Connect() {
callback := f.(Callback) callback := f.(Callback)
if tid, ok := c.GetPostForm("taskid"); ok { if tid, ok := c.GetPostForm("taskid"); ok {
content := c.PostForm("content") 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)
} }
} }
}) })

View File

@ -9,16 +9,20 @@ import (
func TestPort(t *testing.T) { func TestPort(t *testing.T) {
cli := New("http://localhost:7123") cli := New("http://localhost:7123")
ltest := cli.Register("test", func(tid, content string) { ltest := cli.Register("test", func(tid, content string, err error) {
if err != nil {
log.Println("error:", err)
} else {
log.Println(tid, content) log.Println(tid, content)
}
}) })
ltest.SetContentConditionFromFile("example.js") ltest.SetContentConditionFromFile("example.js")
cli.Connect() 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/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/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/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/yanngu?lang=en&deviceType=browser"))
http.ListenAndServe(":42311", nil) http.ListenAndServe(":42311", nil)
} }

View File

@ -1,6 +1,6 @@
function condition() { condition = function () {
var href = window.location.href; var href = window.location.href;
var content = document.documentElement.innerHTML; var content = document.documentElement.innerHTML;
return href.startsWith("https://playerduo.com") && content.startsWith('<ad></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">') return href.startsWith("https://playerduo.com") && content.startsWith('<head></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">')
} }