diff --git a/src/huesoporro/main.py b/src/huesoporro/main.py index d8169c7..ac21949 100644 --- a/src/huesoporro/main.py +++ b/src/huesoporro/main.py @@ -131,6 +131,22 @@ async def get_tts_overlay() -> Template: return Template(template_name="tts.html") +@get( + "/tts/permalink", + media_type=MediaType.HTML, +) +async def get_tts_permalink(access_token: str) -> Template: + """Handler for the /tts permalink endpoint to be used by apps that can only give the authentication as a query + param and not as a cookie, i.e. OBS""" + + # authenticate the user using the provided access token + await _authenticate(access_token) + + return Template( + template_name="tts.html", + ) + + @get( "/", media_type=MediaType.HTML, @@ -197,6 +213,7 @@ def create_app(): login, get_index, get_tts_overlay, + get_tts_permalink, WebsocketHandler, ], static_files_config=( diff --git a/src/huesoporro/static/js/utils.js b/src/huesoporro/static/js/utils.js index ed9a523..e014ab2 100644 --- a/src/huesoporro/static/js/utils.js +++ b/src/huesoporro/static/js/utils.js @@ -15,4 +15,28 @@ function addLogoutEvent() { window.location.href = "/"; }); -} \ No newline at end of file +} + +function setCookie(name, value, days) { + const date = new Date(); + date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)); + const expires = `expires=${date.toUTCString()}`; + document.cookie = `${name}=${value};${expires};path=/;SameSite=Strict`; +} + +function getCookie(name) { + const cookieName = `${name}=`; + const decodedCookie = decodeURIComponent(document.cookie); + const cookieArray = decodedCookie.split(';'); + + for (let i = 0; i < cookieArray.length; i++) { + let cookie = cookieArray[i]; + while (cookie.charAt(0) === ' ') { + cookie = cookie.substring(1); + } + if (cookie.indexOf(cookieName) === 0) { + return cookie.substring(cookieName.length, cookie.length); + } + } + return null; +} diff --git a/src/huesoporro/templates/header.html b/src/huesoporro/templates/header.html index f4bc8d6..9bd74e7 100644 --- a/src/huesoporro/templates/header.html +++ b/src/huesoporro/templates/header.html @@ -10,7 +10,7 @@ - + Huesoporro diff --git a/src/huesoporro/templates/login.html b/src/huesoporro/templates/login.html index 34f8b31..a8c7016 100644 --- a/src/huesoporro/templates/login.html +++ b/src/huesoporro/templates/login.html @@ -35,35 +35,11 @@ this.loginData = null; } - // Helper method to set a cookie - setCookie(name, value, days) { - const date = new Date(); - date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)); - const expires = `expires=${date.toUTCString()}`; - document.cookie = `${name}=${value};${expires};path=/;SameSite=Strict`; - } - // Helper method to get a cookie - getCookie(name) { - const cookieName = `${name}=`; - const decodedCookie = decodeURIComponent(document.cookie); - const cookieArray = decodedCookie.split(';'); - - for (let i = 0; i < cookieArray.length; i++) { - let cookie = cookieArray[i]; - while (cookie.charAt(0) === ' ') { - cookie = cookie.substring(1); - } - if (cookie.indexOf(cookieName) === 0) { - return cookie.substring(cookieName.length, cookie.length); - } - } - return null; - } async readLoginData() { // Try to get existing login data from cookies - const loginData = this.getCookie("twitchLoginData"); + const loginData = getCookie("twitchLoginData"); try { // Parse the stored login data if it exists @@ -89,7 +65,7 @@ try { // Save login data to cookie (expires in 30 days) - this.setCookie("twitchLoginData", JSON.stringify(loginData), 30); + setCookie("twitchLoginData", JSON.stringify(loginData), 30); this.loginData = loginData; // Hide the login button diff --git a/src/huesoporro/templates/tts.html b/src/huesoporro/templates/tts.html index 160f496..c7ce5b5 100644 --- a/src/huesoporro/templates/tts.html +++ b/src/huesoporro/templates/tts.html @@ -21,6 +21,7 @@ + @@ -53,6 +54,7 @@ this.receivedFileSize = 0; this.log("Connecting to WebSocket: " + this.url); this.websocket = new WebSocket(this.url); + this.websocket.withCredentials = true; this.websocket.onopen = () => { this.log('WebSocket connection established'); }; @@ -178,6 +180,17 @@ addLogoutEvent() + const genPermalinkButton = document.getElementById('genPermalinkButton'); + genPermalinkButton.addEventListener('click', () => { + // generate /tts/permalink?access_token= + // the access token is available in the twitchLoginData cookie + + const cookie = JSON.parse(getCookie("twitchLoginData")) + const permalinkUrl = `${window.location.origin}/tts/permalink?access_token=${cookie.access_token}`; + navigator.clipboard.writeText(permalinkUrl); + alert('OBS link copied to clipboard ' + permalinkUrl); + }) + });