Skip to content

Instantly share code, notes, and snippets.

@Chester97
Last active January 29, 2021 08:38
Show Gist options
  • Save Chester97/88830aa383d99e6acc045db44c432d61 to your computer and use it in GitHub Desktop.
Save Chester97/88830aa383d99e6acc045db44c432d61 to your computer and use it in GitHub Desktop.
enum EmbedTypes {
CLIP = 'clip',
VIDEOS = 'videos',
}
const twitchRegex: RegExp = /[^/]*$/;
const twitchEmbedId = (url: string): string => {
const value = twitchRegex.exec(url);
return value ? value[0] : '';
};
const buildTwitchEmbedUrl = (url: string): string => {
if (!url) return '';
if (url.includes(`/${EmbedTypes.VIDEOS}/`)) // TU DA SIĘ LEPIEJ
return `https://player.twitch.tv/?video=${twitchEmbedId(
url,
)}&parent=localhost&autoplay=true`;
if (url.includes(`/${EmbedTypes.CLIP}/`)) // TU TAK SAMO
return `https://clips.twitch.tv/embed?clip=${twitchEmbedId(
url,
)}&parent=localhost&autoplay=true`;
return `https://player.twitch.tv/?channel=${twitchEmbedId(
url,
)}&parent=localhost`;
};
export default buildTwitchEmbedUrl;
// URL's
// 1. https://www.twitch.tv/mrs_honey (channel - live_stream)
// ZAMIANA - https://player.twitch.tv/?channel=mrs_honey&parent=www.example.com
// 2. https://www.twitch.tv/videos/886723557 (videos)
// ZAMIANA - https://player.twitch.tv/?video=886053125&parent=www.example.com
// 3. https://www.twitch.tv/forsen/clip/EphemeralAbrasiveCamelTebowing (clip)
// ZMIANA - https://clips.twitch.tv/embed?clip=AmorphousVivaciousPancakeTooSpicy&parent=www.example.com
// FLOW
// twitchEmbedId - will return a unique id/username
// buildTwitchEmbedUrl - will create an iframe src url based on twitchEmbedId
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment