aboutsummaryrefslogtreecommitdiff
path: root/watch.go
diff options
context:
space:
mode:
authorTim Keller <tjk@tjkeller.xyz>2026-05-22 10:37:15 -0500
committerTim Keller <tjk@tjkeller.xyz>2026-05-22 10:37:15 -0500
commit4407433bd31df5526df0dd167d2d9d9d878a09d1 (patch)
treee32ddd96d2cf3bf696d8813f354961e44323414a /watch.go
parent013753b666cfbae213921af4193620c6b356fbf6 (diff)
downloadembedtube-4407433bd31df5526df0dd167d2d9d9d878a09d1.tar.xz
embedtube-4407433bd31df5526df0dd167d2d9d9d878a09d1.zip
support yt url queries for start time, etcHEADmaster
Diffstat (limited to 'watch.go')
-rw-r--r--watch.go33
1 files changed, 29 insertions, 4 deletions
diff --git a/watch.go b/watch.go
index 81f0d86..bc96bd9 100644
--- a/watch.go
+++ b/watch.go
@@ -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)
})
}