-
Notifications
You must be signed in to change notification settings - Fork 394
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make YouTube Embeds GDPR compliant (#3253)
* Make all youtube embeds gdpr compliant with on hover message * add custom youtube transformer to gatsby-remark-embedder * add needed js and css code in doc/blog components
- Loading branch information
Showing
9 changed files
with
328 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
const shouldTransform = url => { | ||
const { host, pathname, searchParams } = new URL(url) | ||
|
||
return ( | ||
host === 'youtu.be' || | ||
(['youtube.com', 'www.youtube.com'].includes(host) && | ||
pathname.includes('/watch') && | ||
Boolean(searchParams.get('v'))) | ||
) | ||
} | ||
|
||
const getTimeValueInSeconds = timeValue => { | ||
if (Number(timeValue).toString() === timeValue) { | ||
return timeValue | ||
} | ||
|
||
const { | ||
1: hours = '0', | ||
2: minutes = '0', | ||
3: seconds = '0' | ||
} = timeValue.match(/(?:(\d*)h)?(?:(\d*)m)?(?:(\d*)s)?/) | ||
|
||
return String((Number(hours) * 60 + Number(minutes)) * 60 + Number(seconds)) | ||
} | ||
|
||
const getYouTubeIFrameSrc = urlString => { | ||
const url = new URL(urlString) | ||
const id = | ||
url.host === 'youtu.be' ? url.pathname.slice(1) : url.searchParams.get('v') | ||
|
||
const embedUrl = new URL( | ||
`https://www.youtube-nocookie.com/embed/${id}?rel=0&&showinfo=0;` | ||
) | ||
|
||
url.searchParams.forEach((value, name) => { | ||
if (name === 'v') { | ||
return | ||
} | ||
|
||
if (name === 't') { | ||
embedUrl.searchParams.append('start', getTimeValueInSeconds(value)) | ||
} else { | ||
embedUrl.searchParams.append(name, value) | ||
} | ||
}) | ||
return embedUrl.toString() | ||
} | ||
|
||
// all code above taken from gatsby-remark-embedder (https://github.com/MichaelDeBoey/gatsby-remark-embedder) | ||
|
||
const name = 'YouTubeCustom' | ||
|
||
const getHTML = url => { | ||
const iframeSrc = getYouTubeIFrameSrc(url) | ||
|
||
return `<div class="yt-embed-wrapper"><iframe width="100%" height="315" src="${iframeSrc}" frameBorder="0" allow="autoplay; encrypted-media; gyroscope; picture-in-picture" allowFullScreen></iframe><div className="yt-embed-wrapper__overlay"><span class="yt-embed-wrapper__tooltip">By clicking play, you agree to YouTube's <a href="https://policies.google.com/u/3/privacy?hl=en" target="_blank" rel="nofollow noopener noreferrer">Privacy Policy</a> and <a href="https://www.youtube.com/static?template=terms" target="_blank" rel="nofollow noopener noreferrer">Terms of Service</a></span></div></div>` | ||
} | ||
|
||
module.exports = { getHTML, name, shouldTransform } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
plugins/gatsby-theme-iterative-docs/src/utils/front/useCustomYtEmbeds.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { useEffect } from 'react' | ||
|
||
const hideAllEmbedOverlays = (embeds: NodeListOf<Element>) => { | ||
embeds.forEach(embed => { | ||
const overlay = embed.querySelector('.yt-embed-wrapper__overlay') | ||
overlay?.classList.add('hidden') | ||
}) | ||
} | ||
|
||
const setUpEmbedClickListeners = (embeds: NodeListOf<Element>) => { | ||
const removeClickListeners: Array<() => void> = [] | ||
|
||
embeds.forEach(embed => { | ||
const iframe = embed.querySelector('iframe') | ||
const overlay = embed.querySelector('.yt-embed-wrapper__overlay') | ||
const tooltip = embed.querySelector('.yt-embed-wrapper__tooltip') | ||
|
||
const handleOverlayClick = (event: MouseEvent) => { | ||
if (event.target === tooltip || tooltip?.contains(event.target as Node)) { | ||
return | ||
} | ||
|
||
if (iframe && iframe.src) { | ||
iframe.src = iframe?.src + `&autoplay=1` | ||
} | ||
hideAllEmbedOverlays(embeds) | ||
localStorage.setItem('yt-embed-consent', 'true') | ||
} | ||
const removeListener = () => { | ||
overlay?.removeEventListener('click', handleOverlayClick as EventListener) | ||
} | ||
overlay?.addEventListener('click', handleOverlayClick as EventListener) | ||
removeClickListeners.push(removeListener) | ||
}) | ||
|
||
return () => { | ||
removeClickListeners.forEach(rmListener => rmListener()) | ||
} | ||
} | ||
|
||
const useCustomYtEmbeds = () => { | ||
useEffect(() => { | ||
const hasUserGivenConsent = Boolean( | ||
localStorage.getItem('yt-embed-consent') | ||
) | ||
const embeds = document.querySelectorAll('.yt-embed-wrapper') | ||
|
||
if (hasUserGivenConsent) { | ||
hideAllEmbedOverlays(embeds) | ||
return | ||
} | ||
|
||
const cleanUpEventListeners = setUpEmbedClickListeners(embeds) | ||
|
||
return () => { | ||
cleanUpEventListeners() | ||
} | ||
}, []) | ||
} | ||
|
||
export default useCustomYtEmbeds |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.