-
Notifications
You must be signed in to change notification settings - Fork 277
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
use YT player API to force autoplay, resolving double-tap bug #109
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
a652608
YFix double tap issue on Mobile
paulirish b5575bc
Merge branch 'master' into dantovbein/master
paulirish 4667952
refactor and introduce forceautoplay option
paulirish 0461e74
flip on yt api path for firefox and safari only
paulirish 62724cd
dont eagerly fetch yt api. only get after click
paulirish 53b22c6
multiple embeds test page
paulirish f3b132e
Merge branch 'master' into ytapi
paulirish 185af94
Support Android user agent for YT JS API
codepuncher 1fa89dd
no preventDefault needed with the new href removal
paulirish 1f885c2
more specificity on the autoplay useragent checks
paulirish 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,11 +53,13 @@ class LiteYTEmbed extends HTMLElement { | |
// TODO: In the future we could be like amp-youtube and silently swap in the iframe during idle time | ||
// We'd want to only do this for in-viewport or near-viewport ones: https://github.com/ampproject/amphtml/pull/5003 | ||
this.addEventListener('click', this.addIframe); | ||
} | ||
|
||
// // TODO: Support the the user changing the [videoid] attribute | ||
// attributeChangedCallback() { | ||
// } | ||
// Chrome & Edge desktop have no problem with the basic YouTube Embed with ?autoplay=1 | ||
// However Safari desktop and most/all mobile browsers do not successfully track the user gesture of clicking through the creation/loading of the iframe, | ||
// so they don't autoplay automatically. Instead we must load an additional 2 sequential JS files (1KB + 165KB) (un-br) for the YT Player API | ||
// TODO: Try loading the the YT API in parallel with our iframe and then attaching/playing it. #82 | ||
this.needsYTApiForAutoplay = navigator.vendor.includes('Apple') || navigator.userAgent.includes('Mobi'); | ||
} | ||
|
||
/** | ||
* Add a <link rel={preload | preconnect} ...> to the head | ||
|
@@ -96,13 +98,53 @@ class LiteYTEmbed extends HTMLElement { | |
LiteYTEmbed.preconnected = true; | ||
} | ||
|
||
addIframe(e) { | ||
fetchYTPlayerApi() { | ||
if (window.YT || (window.YT && window.YT.Player)) return; | ||
|
||
this.ytApiPromise = new Promise((res, rej) => { | ||
var el = document.createElement('script'); | ||
el.src = 'https://www.youtube.com/iframe_api'; | ||
el.async = true; | ||
el.onload = _ => { | ||
YT.ready(res); | ||
}; | ||
el.onerror = rej; | ||
this.append(el); | ||
}); | ||
} | ||
|
||
async addYTPlayerIframe(params) { | ||
this.fetchYTPlayerApi(); | ||
await this.ytApiPromise; | ||
|
||
const videoPlaceholderEl = document.createElement('div') | ||
this.append(videoPlaceholderEl); | ||
|
||
const paramsObj = Object.fromEntries(params.entries()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. im a little scared this newish feature will pose problems with various people's tsc/acorn/estree versions, if they're too old. but hopefully its fine. |
||
|
||
new YT.Player(videoPlaceholderEl, { | ||
width: '100%', | ||
videoId: this.videoId, | ||
playerVars: paramsObj, | ||
events: { | ||
'onReady': event => { | ||
event.target.playVideo(); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
async addIframe(){ | ||
if (this.classList.contains('lyt-activated')) return; | ||
e.preventDefault(); | ||
this.classList.add('lyt-activated'); | ||
|
||
const params = new URLSearchParams(this.getAttribute('params') || []); | ||
params.append('autoplay', '1'); | ||
params.append('playsinline', '1'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
if (this.needsYTApiForAutoplay) { | ||
return this.addYTPlayerIframe(params); | ||
} | ||
|
||
const iframeEl = document.createElement('iframe'); | ||
iframeEl.width = 560; | ||
|
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,25 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<title>lite-youtube-embed</title> | ||
<meta charset="utf-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1" /> | ||
<link rel="stylesheet" href="../src/lite-yt-embed.css" /> | ||
</head> | ||
<body> | ||
<h1>multiple embeds</h1> | ||
|
||
<lite-youtube videoid="ogfYd705cRs" playlabel="Play: Keynote (Google I/O '18)"></lite-youtube> | ||
|
||
|
||
<lite-youtube videoid="n57U2_-3NLQ" playlabel="Play: Chrome Dev Summit 2021 recap"></lite-youtube> | ||
|
||
|
||
<script src="../src/lite-yt-embed.js"></script> | ||
|
||
<!-- <script> | ||
// Simulate duplicate iframe problem. https://github.com/paulirish/lite-youtube-embed/issues/92 | ||
document.body.addEventListener('click', e => e.target.click()); | ||
</script> --> | ||
</body> | ||
</html> |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
alsoo wondering if there's a lazy way to attach... #82 hmmm!
EDIT: see also #146 (comment)