@@ -18,14 +18,19 @@ public function detect(): ?EmbedCode
18
18
private function fallback (): ?EmbedCode
19
19
{
20
20
$ path = $ this ->extractor ->getUri ()->getPath ();
21
+ $ parent = $ this ->extractor ->getSetting ('twitch:parent ' );
21
22
22
23
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 );
24
27
return new EmbedCode ($ code , 620 , 378 );
25
28
}
26
29
27
30
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 );
29
34
return new EmbedCode ($ code , 620 , 378 );
30
35
}
31
36
@@ -50,7 +55,7 @@ private static function getChannelId(string $path): ?string
50
55
return null ;
51
56
}
52
57
53
- private static function generateCode (array $ params ): string
58
+ private static function generateIframeCode (array $ params ): string
54
59
{
55
60
$ query = http_build_query (['autoplay ' => 'false ' ] + $ params );
56
61
@@ -63,4 +68,15 @@ private static function generateCode(array $params): string
63
68
'width ' => 620 ,
64
69
]);
65
70
}
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
+ }
66
82
}
0 commit comments