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)
}

View file

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

View file

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