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" "log"
"net/http" "net/http"
"os" "os"
"os/exec"
"strings"
) )
var ( var (
@ -32,6 +34,8 @@ func main() {
http.HandleFunc("/", pageIndex) http.HandleFunc("/", pageIndex)
http.HandleFunc("/css/main.css", pageCSS) http.HandleFunc("/css/main.css", pageCSS)
http.HandleFunc("/browser/start", actionBrowserStart)
http.HandleFunc("/start", actionStart) http.HandleFunc("/start", actionStart)
http.HandleFunc("/stop/{uuid}", actionStop) http.HandleFunc("/stop/{uuid}", actionStop)
http.HandleFunc("/sites", actionSites) http.HandleFunc("/sites", actionSites)
@ -40,6 +44,20 @@ func main() {
http.ListenAndServe("[::]:5123", nil) 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) { // {{{ func pageIndex(w http.ResponseWriter, r *http.Request) { // {{{
var tmpl *template.Template var tmpl *template.Template
var err error var err error
@ -76,6 +94,17 @@ func actionSites(w http.ResponseWriter, r *http.Request) { // {{{
w.Write(j) 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) { // {{{ func actionStart(w http.ResponseWriter, r *http.Request) { // {{{
var req struct { var req struct {
URL string URL string
@ -97,7 +126,8 @@ func actionStart(w http.ResponseWriter, r *http.Request) { // {{{
} }
var site Site 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 { if err != nil {
log.Println(err) log.Println(err)
} }

View file

@ -7,15 +7,20 @@ input {
font-size: 1em; font-size: 1em;
} }
.browser {
input {
width: 350px;
}
}
.new { .new {
width: 400px; width: 350px;
} }
.sites { .sites {
display: grid; display: grid;
grid-template-columns: repeat(3, min-content); grid-template-columns: repeat(3, min-content);
grid-gap: 8px 16px; grid-gap: 8px 16px;
margin-top: 32px;
div { div {
white-space: nowrap; white-space: nowrap;

View file

@ -7,9 +7,23 @@
<link rel="stylesheet" type="text/css" href="/css/main.css"> <link rel="stylesheet" type="text/css" href="/css/main.css">
</head> </head>
<script> <script>
function startBrowser() {
const userDataDir = document.querySelector('.user-data-dir').value
localStorage.setItem('userdatadir', userDataDir)
fetch('/browser/start', {
method: 'POST',
body: userDataDir,
})
}
function newSite() { function newSite() {
const url = document.querySelector('.new.url').value const url = document.querySelector('.new.url').value
const watch = document.querySelector('.new.watch').value const watch = document.querySelector('.new.watch').value
localStorage.setItem('url', url)
localStorage.setItem('watch', watch)
fetch('/start', { fetch('/start', {
method: 'POST', method: 'POST',
body: JSON.stringify({url, watch}), body: JSON.stringify({url, watch}),
@ -37,19 +51,35 @@
}) })
} }
function init() {
const userdatadir = localStorage.getItem('userdatadir') || ''
const url = localStorage.getItem('url') || ''
const watch = localStorage.getItem('watch') || ''
document.querySelector('.user-data-dir').value = userdatadir
document.querySelector('.new.url').value = url
document.querySelector('.new.watch').value = watch
setInterval(siteStatus, 2000) setInterval(siteStatus, 2000)
}
</script> </script>
<body> <body>
<input type="text" class="new url" placeholder="https://example.com" value="https://scan.euterm.n44.se"> <div class="browser">
<input type="text" class="new watch" placeholder="~/example.com/css/" <h1>Browser</h1>
value="/home/magnus/repo/euterm/euscan/static/css"> <input type="text" class="user-data-dir" placeholder="~/.local/share/chrome-dev">
<button onclick="newSite()">Start</button> <button onclick="startBrowser()">Start</button>
</div>
<h1>CSS reloading</h1>
<div class="sites"> <div class="sites">
<div class="header">UUID</div>
<div class="header">URL</div> <div class="header">URL</div>
<div class="header">Watch</div>
<div class="header"></div> <div class="header"></div>
<div class="line"></div>
<input type="text" class="new url" placeholder="https://example.com">
<input type="text" class="new watch" placeholder="~/example.com/css/">
<button onclick="newSite()">Start</button>
{{ range . }} {{ range . }}
<div class="line"></div> <div class="line"></div>
@ -58,6 +88,7 @@
<div class="stop" onclick="stopSite('{{ .UUID }}')"></div> <div class="stop" onclick="stopSite('{{ .UUID }}')"></div>
{{ end }} {{ end }}
</div> </div>
<script>init()</script>
</body> </body>
</html> </html>