package switcher import ( "fmt" "log" "time" "gitea.urkob.com/urko/go-wifi-switcher/cfg" "github.com/go-rod/rod" ) type switcher struct { remoteControlBrowserUrl string config *cfg.Config } var ( usernameID = "#username" userpasswordID = "#userpassword" loginText = "Login" skipText = "Skip" netID = "#Net" netWlanID = "#Net-WLAN" mainFrameID = "#mainFrame" wlanEnableID = "#wlanEnable" ) func NewSwitcher(remoteControlBrowserUrl string, config *cfg.Config) switcher { return switcher{ remoteControlBrowserUrl: remoteControlBrowserUrl, config: config, } } func (s switcher) SwitchWIFI() error { browser := rod.New(). ControlURL(s.remoteControlBrowserUrl). MustConnect(). MustIgnoreCertErrors(true) defer browser.MustClose() page := browser.MustPage(s.config.Page) defer page.Close() username, err := page.Element(usernameID) if err != nil { return fmt.Errorf("page.Element %s: %s", userpasswordID, err) } username.MustInput(s.config.AdminUser) log.Println(usernameID, "DONE") userpassword, err := page.Element(userpasswordID) if err != nil { return fmt.Errorf("page.Element %s: %s", userpasswordID, err) } userpassword.MustInput(s.config.Password) log.Println(userpasswordID, "DONE") login, err := page.Element(`input[value="` + loginText + `"]`) if err != nil { return fmt.Errorf("page.Element %s: %s", loginText, err) } login.MustClick() log.Println(loginText, "DONE") skip, err := page.Element(`input[value="` + skipText + `"]`) if err != nil { return fmt.Errorf("page.Element %s: %s", skipText, err) } skip.MustClick() log.Println(skipText, "DONE") net, err := page.Element(netID) if err != nil { return fmt.Errorf("page.Element %s: %s", netID, err) } net.MustClick() log.Println(netID, "DONE") netWlan, err := page.Element(netWlanID) if err != nil { return fmt.Errorf("page.Element %s: %s", netWlanID, err) } netWlan.MustClick() log.Println(netWlanID, "DONE") frameElement, err := page.Element(mainFrameID) if err != nil { return fmt.Errorf("page.Element %s: %s", mainFrameID, err) } mainFrame, err := frameElement.Frame() if err != nil { return fmt.Errorf("frameElement.Frame: %s", err) } wlanEnable, err := mainFrame.Element(wlanEnableID) if err != nil { return fmt.Errorf("mainFrame.Element %s: %s", wlanEnableID, err) } wlanEnable.MustClick() log.Println(wlanEnableID, "DONE") sysSubmitID := "#sysSubmit" sysSubmit, err := mainFrame.Element(sysSubmitID) if err != nil { return fmt.Errorf("mainFrame.Element %s: %s", sysSubmitID, err) } sysSubmit.MustClick() log.Println(sysSubmitID, "DONE") // TODO: improve this and wait until something should happens (refresh) time.Sleep(time.Second * 3) return nil }