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

Add timestamp support to youtube embeds #312

Merged
merged 4 commits into from
Sep 1, 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
2 changes: 1 addition & 1 deletion assets/chat/js/formatters/UrlFormatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export default class UrlFormatter {

let embedHashLink = '';
try {
embedHashLink = this.hashLinkConverter.convert(normalizedUrl);
embedHashLink = this.hashLinkConverter.convert(decodedUrl);
} catch (err) {
// ignore
}
Expand Down
11 changes: 9 additions & 2 deletions assets/chat/js/hashlinkconverter.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class HashLinkConverter {
const pathname = url.pathname.slice(1);
let match;
let videoId;
let timestamp;
switch (url.hostname) {
case 'www.twitch.tv':
case 'twitch.tv':
Expand All @@ -45,13 +46,19 @@ class HashLinkConverter {
return `#youtube/${match[1]}`;
}
videoId = url.searchParams.get('v');
timestamp = url.searchParams.get('t');
if (!videoId) {
throw new Error(MISSING_VIDEO_ID_ERROR);
}
return `#youtube/${videoId}`;
return timestamp
? `#youtube/${videoId}?t=${timestamp}`
: `#youtube/${videoId}`;
case 'www.youtu.be':
case 'youtu.be':
return `#youtube/${pathname}`;
timestamp = url.searchParams.get('t');
return timestamp
? `#youtube/${pathname}?t=${timestamp}`
: `#youtube/${pathname}`;
case 'www.rumble.com':
case 'rumble.com':
match = pathname.match(this.rumbleEmbedRegex);
Expand Down
10 changes: 10 additions & 0 deletions assets/chat/js/hashlinkconverter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ describe('Valid embeds', () => {
'https://youtu.be/dPmLveKE_wY',
'#youtube/dPmLveKE_wY',
],
[
'Youtube video with timestamp',
'https://www.youtube.com/watch?v=tZ_gn0E87Qo&t=5',
'#youtube/tZ_gn0E87Qo?t=5',
],
[
'Youtube video shortened link with timestamp',
'https://youtu.be/dPmLveKE_wY?t=5',
'#youtube/dPmLveKE_wY?t=5',
],
[
'Youtube live stream shareable link',
'https://www.youtube.com/live/jfKfPfyJRdk?feature=share',
Expand Down