@@ -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