Giter VIP home page Giter VIP logo

lite-youtube-embed's Introduction

Lite YouTube Embed NPM lite-youtube-embed package

Renders faster than a sneeze.

Provide videos with a supercharged focus on visual performance. This custom element renders just like the real thing but approximately 224ร— faster.

Demo: https://paulirish.github.io/lite-youtube-embed/

Comparison

Normal <iframe> YouTube embed lite-youtube
Screen Shot 2019-11-03 at 5 23 50 PM Screen Shot 2019-11-03 at 5 21 05 PM Screen Shot 2019-11-03 at 5 19 35 PM Screen Shot 2019-11-03 at 5 23 27 PM Screen Shot 2019-11-03 at 5 20 55 PM Screen Shot 2019-11-03 at 5 20 16 PM

Basic usage

Use the lite-youtube-embed npm package or download from this repo and use src/.

To use the custom element you will need to:

  1. Include the stylesheet within your application
  2. Include the script as well
  3. Use the lite-youtube tag via HTML or JS.
  4. Be happy that you're providing a better user experience to your visitors
<!-- Include the CSS & JS.. (This could be direct from the package or bundled) -->
<link rel="stylesheet" href="node_modules/lite-youtube-embed/src/lite-yt-embed.css" />

<script src="node_modules/lite-youtube-embed/src/lite-yt-embed.js"></script>

<!-- Use the element. You may use it before the lite-yt-embed JS is executed. -->
<lite-youtube videoid="ogfYd705cRs" playlabel="Play: Keynote (Google I/O '18)"></lite-youtube>

Privacy note: lite-youtube uses youtube-nocookie.com instead of youtube.com in order to be more privacy friendly for end users.

Pro-usage: load w/ JS deferred (aka progressive enhancement)

Use this as your HTML, load the script asynchronously, and let the JS progressively enhance it.

<lite-youtube videoid="ogfYd705cRs" style="background-image: url('https://i.ytimg.com/vi/ogfYd705cRs/hqdefault.jpg');">
  <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>
  </a>
</lite-youtube>

Demo: progressive enhancement

Custom poster image

If you want to provide a custom poster image, just set it as the background-image, and lite-yt will not overwrite it:

<lite-youtube videoid="ogfYd705cRs" style="background-image: url('https://i.ytimg.com/vi/ogfYd705cRs/hqdefault.jpg');"></lite-youtube>

Use poster-image-availability.html to determine what poster images are available for you.

Access the YouTube Iframe Player API

Use the js-api param: <lite-youtube videoid="ogfYd705cRs" js-api>.. Then you can use IFrame Player API:

const player = await document.querySelector('lite-youtube').getYTPlayer();
player.seekTo(15); // jump to 15 seconds

Demo: Iframe Player API usage

Add a video title

If you want to display a title prior to loading the full embed, set the title attribute:

<lite-youtube videoid="ogfYd705cRs" title="Keynote (Google I/O '18)"></lite-youtube>

Demo: visible title

Custom Player Parameters

YouTube supports a variety of player parameters to control the iframe's behavior and appearance. These may be applied by using the params attribute.

<!-- Example to show a video player without controls, starting at 10s in, ending at 20s,
     with modest branding *and* enabling the JS API -->
<lite-youtube videoid="ogfYd705cRs" params="controls=0&start=10&end=30&modestbranding=2&rel=0&enablejsapi=1"></lite-youtube>

Note that lite-youtube uses autoplay=1 by default.

Demo: Custom player parameters

Other fast YouTube embeds

lite-youtube-embed's People

Contributors

aarongustafson avatar codepuncher avatar coliff avatar davidsneighbour avatar delucis avatar garbee avatar ibrahimcesar avatar inputsh avatar jakeparis avatar karlhorky avatar leerob avatar madlittlemods avatar michaelsolati avatar patrick91 avatar paulirish avatar pavelloz avatar quis avatar shoonia avatar spacedawwwg avatar zachleat avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

lite-youtube-embed's Issues

npmjs version not most recent

Installing this module via npm install as suggested in the README downloads a release without the .lyt-visually-hidden class. It's trivial to add the class again to the CSS but kind of misses the point of using npm here :)

Default placeholder image

I tried manually setting a high res placeholder using the following markup. The maxresdefault.jpg image displays initially, but as soon as the custom element script kicks in, the maxresdefault.jpg version gets replaced with the hqdefault.jpg.

That replacement seems to happen here: https://github.com/paulirish/lite-youtube-embed/blob/master/src/lite-yt-embed.js#L33-L37

<lite-youtube videoid="OBmlCZTF4Xs" style="background-image:url('https://i.ytimg.com/vi/OBmlCZTF4Xs/maxresdefault.jpg')">
  <div class="lty-playbtn"></div>
</lite-youtube>

In my layout, the <lite-youtube> element takes up the entire width of the page, so the hqdefault image is really blocky and blurry. Especially on larger screens like tablets and laptops.

The comment on line 21-36 mentions something about doing a fallback from sqdefault -> hqdefault. How would you feel about an additional parameter to set the wanted placeholder size and only fall back to hqdefault in case the wanted image size isn't available.

Maybe something like this, where you provide one of the following sizes:

<lite-youtube videoid="..." placeholder="sd|mq|hq|maxres">

...or a full URL. That would probably be easier for users to understand, without having to dig through supported youtube image sizes. It would also allow custom placeholder images:

<lite-youtube videoid="..." placeholder-url="https://i.ytimg.com/vi/OBmlCZTF4Xs/maxresdefault.jpg">

The video I'm testing with is https://www.youtube.com/watch?v=OBmlCZTF4Xs

<lite-vimeo>

My apologies for jumping on this so quickly but I thought it was a very cool project (added the Apache-2.0 license mentioned in #31) I started a fork for the Vimeo player which has some small improvements I think around configuration and including CSS in the JS bundle unless you decide to go a different route. would be cool to have no need for build tools but not sure if that's doable.

Feel free to close this issue, just wanted to ping here. Cheers!

Link: https://github.com/luwes/lite-vimeo-embed

rel:0 not being applied to iframe url param

I have an oEmbed field with a handle of: videoUrl

I pasted the youtube url for a video into the field: https://www.youtube.com/watch?v=BAtlRhAteY4

I then output the iframe using this snippet of code in my twig file:

{{ entry.videoUrl.render({ params: { rel: 0 } }) }}

I expect to see &rel=0 in the iframe markup, this is what I see when I inspect the page:
<iframe width="480" height="270" src="https://www.youtube.com/embed/BAtlRhAteY4?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe>

There is no caching enabled on the site.

Browsers won't allow autoplay

iOS devices and perhaps Safari on Macs won't begin playing the video after clicking on the thumbnail. Rather, the iframe replaces the img tag and the user must tap or click the play button a second time. This is not an ideal user experience. My assumption is that the browser is preventing injected iframe elements from autoplaying with sound turned on. Not sure if there is any way around this or if we should simply use "loading=lazy" on our youtube embeds instead of this program.

Add a license

I noticed this repository doesn't have any license attached. Could you add one, please? See Choose a License for more information ๐Ÿ™‚

Link to React version at bottom of readme?

Hey @paulirish, thanks for this, looks really cool!

Just testing the waters - not looking to start a flame war or anything:

For React users, would you be open to linking to the React version of this at the bottom of the Readme?

use isolated CE registration?

In #3, @Garbee separated out the customElements.define() call so the developer had control over it.

In a way this is kinda annoying... for example the dumb onload handler i had to do here:

elem.onload = _ => {
customElements.define('lite-youtube', LiteYTEmbed);

I'm considering putting that back in as part of the script, basically taking registration away from the developer. This also means the custom element class wouldn't necessarily need to be a global.

Figure out how to publish a performant custom element

I need help here, this isn't my area of expertise. ;)

I don't love the idea that JS has to be run in order for the styles to be added to the page.

This thing should still look great if the JS is in a <script async> and delivered really late.

What is the best practice for packaging a web component such that its straightforward for someone to npm install it but with styles built separately from the JS?

Using lite-youtube with Google Tag Manager

Hi, I'm having issues with the JS API with GTM.

I had the following HTML which worked fine with my GTM installation and its current triggers:

<iframe src="https://www.youtube.com/embed/<?= $video_id ?>?enablejsapi=1"> </iframe>

Then I implemented lite-youtube and I have the following HTML on my website:

<lite-youtube videoid="<?= $video_id ?>" style="background-image: url('https://i.ytimg.com/vi/<?= $video_id ?>/hqdefault.jpg');" params="enablejsapi=1">
	<button type="button" class="lty-playbtn" title=""></button>
</lite-youtube>

Unless I load "https://www.youtube.com/iframe_api" on the page itself, my GTM triggers don't trigger. I can load it below the fold, so it's not that big a deal, but in that case, should I even add "enablejsapi=1" to the parameters, or is there a way to make it work natively with GTM?

Implement ideal accessibility

forked from #37

The question is "what is the ideal accessibility experience for a (unplayed) video embed?"

How should it be made accessible to AT like screenreaders?


First, the play button should definitely be a semantic button (#9), but I suppose there are other opportunities, like a general aria-label on the parent container, etc.

My quick googling points to these resources:

But i imagine there's more.

How to add playlists

I can't work out how to add playlists if possible. :)

If not please can this be added then all the embeds across my site can be replaced.

Great project thanks

Access to image blocked by CORS policy

In Chrome, I get CORS errors in the console using this plugin:
Access to image at 'https://i.ytimg.com/vi/_2aH3Z7YRZ4/hqdefault.jpg' from origin 'https://www.megunolink.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Maybe YouTube is indicating they don't want their images to be used this way? The thumbnail is present when I scroll the video into view, so I'm a bit unclear whether this is a real problem or not.

As a solution, I added a poster attribute to a hosted image by replacing
this.posterUrl = `https://i.ytimg.com/vi/${this.videoId}/hqdefault.jpg`;
with
this.posterUrl = this.getAttribute('poster');
in lite-yt-embed.js

Then the tag becomes:
<lite-youtube videoid="_2aH3Z7YRZ4" poster="https://www.megunolink.com/wp-content/uploads/2020/06/introvideothumbnail.jpg" ></lite-youtube>

Allow reduced privacy embeds

Spawning from discussion in #80

Currently the lite embed only works in the enhanced privacy mode. Since the URL is hard-coded to use the "no cookie" serving. This can cause potential problems with some types of content if a publisher allows it to be embedded while being protected by a paywall for example.

A new attribute should be introduced which will remove the no cookie from the iframe URL. My personal preference is calling it reduced-privacy which is directly opposite of the default of using what YouTube calls "Enhanced Privacy".

The attribute, regardless of final name, should be a boolean attribute. Meaning it's presence alone triggers the effect and any values assigned are ignored.

Some improvements ideas.

I've working on a similar implementation. Here is a couple of ideas.

  • Used the a tag <a href="https://youtu.be/3vBwRfQbXkg" class="ytlight">HOW TO FUNK IN TWO MINUTES</a>. If the user has JavaScript disabled, it can go to the video anyway.
  • Lazy load the thumbnail.
  • The quality of the thumbnail to load is decided with NetworkInformation.effectiveType.

POC

Test page lite-youtube-embed
Test page ytlight

make it responsive

Looking at the element it creates an embed with a hard-coded size. By allowing custom attributes to be used to give a size (and handle their changing events) we can allow developers to get the size they need seamlessly.

While carrying this out, we'll follow the best practices for custom elements where applicable.

Fallow spec and use HTML standard attributes data-*

Spec: JavaScript HTMLElement.dataset, HTML data-*.

Replace videoid with data-videoid and #29 startTime with data-starttime (notice: attributes should be in lowercase). If you like can use instead data-video-id (accessible in HTMLElement.dataset.videoId) and data-start-time. Personally I would prefer shorter data-id and data-time. Element <lite-youtube> already makes clear that it is related to YouTube making understandable to what id refers.

Safari support?

Hi Guys,

I was wondering if this has safari support to? I've tried to implement it on a website, and on safari I'm getting a strange repeated background image and i don't see any play button, I also can't click the video to let it play.

Thanks in advance!

grts

Please add a link to a test example to readme.md

I found a trouble when testing my version on mobile Chrome. I thought I might have made some stupid mistake and wanted to see how the original version worked. I could not find any test example here.

There might be some example, but I have no idea how to find it. Please link to a test example in readme.md.

Lazy load cover image

This was briefly mentioned in #48 but since Firefox now supports loading="lazy", does it make sense to investigate moving away from background: url(...); and into inserting an <img src="..." loading="lazy" alt="" aria-hidden="true"/> tag as the cover image instead?

Lighthouse keeps bugging me to defer my lite-youtube cover images, so I wanted to see if others were having the same before I make something janky or something good, haha

I'd be happy to help!

Extra params (like start and end times)

It's not an issue, just a feature request. Could you consider adding support for extra URL params, like
<iframe ... src="https://www.youtube.com/embed/${this.videoId}?autoplay=1&start=10&end=30"</iframe>

I think it can look something like:
<lite-youtube videoid="..." params="start=10&end=30"></lite-youtube>

Achieve "Gold Standard" baseline

Ref 1: https://github.com/webcomponents/gold-standard/wiki
Ref 2: https://developers.google.com/web/fundamentals/web-components/best-practices

This is more of a meta-issue to discuss what overall goals we want to go after. Then the individual bits we wish to go after can be broken down into their own sub-issues for PRs to go against.

Does anyone in the community have any alternative recommended guidelines? Particularly interested in anything that conflicts with the guiding points laid out in gold-standard.

For any set of goals, individual issues should be opened describing it and a proposed solution. From there we can try to have some discussion before anyone digs heavy into the coding side.

The goal here, is to provide a baseline element that works along the lines of what the community expects from web components. While preserving the heavy focus on overall performance. With each individual goal to build up to this, the performance impacts need to be kept in mind.

Custom cover image?

Thanks for this!
Is there a way to insert your own cover image for a video?

lite-youtube-embed Implementation in wordpress

Hello.
On my site in the records a lot of videos are added from youtube !! How to optimize already uploaded videos to a site using your plugin?
for this you need to write an additional script that would find the id in the pages from the youtube player code and add them to < lite-youtube videoid = " Id_video" > </ lite-youtube > ?
Please tell me how to optimize the videos already added to the site .. I have a lot of them on the site !!

Responsive sizing

I currently use these to responsively size the video:

.video-container {
  position: relative;
  width: 100%;
  height: 0;
  padding-bottom: 56.25%;
}

.video-embed {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  border: 0;
}

which is the de-facto best practice to do so, correct me if I'm wrong please.

Any way this could be integrated into lite-youtube-embed itself?

NPM Packaging

Opening a discussion on NPM packaging requirements. Sadly there are a few "best practices" articles around but none quite cover the nuance or context of most decisions. Let's just air what we think a bit and then roll forward.

In the context of this component, I think all we'd really need to distribute via NPM itself is the script and stylesheet. All the HTML and possible markdown files (hopefully just one or two when we're done) are not that useful when integrating and deploying. Those are very much dev time oriented. Given my current experience knowing how some organizations need to scrutinize and approve everything that operates on their system's, I'd say we should use npmignore to only package the bits that need to be in the distribution. This way anyone that needs to get what we provide audited should have an easier time.

Beyond just minimizing what is in the package to the absolute necessities I don't have any real concerns with how we package this (as long as we attempt to obey SemVer.) Does anyone else have other concerns with packaging? Or any hard objections to stripping the package distributed to the bare minimum?

What's about events?

Need some events when video start/stop, to control playing another video blocks

A more accurate YouTube play button

The existing button is a implemented with the CSS box model. The actual YouTube has subtle curvature around the box, which is very hard to achieve with pure CSS.

Consider replace the CSS with an SVG, either as background image or as HTML. I'm happy to assist by creating a PR if you are ok with it. @paulirish

Consider using <img> instead of background for poster

I think the video poster is part of the content and therefore should exist as an actual <img> element in the page instead of background. Of course that would also require an alt description to be provided. What do you think?

Add embed url or video id as one of the variables

In addition to these already available variables

entry.field.media.title
entry.field.media.description
entry.field.media.url
entry.field.media.type
entry.field.media.tags
entry.field.media.images
entry.field.media.image
entry.field.media.imageWidth
entry.field.media.imageHeight
entry.field.media.code
entry.field.media.width
entry.field.media.height
entry.field.media.aspectRatio
entry.field.media.authorName
entry.field.media.authorUrl
entry.field.media.providerName
entry.field.media.providerUrl
entry.field.media.providerIcons
entry.field.media.providerIcon
entry.field.media.publishedDate
entry.field.media.license
entry.field.media.linkedData
entry.field.media.feeds

Could you add:

entry.field.media.id
entry.field.media.embed_url

The reason I think it would be helpful is if someone wanted to build their own iframe element, they can build their own iframe tag and apply params like &rel=0 to it

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.