Skip to content

Commit

Permalink
feat: strip tracking params from youtube links
Browse files Browse the repository at this point in the history
  • Loading branch information
vyneer committed Dec 27, 2023
1 parent dac7c0b commit 6568439
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
11 changes: 9 additions & 2 deletions assets/chat/js/formatters/UrlFormatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ export default class UrlFormatter {
const decodedUrl = self.elem.html(url).text();
const m = decodedUrl.match(self.linkregex);
if (m) {
const encodedUrl = self.encodeUrl(m[0]);
const normalizedUrl = this.normalizeUrl(encodedUrl);
const normalizedUrl = self.encodeUrl(this.normalizeUrl(m[0]));

let embedHashLink = '';
try {
Expand Down Expand Up @@ -114,6 +113,14 @@ export default class UrlFormatter {
return url.split('?')[0];
}

if (/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;
}

return url;
}
}
32 changes: 31 additions & 1 deletion assets/chat/js/formatters/UrlFormatter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,37 @@ describe('Normalizing URLs', () => {
);
});

test("Don't modify a URL that isn't Twitter or X", () => {
test('Remove the share tracking query param from a youtube.com link', () => {
expect(
urlFormatter.normalizeUrl(
'https://www.youtube.com/live/2NjXXQYtUNY?si=5ALpT28ptRec6T7u&t=70',
),
).toBe('https://www.youtube.com/live/2NjXXQYtUNY?t=70');
});

test('Remove the share tracking query param from a youtu.be link', () => {
expect(
urlFormatter.normalizeUrl(
'https://youtu.be/SbPP1i6INPk?si=K0qpdHBGOIJ-gBMK&t=60',
),
).toBe('https://youtu.be/SbPP1i6INPk?t=60');
});

test("Don't modify a youtube.com link that doesn't contain the share tracking query param", () => {
expect(
urlFormatter.normalizeUrl(
'https://www.youtube.com/live/2NjXXQYtUNY?t=70',
),
).toBe('https://www.youtube.com/live/2NjXXQYtUNY?t=70');
});

test("Don't modify a youtu.be link that doesn't contain the share tracking query param", () => {
expect(urlFormatter.normalizeUrl('https://youtu.be/SbPP1i6INPk?t=60')).toBe(
'https://youtu.be/SbPP1i6INPk?t=60',
);
});

test("Don't modify a URL that isn't Twitter, X or YouTube", () => {
expect(
urlFormatter.normalizeUrl('https://www.twitch.tv/search?term=vtuber'),
).toBe('https://www.twitch.tv/search?term=vtuber');
Expand Down

0 comments on commit 6568439

Please sign in to comment.