package providers import ( "fmt" "log" "time" "github.com/go-rod/rod" ) type ArcherAx50 struct { passwordID string loginButtonID string wirelessTab string chkWifi2gID string saveButtonID string page *rod.Page } func NewArcherAx50(page *rod.Page) ArcherAx50 { return ArcherAx50{ passwordID: "#login-password", loginButtonID: "#login-btn", wirelessTab: "wireless", chkWifi2gID: "chk_enable_wireless_24g", saveButtonID: "#total_save", page: page, } } func (p ArcherAx50) Login(user, pass string) error { time.Sleep(time.Millisecond * 1250) p.page.MustEvaluate(&rod.EvalOptions{ JS: `() => { document.querySelectorAll("input.password-text").forEach((v, i) => { v.click(); v.focus(); v.value="` + pass + `"; }) return ''; }`, }) time.Sleep(time.Millisecond * 250) log.Println(p.passwordID, "DONE") login, err := p.page.Element(p.loginButtonID) if err != nil { return fmt.Errorf("page.Element %s: %s", p.loginButtonID, err) } login.MustClick() log.Println(p.loginButtonID, "DONE") time.Sleep(time.Millisecond * 1550) return nil } func (p ArcherAx50) SwitchWIFI() error { time.Sleep(time.Millisecond * 1350) pageURL := p.page.MustInfo().URL log.Println("p.page.MustInfo().URL", pageURL) wirelessTab, err := p.page.Element(`[name="` + p.wirelessTab + `"]`) if err != nil { return fmt.Errorf("page.Element %s: %s", p.wirelessTab, err) } wirelessTab.MustClick() log.Println(p.wirelessTab, "DONE") time.Sleep(time.Millisecond * 1250) checked := p.page.MustEvaluate(&rod.EvalOptions{ JS: `() => { return document.getElementById('` + p.chkWifi2gID + `').checked; }`, }) isChecked := checked.Value.Bool() log.Println(getCheckedMessage(isChecked)) p.page.MustEvaluate(&rod.EvalOptions{ JS: `() => { document.getElementById('` + p.chkWifi2gID + `').checked=` + fmt.Sprint(!isChecked) + `; return ''; }`, }) log.Println(p.chkWifi2gID, "DONE") saveButton, err := p.page.Element(p.saveButtonID) if err != nil { return fmt.Errorf("page.Element %s: %s", p.saveButtonID, err) } saveButton.MustClick() log.Println(p.saveButtonID, "DONE") time.Sleep(time.Millisecond * 1500) return nil } func getCheckedMessage(isChecked bool) string { if isChecked { return "Time to sleep, let's switch off" } return "Time to laborate, let's switch WIFI on" }