Skip to content

Commit ed0da37

Browse files
committed
[GolemBridge] Fix ordering of embeds
By going through all embeds in the JavaScript source. Due to complexity with e.g. the Twitter embed, still only YouTube is supported (now at the right placeholder place). Test page (also with a Twitter embed): https://www.golem.de/news/stornierung-sam-altman-bekommt-anzahlung-fuer-tesla-roadster-nicht-zurueck-2511-201749.html Previous test page with multiple YouTube embeds: https://www.golem.de/news/free-to-play-kostenlos-skaten-ballern-und-klicken-2510-201000.html
1 parent 73973e3 commit ed0da37

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

bridges/GolemBridge.php

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -115,20 +115,29 @@ private function extractContent($page, $prevcontent)
115115

116116
$article = $page->find('article', 0);
117117

118-
//built youtube iframes
118+
// extract embeds from script tags (unfortunately no JSON)
119+
$embedSrcs = [];
120+
foreach ($page->find('script') as $script) {
121+
// Ungreedy match to get precisely the snippet of one embed
122+
if (preg_match_all('/type:\s*\"Embed(.*)urlPrivacy:/U', $script, $embeds)) {
123+
foreach ($embeds[1] as $embed) {
124+
if (preg_match('/src:\s*\"([^\"]+)\"/', $embed, $src)) {
125+
$embedSrcs[] = $src[1];
126+
}
127+
}
128+
}
129+
}
130+
// inject the embed into the HTML placeholder
119131
$placeholders = $article->find('.go-embed-container');
120132
foreach (range(0, count($placeholders) - 1) as $i) {
121-
foreach ($page->find('script') as $ytscript) {
122-
if (preg_match_all('/(www.youtube.com.*?)\"/', $ytscript->innertext, $link)) {
123-
if (array_key_exists($i, $link[1]) && array_key_exists($i, $placeholders)) {
124-
$link = 'https://' . str_replace('\\', '', $link[1][$i]);
125-
$placeholders[$i]->innertext .= <<<EOT
126-
<iframe width="560" height="315" src="$link" title="YouTube video player" frameborder="0"
127-
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share"
128-
referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>';
129-
EOT;
130-
break;
131-
}
133+
if (array_key_exists($i, $embedSrcs)) {
134+
$src = $embedSrcs[$i];
135+
if (preg_match('/youtube(-nocookie)?\.com/', $src, $match)) {
136+
$placeholders[$i]->innertext = <<<EOT
137+
<iframe width="560" height="315" src="$src" title="YouTube video player" frameborder="0"
138+
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share"
139+
referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>';
140+
EOT;
132141
}
133142
}
134143
}

0 commit comments

Comments
 (0)