Start browser, store latest field data

This commit is contained in:
Magnus Åhall 2025-11-10 08:52:12 +01:00
parent b7cd308016
commit 1203ee589c
3 changed files with 75 additions and 9 deletions

32
main.go
View file

@ -12,6 +12,8 @@ import (
"log"
"net/http"
"os"
"os/exec"
"strings"
)
var (
@ -32,6 +34,8 @@ func main() {
http.HandleFunc("/", pageIndex)
http.HandleFunc("/css/main.css", pageCSS)
http.HandleFunc("/browser/start", actionBrowserStart)
http.HandleFunc("/start", actionStart)
http.HandleFunc("/stop/{uuid}", actionStop)
http.HandleFunc("/sites", actionSites)
@ -40,6 +44,20 @@ func main() {
http.ListenAndServe("[::]:5123", nil)
}
func replaceTilde(str string) string {// {{{
homedir, err := os.UserHomeDir()
if err != nil {
log.Println(err)
return str
}
if str[0] == '~' {
str = strings.Replace(str, "~", homedir, 1)
}
return str
}// }}}
func pageIndex(w http.ResponseWriter, r *http.Request) { // {{{
var tmpl *template.Template
var err error
@ -76,6 +94,17 @@ func actionSites(w http.ResponseWriter, r *http.Request) { // {{{
w.Write(j)
} // }}}
func actionBrowserStart(w http.ResponseWriter, r *http.Request) {// {{{
body, _ := io.ReadAll(r.Body)
userDataDir := replaceTilde(string(body))
browser := exec.Command("google-chrome", "--user-data-dir="+string(userDataDir), "--remote-debugging-port=9222")
err := browser.Start()
if err != nil {
log.Println(err)
} else {
go browser.Wait()
}
}// }}}
func actionStart(w http.ResponseWriter, r *http.Request) { // {{{
var req struct {
URL string
@ -97,7 +126,8 @@ func actionStart(w http.ResponseWriter, r *http.Request) { // {{{
}
var site Site
site, err = NewSite(flagWsURL, req.URL, req.Watch)
expandedWatch := replaceTilde(req.Watch)
site, err = NewSite(flagWsURL, req.URL, expandedWatch)
if err != nil {
log.Println(err)
}