Compare commits
2 commits
0c0811a5fc
...
2098da3417
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2098da3417 | ||
|
|
5a15824ed5 |
3 changed files with 45 additions and 23 deletions
35
main.go
35
main.go
|
|
@ -7,6 +7,7 @@ import (
|
|||
"embed"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"io"
|
||||
"log"
|
||||
|
|
@ -16,10 +17,12 @@ import (
|
|||
"strings"
|
||||
)
|
||||
|
||||
const VERSION = "v2"
|
||||
|
||||
var (
|
||||
flagVerbose bool
|
||||
flagWsURL string
|
||||
flagWatch string
|
||||
flagVersion bool
|
||||
flagDebuggingPort int
|
||||
|
||||
//go:embed static
|
||||
fs embed.FS
|
||||
|
|
@ -27,10 +30,15 @@ var (
|
|||
|
||||
func main() {
|
||||
flag.BoolVar(&flagVerbose, "v", false, "verbose")
|
||||
flag.StringVar(&flagWsURL, "ws", "ws://127.0.0.1:9222", "devtools url")
|
||||
flag.StringVar(&flagWatch, "watch", "", "Files to watch")
|
||||
flag.BoolVar(&flagVersion, "version", false, "Print version and exit")
|
||||
flag.IntVar(&flagDebuggingPort, "port", 9222, "Chrome debugging port")
|
||||
flag.Parse()
|
||||
|
||||
if flagVersion {
|
||||
fmt.Println(VERSION)
|
||||
return
|
||||
}
|
||||
|
||||
http.HandleFunc("/", pageIndex)
|
||||
http.HandleFunc("/css/main.css", pageCSS)
|
||||
|
||||
|
|
@ -44,7 +52,7 @@ func main() {
|
|||
http.ListenAndServe("[::]:5123", nil)
|
||||
}
|
||||
|
||||
func replaceTilde(str string) string {// {{{
|
||||
func replaceTilde(str string) string { // {{{
|
||||
homedir, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
|
|
@ -56,7 +64,7 @@ func replaceTilde(str string) string {// {{{
|
|||
}
|
||||
|
||||
return str
|
||||
}// }}}
|
||||
} // }}}
|
||||
|
||||
func pageIndex(w http.ResponseWriter, r *http.Request) { // {{{
|
||||
var tmpl *template.Template
|
||||
|
|
@ -70,7 +78,11 @@ func pageIndex(w http.ResponseWriter, r *http.Request) { // {{{
|
|||
return
|
||||
}
|
||||
|
||||
tmpl.Execute(w, sites)
|
||||
data := map[string]any{
|
||||
"Sites": sites,
|
||||
"VERSION": VERSION,
|
||||
}
|
||||
tmpl.Execute(w, data)
|
||||
} // }}}
|
||||
func pageCSS(w http.ResponseWriter, r *http.Request) { // {{{
|
||||
w.Header().Add("Content-Type", "text/css")
|
||||
|
|
@ -94,17 +106,17 @@ func actionSites(w http.ResponseWriter, r *http.Request) { // {{{
|
|||
w.Write(j)
|
||||
} // }}}
|
||||
|
||||
func actionBrowserStart(w http.ResponseWriter, r *http.Request) {// {{{
|
||||
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")
|
||||
browser := exec.Command("google-chrome", "--user-data-dir="+string(userDataDir), fmt.Sprintf("--remote-debugging-port=%d", flagDebuggingPort))
|
||||
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
|
||||
|
|
@ -127,7 +139,8 @@ func actionStart(w http.ResponseWriter, r *http.Request) { // {{{
|
|||
|
||||
var site Site
|
||||
expandedWatch := replaceTilde(req.Watch)
|
||||
site, err = NewSite(flagWsURL, req.URL, expandedWatch)
|
||||
wsURL := fmt.Sprintf("ws://127.0.0.1:%d", flagDebuggingPort)
|
||||
site, err = NewSite(wsURL, req.URL, expandedWatch)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,12 @@ body {
|
|||
margin: 32px;
|
||||
}
|
||||
|
||||
#version {
|
||||
position: absolute;
|
||||
right: 16px;
|
||||
top: 16px;
|
||||
}
|
||||
|
||||
input {
|
||||
font-size: 1em;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -44,8 +44,9 @@
|
|||
for (const uuid of Object.keys(json.Sites)) {
|
||||
const site = json.Sites[uuid]
|
||||
if (site.StopLoop) {
|
||||
document.querySelector(`.url[data-uuid="${uuid}"]`)?.classList.add('dead')
|
||||
document.querySelector(`.watch[data-uuid="${uuid}"]`)?.classList.add('dead')
|
||||
const els = document.querySelectorAll(`[data-uuid="${uuid}"]`)
|
||||
for (const el of els)
|
||||
el.remove()
|
||||
}
|
||||
}
|
||||
})
|
||||
|
|
@ -64,6 +65,7 @@
|
|||
</script>
|
||||
|
||||
<body>
|
||||
<div id="version">{{ .VERSION }}</div>
|
||||
<div class="browser">
|
||||
<h1>Browser</h1>
|
||||
<input type="text" class="user-data-dir" placeholder="~/.local/share/chrome-dev">
|
||||
|
|
@ -81,14 +83,15 @@
|
|||
<input type="text" class="new watch" placeholder="~/example.com/css/">
|
||||
<button onclick="newSite()">Start</button>
|
||||
|
||||
{{ range . }}
|
||||
{{ range .Sites }}
|
||||
{{ if .StopLoop }}
|
||||
{{ continue }}
|
||||
{{ end }}
|
||||
<div class="line"></div>
|
||||
<div class="url {{ if .StopLoop }}dead{{ end }}" data-uuid="{{ .UUID }}">{{ .URL }}</div>
|
||||
<div class="watch {{ if .StopLoop }}dead{{ end }}" data-uuid="{{ .UUID }}">{{ .Watch }}</div>
|
||||
<div class="stop" onclick="stopSite('{{ .UUID }}')">{{ if not .StopLoop }}❌{{ end }}</div>
|
||||
<div class="line" data-uuid="{{ .UUID }}"></div>
|
||||
<div class="url" data-uuid="{{ .UUID }}">{{ .URL }}</div>
|
||||
<div class="watch" data-uuid="{{ .UUID }}">{{ .Watch }}</div>
|
||||
<div class="stop" onclick="stopSite('{{ .UUID }}')" data-uuid="{{ .UUID }}">{{ if not .StopLoop }}❌{{
|
||||
end }}</div>
|
||||
{{ end }}
|
||||
</div>
|
||||
<script>init()</script>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue