Skip to content

Commit

Permalink
KYAN-298 Add cookie option to YouTube videos and set nocookie as defa…
Browse files Browse the repository at this point in the history
…ult (#511)
  • Loading branch information
botondmezei authored Oct 25, 2024
1 parent 0a4a627 commit 85f7d72
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ public class VideoComponent {
@Default(values = StringUtils.EMPTY)
private String youtubeLink;

@ValueMapValue
@Default(booleanValues = false)
private boolean enableCookies;

@ValueMapValue
@Default(values = StringUtils.EMPTY)
private String vimeoLink;
Expand Down Expand Up @@ -120,7 +124,7 @@ private void init() {
}

switch (this.source) {
case ("youtube") -> this.src = VideoLinkParser.getYouTubeLink(youtubeLink);
case ("youtube") -> this.src = VideoLinkParser.getYouTubeLink(youtubeLink, enableCookies);
case ("vimeo") -> this.src = VideoLinkParser.getVimeoLink(vimeoLink);
default -> this.src = StringUtils.EMPTY;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ public class VideoLinkParser {
"(?:embed|vi?)/([^/?]*)", "(?:user/[A-Za-z0-9]*#[a-z]/[a-z]/[a-z0-9]*/*)/([^&]*)",
"^([A-Za-z0-9\\-]*)"};
private static final String YOUTUBE_PLAYER_URL_PREFIX = "https://www.youtube.com/embed/";

private static final String YOUTUBE_PLAYER_URL_NOCOOKIE_PREFIX = "https://www.youtube-nocookie.com/embed/";

private static final String[] VIMEO_VIDEO_ID_REGEX = {
"(?:video|channels|groups/name/videos)/([^/?]*)",
"^([A-Za-z0-9\\-]*)"};
Expand All @@ -36,12 +39,15 @@ public class VideoLinkParser {
+ "(www.)?(m.)?((vimeo.com)|(player.vimeo.com))/";
private static final String VIMEO_PLAYER_URL_PREFIX = "https://player.vimeo.com/video/";

public static String getYouTubeLink(String link) {
public static String getYouTubeLink(String link, boolean enableCookies) {
if (isYouTubeFullUrl(link)) {
link = extractVideoIdFromUrl(link, YOUTUBE_VIDEO_ID_REGEX, YOUTUBE_URL_REGEX);
}
if (enableCookies) {
return YOUTUBE_PLAYER_URL_PREFIX + link;
}

return YOUTUBE_PLAYER_URL_PREFIX + link;
return YOUTUBE_PLAYER_URL_NOCOOKIE_PREFIX + link;
}

public static String getVimeoLink(String link) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@
"name": "youtubeLink",
"label": "Youtube link"
},
"enableCookies": {
"sling:resourceType": "wcm/dialogs/components/toggle",
"description": "By default all links will be generated using the youtube-nocookie.com domain, but if this property is set to true, it will generated with simply youtube.com",
"name": "enableCookies",
"label": "Enable cookies"
},
"ws:display": {
"condition": {
"sourceName": "source",
Expand Down

0 comments on commit 85f7d72

Please sign in to comment.