Skip to content
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

Support Android user agent for YT JS API #122

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# https://editorconfig.org

root = true

[*]
charset = utf-8
end_of_line = lf
trim_trailing_whitespace = true

[src/]
indent_size = 4
indent_style = space
insert_final_newline = true
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ Use this as your HTML, load the script asynchronously, and let the JS progressiv

```html
<lite-youtube videoid="ogfYd705cRs" style="background-image: url('https://i.ytimg.com/vi/ogfYd705cRs/hqdefault.jpg');">
<button type="button" class="lty-playbtn">
<a href="https://youtube.com/watch?v=ogfYd705cRs" class="lty-playbtn" title="Play Video">
<span class="lyt-visually-hidden">Play Video: Keynote (Google I/O '18)</span>
</button>
</a>
</lite-youtube>
```

Expand Down
1 change: 1 addition & 0 deletions src/lite-yt-embed.css
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ lite-youtube > iframe {

/* play button */
lite-youtube > .lty-playbtn {
display: block;
width: 68px;
height: 48px;
position: absolute;
Expand Down
13 changes: 7 additions & 6 deletions src/lite-yt-embed.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class LiteYTEmbed extends HTMLElement {
// However Safari and Firefox 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 300KB (ungz) of JS for the YT Player API
// TODO: chrome android seems to also need this
this.needsYTApiForAutoplay = navigator.vendor.includes('Apple') || navigator.userAgent.includes('Firefox');
this.needsYTApiForAutoplay = navigator.vendor.includes('Apple') || ['Firefox', 'Android'].some(userAgent => navigator.userAgent.includes(userAgent));
}

// // TODO: Support the the user changing the [videoid] attribute
Expand Down Expand Up @@ -109,7 +109,7 @@ class LiteYTEmbed extends HTMLElement {
var el = document.createElement('script');
el.src = 'https://www.youtube.com/iframe_api';
el.async = true;
el.onload = _ => {
el.onload = () => {
YT.ready(res);
};
el.onerror = rej;
Expand All @@ -131,15 +131,16 @@ class LiteYTEmbed extends HTMLElement {
videoId: this.videoId,
playerVars: paramsObj,
events: {
'onReady': event => {
onReady: event => {
event.target.playVideo();
}
}
},
},
});
}

async addIframe(){
async addIframe(e){
if (this.classList.contains('lyt-activated')) return;
e.preventDefault();
this.classList.add('lyt-activated');

const params = new URLSearchParams(this.getAttribute('params') || []);
Expand Down
6 changes: 3 additions & 3 deletions variants/pe.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
<link rel="stylesheet" href="../src/lite-yt-embed.css" />
</head>
<body>
<h1>progressively enhanced</h1>
<h1>Progressively Enhanced</h1>
<p>After 2 seconds, the JS is executed and custom element defined.</p>
<p id="done">...</p>

<lite-youtube videoid="ogfYd705cRs" style="background-image: url('https://i.ytimg.com/vi/ogfYd705cRs/hqdefault.jpg');">
<button type="button" class="lty-playbtn">
<a href="https://youtube.com/watch?v=ogfYd705cRs" class="lty-playbtn" title="Play Video">
<span class="lyt-visually-hidden">Play Video: Keynote (Google I/O '18)</span>
</button>
</a>
</lite-youtube>

<script>
Expand Down
2 changes: 1 addition & 1 deletion now.json → vercel.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
"github": {
"silent": true
}
}
}