Fixed song titles breaking hyperscript if they included quotes or apostrophes

This commit is contained in:
Atridad Lahiji 2024-02-29 10:53:08 -07:00
parent 981ec34e8a
commit 3193f6be52
No known key found for this signature in database
2 changed files with 15 additions and 2 deletions

View file

@ -20,7 +20,10 @@ func NowPlayingHandler(c echo.Context) error {
} }
if playing.Item != nil && playing.Playing { if playing.Item != nil && playing.Playing {
return c.String(http.StatusOK, `<div class="indicator-item badge badge-success"><a _="on mouseover put '🔥 Listening to `+playing.Item.Name+" by "+playing.Item.Artists[0].Name+` 🔥' into my.textContent on mouseout put '🔥' into my.textContent" href="`+playing.Item.ExternalURLs["spotify"]+`" rel="noreferrer" target="_blank">🔥</a></div>`) songName := lib.NowPlayingTextFilter(playing.Item.Name)
artistName := lib.NowPlayingTextFilter(playing.Item.Artists[0].Name)
return c.String(http.StatusOK, `<div class="indicator-item badge badge-success"><a _='on mouseover put "🔥 Listening to `+songName+" by "+artistName+` 🔥" into my.textContent on mouseout put "🔥" into my.textContent' href="`+playing.Item.ExternalURLs["spotify"]+`" rel="noreferrer" target="_blank">🔥</a></div>`)
} else { } else {
return c.String(http.StatusOK, "") return c.String(http.StatusOK, "")
} }

View file

@ -3,6 +3,7 @@ package lib
import ( import (
"context" "context"
"os" "os"
"strings"
"sync" "sync"
"atri.dad/lib/pubsub" "atri.dad/lib/pubsub"
@ -19,6 +20,12 @@ var (
once sync.Once once sync.Once
) )
func NowPlayingTextFilter(s string) string {
s = strings.Replace(s, "'", "&#39;", -1)
s = strings.Replace(s, "\"", "&quot;", -1)
return s
}
func GetOAuth2Config(clientID string, clientSecret string) *oauth2.Config { func GetOAuth2Config(clientID string, clientSecret string) *oauth2.Config {
once.Do(func() { once.Do(func() {
config = &oauth2.Config{ config = &oauth2.Config{
@ -67,7 +74,10 @@ func CurrentlyPlayingTrackSSE(ctx context.Context, pubSub pubsub.PubSub) error {
} }
if playing.Item != nil && playing.Playing { if playing.Item != nil && playing.Playing {
SendSSE(ctx, pubSub, "spotify", `<div class="indicator-item badge badge-success"><a _="on mouseover put '🔥 Listening to `+playing.Item.Name+" by "+playing.Item.Artists[0].Name+` 🔥' into my.textContent on mouseout put '🔥' into my.textContent" href="`+playing.Item.ExternalURLs["spotify"]+`" rel="noreferrer" target="_blank">🔥</a></div>`) songName := NowPlayingTextFilter(playing.Item.Name)
artistName := NowPlayingTextFilter(playing.Item.Artists[0].Name)
return SendSSE(ctx, pubSub, "spotify", `<div class="indicator-item badge badge-success"><a _='on mouseover put "🔥 Listening to `+songName+" by "+artistName+` 🔥" into my.textContent on mouseout put "🔥" into my.textContent' href="`+playing.Item.ExternalURLs["spotify"]+`" rel="noreferrer" target="_blank">🔥</a></div>`)
} else { } else {
SendSSE(ctx, pubSub, "spotify", "") SendSSE(ctx, pubSub, "spotify", "")
} }