Last active
January 29, 2021 08:38
-
-
Save Chester97/88830aa383d99e6acc045db44c432d61 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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