@@ -18,14 +18,19 @@ public function detect(): ?EmbedCode
1818 private function fallback (): ?EmbedCode
1919 {
2020 $ path = $ this ->extractor ->getUri ()->getPath ();
21+ $ parent = $ this ->extractor ->getSetting ('twitch:parent ' );
2122
2223 if ($ id = self ::getVideoId ($ path )) {
23- $ code = self ::generateCode (['video ' => "v {$ id }" ]);
24+ $ code = $ parent
25+ ? self ::generateIframeCode (['id ' => $ id , 'parent ' => $ parent ])
26+ : self ::generateJsCode ('video ' , $ id );
2427 return new EmbedCode ($ code , 620 , 378 );
2528 }
2629
2730 if ($ id = self ::getChannelId ($ path )) {
28- $ code = self ::generateCode (['channel ' => $ id ]);
31+ $ code = $ parent
32+ ? self ::generateIframeCode (['channel ' => $ id , 'parent ' => $ parent ])
33+ : self ::generateJsCode ('channel ' , $ id );
2934 return new EmbedCode ($ code , 620 , 378 );
3035 }
3136
@@ -50,7 +55,7 @@ private static function getChannelId(string $path): ?string
5055 return null ;
5156 }
5257
53- private static function generateCode (array $ params ): string
58+ private static function generateIframeCode (array $ params ): string
5459 {
5560 $ query = http_build_query (['autoplay ' => 'false ' ] + $ params );
5661
@@ -63,4 +68,15 @@ private static function generateCode(array $params): string
6368 'width ' => 620 ,
6469 ]);
6570 }
71+
72+ private static function generateJsCode ($ key , $ value )
73+ {
74+ return <<<HTML
75+ <div id="twitch-embed"></div>
76+ <script src="https://player.twitch.tv/js/embed/v1.js"></script>
77+ <script type="text/javascript">
78+ new Twitch.Player("twitch-embed", { {$ key }: " {$ value }" });
79+ </script>
80+ HTML ;
81+ }
6682}
0 commit comments