Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strip tracking params from YouTube links #388

Merged
merged 1 commit into from
Dec 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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