Skip to content

Commit b363814

Browse files
committed
improved twitch code. fixes #384
1 parent a9b9ffc commit b363814

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -330,10 +330,10 @@ If you need to pass settings to your detectors, you can use the `setSettings` me
330330
//Create the extractor
331331
$info = $embed->get($url);
332332

333-
//Pass settings for example.com site
334-
if ($info->getUri()->getHost() === 'example.com') {
335-
$info->setSettings(['example_api_key' => 'xxx']);
336-
}
333+
$info->setSettings([
334+
'oembed:query_parameters' => [] //Extra parameters send to oembed
335+
'twitch:parent' => 'example.com' //Required to embed twitch videos as iframe
336+
]);
337337
```
338338

339339
Note: The built-in detectors does not require settings. This feature is only for convenience if you create a specific detector that requires settings.

demo/index.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,9 @@ function printCode(?string $code, bool $asHtml = true): void
194194
'Cache-Control' => 'max-age=0,no-cache',
195195
]);
196196
$info = $embed->get(getUrl());
197+
$info->setSettings([
198+
'twitch:parent' => $_SERVER['SERVER_NAME'] === 'localhost' ? null : $_SERVER['SERVER_NAME']
199+
]);
197200
} catch (Exception $exception) {
198201
echo '<pre>';
199202
echo $exception;

src/Adapters/Twitch/Detectors/Code.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)