diff options
| author | Tim Keller <tjk@tjkeller.xyz> | 2026-05-22 10:37:15 -0500 |
|---|---|---|
| committer | Tim Keller <tjk@tjkeller.xyz> | 2026-05-22 10:37:15 -0500 |
| commit | 4407433bd31df5526df0dd167d2d9d9d878a09d1 (patch) | |
| tree | e32ddd96d2cf3bf696d8813f354961e44323414a | |
| parent | 013753b666cfbae213921af4193620c6b356fbf6 (diff) | |
| download | embedtube-4407433bd31df5526df0dd167d2d9d9d878a09d1.tar.xz embedtube-4407433bd31df5526df0dd167d2d9d9d878a09d1.zip | |
| -rw-r--r-- | main.go | 2 | ||||
| -rw-r--r-- | templates/watch.html | 2 | ||||
| -rw-r--r-- | watch.go | 33 |
3 files changed, 31 insertions, 6 deletions
@@ -18,7 +18,7 @@ func rootHandler(w http.ResponseWriter, r *http.Request) { //http.ServeFile(w, r, "static/index.html") renderIndexTemplate(w) } else { - renderWatchTemplate(w, path) // FIXME just redirect this one to /watch for simplicity + renderWatchTemplate(w, path, r.URL.RawQuery) // FIXME just redirect this one to /watch for simplicity } } diff --git a/templates/watch.html b/templates/watch.html index 2842547..501e44d 100644 --- a/templates/watch.html +++ b/templates/watch.html @@ -6,7 +6,7 @@ {{ define "content" }} <iframe id="player" - src="https://www.youtube-nocookie.com/embed/{{ .Id }}?enablejsapi=1&autoplay=1" + src="{{ .Src }}" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" @@ -3,6 +3,7 @@ package main import ( "html/template" "net/http" + "net/url" ) var templateWatchFiles = []string{ "templates/base.html", "templates/watch.html" } @@ -10,15 +11,39 @@ var templateWatch = template.Must(template.ParseFiles(templateWatchFiles...)) /* render template */ type WatchTemplateData struct { - Id string + Src string } -func renderWatchTemplate(w http.ResponseWriter, id string) { +func renderWatchTemplate(w http.ResponseWriter, id string, rawQuery string) { if debug { reloadTemplate(&templateWatch, templateWatchFiles...) } + + // construct url query from existing rawQuery + q, parseErr := url.ParseQuery(rawQuery) + if parseErr != nil { + templateError(parseErr, w) + return + } + q.Add("enablejsapi", "1") + q.Add("autoplay", "1") + q.Del("v") + if q.Has("t") { + q.Add("start", q.Get("t")) // translate 't' to 'start' (embed equivalent) + q.Del("t") + } + + // construct url + u, urlErr := url.Parse("https://www.youtube-nocookie.com/embed/" + id) + if urlErr != nil { + templateError(urlErr, w) + return + } + u.RawQuery = q.Encode() + + // execute template err := templateWatch.Execute(w, WatchTemplateData{ - Id: id, + Src: u.String(), }) if err != nil { templateError(err, w) @@ -29,6 +54,6 @@ func renderWatchTemplate(w http.ResponseWriter, id string) { func registerRoutesWatch(mux *http.ServeMux) { mux.HandleFunc("/watch", func(w http.ResponseWriter, r *http.Request) { id := r.URL.Query().Get("v") - renderWatchTemplate(w, id) + renderWatchTemplate(w, id, r.URL.RawQuery) }) } |
