From 3193f6be52e1d8b5f4582f0a07f97b27b0756f91 Mon Sep 17 00:00:00 2001 From: Atridad Lahiji Date: Thu, 29 Feb 2024 10:53:08 -0700 Subject: [PATCH] Fixed song titles breaking hyperscript if they included quotes or apostrophes --- api/nowplaying.go | 5 ++++- lib/spotify.go | 12 +++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/api/nowplaying.go b/api/nowplaying.go index 76ced3e..08ae9c8 100644 --- a/api/nowplaying.go +++ b/api/nowplaying.go @@ -20,7 +20,10 @@ func NowPlayingHandler(c echo.Context) error { } if playing.Item != nil && playing.Playing { - return c.String(http.StatusOK, `
🔥
`) + songName := lib.NowPlayingTextFilter(playing.Item.Name) + artistName := lib.NowPlayingTextFilter(playing.Item.Artists[0].Name) + + return c.String(http.StatusOK, `
🔥
`) } else { return c.String(http.StatusOK, "") } diff --git a/lib/spotify.go b/lib/spotify.go index e6faa95..30f4df7 100644 --- a/lib/spotify.go +++ b/lib/spotify.go @@ -3,6 +3,7 @@ package lib import ( "context" "os" + "strings" "sync" "atri.dad/lib/pubsub" @@ -19,6 +20,12 @@ var ( once sync.Once ) +func NowPlayingTextFilter(s string) string { + s = strings.Replace(s, "'", "'", -1) + s = strings.Replace(s, "\"", """, -1) + return s +} + func GetOAuth2Config(clientID string, clientSecret string) *oauth2.Config { once.Do(func() { config = &oauth2.Config{ @@ -67,7 +74,10 @@ func CurrentlyPlayingTrackSSE(ctx context.Context, pubSub pubsub.PubSub) error { } if playing.Item != nil && playing.Playing { - SendSSE(ctx, pubSub, "spotify", `
🔥
`) + songName := NowPlayingTextFilter(playing.Item.Name) + artistName := NowPlayingTextFilter(playing.Item.Artists[0].Name) + + return SendSSE(ctx, pubSub, "spotify", `
🔥
`) } else { SendSSE(ctx, pubSub, "spotify", "") }