Embedded video from youtube.com/embed/ won't load after consent

Created on 15 April 2023, about 1 year ago
Updated 31 October 2023, 8 months ago

Problem/Motivation

When embedding a video from youtube.com/embed/, the video won't load after giving consent. Instead the embedded youtube media player says that the video is unavailable.

Steps to reproduce

  1. Install the GDPR for Youtube and Vimeo module.
  2. Embed a video on a page via
    <iframe frameborder="0" height="315" src="https://www.youtube.com/embed/EXAMPLE" width="560"></iframe>
  3. Open the page and give consent to the message shown by this module

Instead of the video starting to play, youtube shows an error message.

Proposed resolution

I did some quick research and the issue seems to be that after consent, the iframe src parameter is altered slightly by the module by adding additional parameters via JavaScript. However, the playlist parameter has mistakes which results in youtube displaying an error message.
Specifically, the URL to load is altered to
https://www.youtube.com/embed/EXAMPLE?rel=0&loop=1&playlist=embed/EXAMPLE
It should be
https://www.youtube.com/embed/EXAMPLE?rel=0&loop=1&playlist=EXAMPLE

The issue is that the variable vid holds embed/EXAMPLE instead of EXAMPLE (only the video ID). This should probably be solved by correcting the regex in line 52 of gdpr_video.js
However as I'm lacking overview of the different potential URLs and didn't have more time I solved this for me with a quick workaround:
Replace
const vid = match[2] (line 70) with
const vid = match[2].startsWith('embed/') ? match[2].substring(6) : match[2]

Remarks

Thanks for the module and for the general idea of adding these extra parameters to reduce distractions!

πŸ› Bug report
Status

Active

Version

1.1

Component

Code

Created by

πŸ‡©πŸ‡ͺGermany holybiber Berlin

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @holybiber
  • πŸ‡©πŸ‡ͺGermany marcoka

    Info for autoplay of youtube-videos taken from googles docs

    To make an embedded video autoplay, add "&autoplay=1" to the video's embed code right after the video ID (the series of letters that follows "embed/"). Embedded videos that are autoplayed don't increment video views.

  • πŸ‡©πŸ‡ͺGermany marcoka

    Ok this is how you can achieve that.
    I also switched the domain to cookieless "youtube-nocookie.com" in this example

    1. pd7_radix: is the themes name
    2. This code goes to the pd7_radix.theme
    3. Only works for oembed

    function pd7_radix_preprocess_media_oembed_iframe(&$variables) {
    // Pull iframe tag
    $iFrame = $variables['media']->__toString();
    // Switch domain to the youtube nocookie domain.
    if (stripos($iFrame, 'youtube.com') !== false){
    $newFrame = str_replace('youtube.com/', 'youtube-nocookie.com/', $iFrame);
    $autoplay = str_replace('?feature=oembed', '?feature=oembed&autoplay=1', $iFrame);
    $variables['media'] = IFrameMarkup::create($autoplay);
    }
    }

    Using that the video will autoplay when you click ok.

  • πŸ‡©πŸ‡ͺGermany marcoka

    I did some experiments with that code. It seems sometimes the video is still loaded even if the user does not click ok.
    In that case you can "hear" it because its on autoplay.

Production build 0.69.0 2024