From 67fa2d4ff592bfc2e52aba14f9079c294d665b81 Mon Sep 17 00:00:00 2001 From: vyneer <41237021+vyneer@users.noreply.github.com> Date: Sun, 31 Dec 2023 10:15:30 +0300 Subject: [PATCH] Fix bad YouTube links breaking chat (#390) * fix: ignore url errors while trying to normalize yt links * fix: make youtube url regex stricter --- assets/chat/js/formatters/UrlFormatter.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/assets/chat/js/formatters/UrlFormatter.js b/assets/chat/js/formatters/UrlFormatter.js index 6a69e8c6..9cfb28cb 100644 --- a/assets/chat/js/formatters/UrlFormatter.js +++ b/assets/chat/js/formatters/UrlFormatter.js @@ -113,12 +113,16 @@ export default class UrlFormatter { return url.split('?')[0]; } - if (/youtu(?:be\.com|\.be)/i.test(url)) { + if (/^(?:(?:https|http):\/\/)?(?:www\.)?youtu(?:be\.com|\.be)/i.test(url)) { // Same as with xeets, remove the nasty share tracking query param // from YouTube links. - const ytLink = new URL(url); - ytLink.searchParams.delete('si'); - return ytLink.href; + try { + const ytLink = new URL(url); + ytLink.searchParams.delete('si'); + return ytLink.href; + } catch { + return url; + } } return url;