From a850af05264d3c51f6186e9a2e56fb6778d83533 Mon Sep 17 00:00:00 2001 From: Ryan Stafford Date: Mon, 3 Jul 2023 22:43:16 -0400 Subject: [PATCH] youtube embeds fixes #3 --- public/style.css | 7 +++++-- public/utils.js | 31 +++++++++++++++++++++++++++++++ routes.go | 3 +++ templates/frontpage.html | 2 +- templates/main.html | 2 +- templates/post.html | 10 +++++----- 6 files changed, 46 insertions(+), 9 deletions(-) diff --git a/public/style.css b/public/style.css index f05af30..d9e612c 100644 --- a/public/style.css +++ b/public/style.css @@ -381,6 +381,9 @@ code { .expando-button:hover{ background-color: #466599; } +.expando-button.hidden { + display: none; +} .expando { display: none; max-width: 587px; @@ -392,7 +395,7 @@ code { .expando img { max-width: 100%; } -.expando > div { +.expando .md { background-color: #fafafa; border: 1px solid #369; border-radius: 7px; @@ -401,7 +404,7 @@ code { font-size: 14px; overflow: auto; } -.dark .expando > div { +.dark .expando .md { background-color: #262626; color: #ddd; } diff --git a/public/utils.js b/public/utils.js index 4bdffbc..2677e7f 100644 --- a/public/utils.js +++ b/public/utils.js @@ -24,9 +24,14 @@ function postClick(e) { if (bdy.className.indexOf("open")>-1) { bdy.className = 'expando'; btn.className = "expando-button" + targ.getElementsByClassName("embed")[0].innerHTML = "" } else { bdy.className = 'expando open'; btn.className = "expando-button open" + var url = targ.getElementsByClassName("url")[0].href + if (id = parse_youtube(url)) { + targ.getElementsByClassName("embed")[0].innerHTML = youtube_iframe(id) + } } } function commentClick(e) { @@ -89,3 +94,29 @@ function formSubmit(e) { }) return false } + +function parse_youtube(url){ + var regExp = /^.*(?:(?:youtu\.be\/|v\/|vi\/|u\/\w\/|embed\/|shorts\/)|(?:(?:watch)?\?v(?:i)?=|\&v(?:i)?=))([^#\&\?]*).*/; + var match = url.match(regExp); + if (match.length > 1) { + return match[1] + } + return false +} +function youtube_iframe(id) { + return '' +} + +var posts = document.getElementsByClassName("post") +for (var i = 0; i < posts.length; i++) { + var url = posts[i].getElementsByClassName("url")[0].href + if (id = parse_youtube(url)) { + var btn = posts[i].getElementsByClassName("expando-button")[0] + if (btn.className.indexOf("open") > -1) { + console.log(id) + posts[i].getElementsByClassName("embed")[0].innerHTML = youtube_iframe(id) + } else { + btn.className = "expando-button" + } + } +} diff --git a/routes.go b/routes.go index dca64b9..eb43f2d 100644 --- a/routes.go +++ b/routes.go @@ -771,6 +771,9 @@ func UserOp(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { fmt.Println(err) } case "create_post": + if state.CommunityName == "" { + state.CommunityName = r.FormValue("communityname") + } state.GetCommunity(state.CommunityName) state.GetSite() if state.Community == nil { diff --git a/templates/frontpage.html b/templates/frontpage.html index ed38c8a..d45659d 100644 --- a/templates/frontpage.html +++ b/templates/frontpage.html @@ -2,7 +2,7 @@ {{ if and .Community (ne .Community.CommunityView.Community.Title "")}}{{.Community.CommunityView.Community.Title}}{{else if ne .CommunityName ""}}/c/{{.CommunityName}}{{ else if .User}}overview for {{.User.PersonView.Person.Name}}{{else}}{{ host .Host }}{{end}} - + diff --git a/templates/main.html b/templates/main.html index 5e3fb50..0334882 100644 --- a/templates/main.html +++ b/templates/main.html @@ -3,7 +3,7 @@ {{if and .Posts .PostID }}{{ (index .Posts 0).Post.Name}} : {{.CommunityName}}{{else if and .Community (ne .Community.CommunityView.Community.Title "")}}{{.Community.CommunityView.Community.Title}}{{else if ne .CommunityName ""}}/c/{{.CommunityName}}{{ else if .User}}overview for {{.User.PersonView.Person.Name}}{{else}}{{ host .Host }}{{end}} - +