Skip to content

Commit 1881082

Browse files
author
Ibrahim Cesar
authored
Merge pull request #6 from elbotho/add-nocookie-option
add option to use privacy enhanced mode (nocookie)
2 parents 99c22c8 + a0a71d6 commit 1881082

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

β€ŽREADME.mdβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ const App = () => (
4949
playlist={false} // Use true when your ID be from a playlist
5050
poster="hqdefault" // Defines the image size to call on first render as poster image. Possible values are "default","mqdefault", "hqdefault", "sddefault" and "maxresdefault". Default value for this prop is "hqdefault". Please be aware that "sddefault" and "maxresdefault", high resolution images are not always avaialble for every video. See: https://stackoverflow.com/questions/2068344/how-do-i-get-a-youtube-video-thumbnail-from-the-youtube-api
5151
title="YouTube Embed" // a11y, always provide a title for iFrames: https://dequeuniversity.com/tips/provide-iframe-titles Help the web be accessible ;)
52+
noCookie={true} //Default false, connect to YouTube via the Privacy-Enhanced Mode using https://www.youtube-nocookie.com
5253
/>
5354
</div>
5455
);

β€Ždist/LiteYouTubeEmbed.jsβ€Ž

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ var LiteYouTubeEmbed = function LiteYouTubeEmbed(_ref) {
88
playlist = _ref.playlist,
99
poster = _ref.poster,
1010
title = _ref.title,
11+
noCookie = _ref.noCookie,
1112
activatedClass = _ref.activatedClass,
1213
iframeClass = _ref.iframeClass,
1314
playerClass = _ref.playerClass,
@@ -26,7 +27,8 @@ var LiteYouTubeEmbed = function LiteYouTubeEmbed(_ref) {
2627
var videoId = encodeURIComponent(id);
2728
var videoTitle = title;
2829
var posterUrl = "https://i.ytimg.com/vi/".concat(videoId, "/").concat(poster, ".jpg");
29-
var iframeSrc = !playlist ? "https://www.youtube.com/embed/".concat(videoId, "?autoplay=1") : "https://www.youtube.com/embed/videoseries?list=".concat(videoId);
30+
var ytUrl = noCookie ? "https://www.youtube-nocookie.com" : "https://www.youtube.com";
31+
var iframeSrc = !playlist ? "".concat(ytUrl, "/embed/").concat(videoId, "?autoplay=1") : "".concat(ytUrl, "/embed/videoseries?list=").concat(videoId);
3032
var refVideo = useRef();
3133

3234
var warmConnections = function warmConnections() {
@@ -55,7 +57,7 @@ var LiteYouTubeEmbed = function LiteYouTubeEmbed(_ref) {
5557
as: "image"
5658
}), React.createElement(React.Fragment, null, preconnected && React.createElement(React.Fragment, null, React.createElement("link", {
5759
rel: "preconnect",
58-
href: "https://www.youtube.com"
60+
href: ytUrl
5961
}), React.createElement("link", {
6062
rel: "preconnect",
6163
href: "https://www.google.com"
@@ -89,6 +91,7 @@ LiteYouTubeEmbed.defaultProps = {
8991
playlist: false,
9092
poster: "hqdefault",
9193
title: "YouTube Embed",
94+
noCookie: false,
9295
activatedClass: "lyt-activated",
9396
iframeClass: "",
9497
playerClass: "lty-playbtn",

β€Žsrc/lib/LiteYouTubeEmbed.jsβ€Ž

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,20 @@ import React, { Fragment, useState, useRef, useEffect } from "react";
22
import PropTypes from "prop-types";
33
import "./LiteYouTubeEmbed.css";
44

5-
const LiteYouTubeEmbed = ({ adNetwork, id, playlist, poster, title, activatedClass, iframeClass, playerClass, wrapperClass
5+
const LiteYouTubeEmbed = ({ adNetwork, id, playlist, poster, title, noCookie, activatedClass, iframeClass, playerClass, wrapperClass
66
}) => {
77

88
const [preconnected, setPreconnected] = useState(false);
99
const [iframe, setIframe] = useState(false);
1010
const videoId = encodeURIComponent(id);
1111
const videoTitle = title;
1212
const posterUrl = `https://i.ytimg.com/vi/${videoId}/${poster}.jpg`;
13-
const iframeSrc = !playlist ? `https://www.youtube.com/embed/${videoId}?autoplay=1` : `https://www.youtube.com/embed/videoseries?list=${videoId}`;
13+
const ytUrl = noCookie
14+
? "https://www.youtube-nocookie.com"
15+
: "https://www.youtube.com";
16+
const iframeSrc = !playlist
17+
? `${ytUrl}/embed/${videoId}?autoplay=1`
18+
: `${ytUrl}/embed/videoseries?list=${videoId}`;
1419
const refVideo = useRef();
1520

1621
const warmConnections = () => {
@@ -40,7 +45,7 @@ const LiteYouTubeEmbed = ({ adNetwork, id, playlist, poster, title, activatedCla
4045
<>
4146
{preconnected && (
4247
<>
43-
<link rel="preconnect" href="https://www.youtube.com" />
48+
<link rel="preconnect" href={ytUrl} />
4449
<link rel="preconnect" href="https://www.google.com" />
4550
{adNetwork && (
4651
<>
@@ -81,22 +86,24 @@ LiteYouTubeEmbed.propTypes = {
8186
playlist: PropTypes.bool,
8287
poster: PropTypes.string,
8388
title: PropTypes.string,
89+
noCookie: PropTypes.bool,
8490
activatedClass: PropTypes.string,
8591
iframeClass: PropTypes.string,
8692
playerClass: PropTypes.string,
8793
wrapperClass: PropTypes.string
8894
};
8995

9096
LiteYouTubeEmbed.defaultProps = {
91-
adNetwork: true,
97+
adNetwork: true,
9298
id: "",
9399
playlist: false,
94100
poster: "hqdefault",
95101
title: "YouTube Embed",
102+
noCookie: false,
96103
activatedClass: "lyt-activated",
97104
iframeClass: "",
98105
playerClass: "lty-playbtn",
99-
wrapperClass: "yt-lite"
106+
wrapperClass: "yt-lite",
100107
};
101108

102109
export default LiteYouTubeEmbed;

0 commit comments

Comments
Β (0)