-
Notifications
You must be signed in to change notification settings - Fork 406
Make YouTube Embeds GDPR compliant #3253
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
e4a1a49
Make markdown youtube embeds gdpr compliant
julieg18 a10c1d1
Make Video youtube embeds gdpr compliant
julieg18 ed03a29
Fix bugs
julieg18 8f84447
Fix typo
julieg18 4ff8cbe
Add missing controls to md yt embeds
julieg18 4388b0e
convert custom yt js func to hook
julieg18 8eecca8
Add missing href attributes to yt tooltip links
julieg18 c1fa1a4
Fix ts error
julieg18 644fca6
fix cursor on tooltip
julieg18 c88e09e
Add remove click listeners
julieg18 eabc30d
Set up local storage
julieg18 5e25a5d
Refactor custom-yt-embedder
julieg18 3d03fcf
Fix bug
julieg18 6b2af71
Keep embed links style consistent
julieg18 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.