0.2.1 • Published 3 years ago

responsive-youtube.js v0.2.1

Weekly downloads
2
License
MIT
Repository
github
Last release
3 years ago

Responsive-youtube.js

Simple script for use Youtube Iframe API in responsive web sites

Requirements

  • IE10+
  • Safari6+
  • Google Chrome
  • Firefox
  • MutationObserver

Note¹: Use if (ResponsiveYoutube.supported)

Note²: Youtube API requires IE10+

HTML usage

<!-- embed video in page -->
<div data-ry-video="TALH8SsQJp4"></div>

<!-- embed video in page without "responsive" -->
<div data-ry-video="5ABos9UTfJU" data-ry-ignore="true"></div>

<script src="responsive-youtube.js"></script>
<script>
ResponsiveYoutube.start();
</script>

Import:

const ResponsiveYoutube = require('responsive-youtube.js');

...

ESM/ES6 import:

import ResponsiveYoutube from 'responsive-youtube.js';

...

RequireJS:

requirejs(['folder/foo/bar/responsive-youtube'], function (ResponsiveYoutube) {
    ...
});

Properties and functions

Namedescription
ResponsiveYoutube.supportedCreate players
ResponsiveYoutube.start([setup])Create players
ResponsiveYoutube.on(type, function)Add global event
ResponsiveYoutube.off(type, function)Remove global event
ResponsiveYoutube.destroy()Destroy all players (use with Angular pagination, Ajax pagination, and Pjax)

Events

Nameequivalent tocallback format
doneonYouTubeIframeAPIReadyfunction () {...}
create-function (player) {...}
readyonReadyfunction (event, player) {...}
stateonStateChangefunction (event, player) {...}
qualityonPlaybackQualityChangefunction (event, player) {...}
rateonPlaybackRateChangefunction (event, player) {...}
erroronErrorfunction (event, player) {...}
apionApiChangefunction (event, player) {...}

More details in: https://developers.google.com/youtube/iframe_api_reference#Events

Example usage:

ResponsiveYoutube.start();

ResponsiveYoutube.on("done", function () {
    //API done...
});

ResponsiveYoutube.on("create", function (player) {
    //Something...
});

ResponsiveYoutube.on("state", function (event, player) {
    //Something...
});

Properties

Propertyhtml5description
autoplaydata-ry-autoplayThis parameter specifies whether the initial video will automatically start to play when the player loads. Supported values are 0 or 1. The default value is 0.
cc_load_policydata-ry-cc_load_policySetting the parameter's value to 1 causes closed captions to be shown by default, even if the user has turned captions off. The default behavior is based on user preference.
colordata-ry-colorThis parameter specifies the color that will be used in the player's video progress bar to highlight the amount of the video that the viewer has already seen.
controlsdata-ry-controlsThis parameter indicates whether the video player controls are displayed
disablekbdata-ry-disablekbSetting the parameter's value to 1 causes the player to not respond to keyboard controls.
fsdata-ry-fsSetting this parameter to 0 prevents the fullscreen button from displaying in the player. The default value is 1, which causes the fullscreen button to display.
hldata-ry-hlSets the player's interface language.
iv_load_policydata-ry-iv_load_policySetting the parameter's value to 1 causes video annotations to be shown by default, whereas setting to 3 causes video annotations to not be shown by default. The default value is 1.
listdata-ry-listThe list parameter, in conjunction with the listType parameter, identifies the content that will load in the player. (no longer be supported as of 15 November 2020)
listTypedata-ry-listTypeThe listType parameter, in conjunction with the list parameter, identifies the content that will load in the player. Valid parameter values are playlist, search, and user_uploads (no longer be supported as of 15 November 2020)
loopdata-ry-loopIn the case of a single video player, a setting of 1 causes the player to play the initial video again and again.
modestbrandingdata-ry-modestbrandingThis parameter lets you use a YouTube player that does not show a YouTube logo. Set the parameter value to 1 to prevent the YouTube logo from displaying in the control bar. Note that a small YouTube text label will still display in the upper-right corner of a paused video when the user's mouse pointer hovers over the player.
playlistdata-ry-playlistThis parameter specifies a comma-separated list of video IDs to play. If you specify a value, the first video that plays will be the VIDEO_ID specified in the URL path, and the videos specified in the playlist parameter will play thereafter.
playsinlinedata-ry-playsinlineThis parameter controls whether videos play inline or fullscreen in an HTML5 player on iOS.
reldata-ry-relIf the rel parameter is set to 0, related videos will come from the same channel as the video that was just played (changing on or after September 25, 2018)
showinfodata-ry-showinfoSupported values are 0 and 1.
startdata-ry-startThis parameter causes the player to begin playing the video at the given number of seconds from the start of the video.
widget_referrerdata-ry-widget_referrerThis parameter identifies the URL where the player is embedded.

More parameters and details in: https://developers.google.com/youtube/player_parameters?playerVersion=HTML5#Parameters

Set default properties in .start():

ResponsiveYoutube.start({
    rel: 0 //Remove related videos
});

Usage in html:

<!-- autoplay -->
<div data-ry-video="hFUQQIsPoVA" data-ry-autoplay="1"></div>

<!-- remove related -->
<div data-ry-video="hFUQQIsPoVA" data-ry-rel="1"></div>

<!-- without controls with closed caption -->
<div data-ry-video="hFUQQIsPoVA" data-ry-controls="0" data-ry-cc_load_policy="1"></div>

Youtube iframe embed with JSON-LD or microdata

Example with JSON-LD

<div data-ry-video="<youtube ID>"></div>

<script type="application/ld+json">{
    "@context": "http://schema.org",
    "@type": "VideoObject",
    "name": "Youtube iframe embed with JSON-LD",
    "description": "Youtube iframe embed with JSON-LD example",
    "thumbnailUrl": "https://i.ytimg.com/vi/<youtube ID>/default.jpg",
    "uploadDate": "2020-08-10T08:01:27Z",
    "duration": "PT15M43S",
    "embedUrl": "https://www.youtube.com/embed/<youtube ID>",
    "interactionCount": "464"
}</script>

Example with microdata

<div itemscope itemprop="video" itemtype="http://schema.org/VideoObject">
    <h2><span itemprop="name">Youtube iframe embed with microdata</span></h2>
    <meta itemprop="duration" content="PT15M43S">
    <meta itemprop="uploadDate" content="2020-08-10T08:01:27Z">
    <meta itemprop="thumbnailURL" content="https://i.ytimg.com/vi/<youtube ID>/default.jpg">
    <meta itemprop="interactionCount" content="464">
    <meta itemprop="embedURL" content="https://youtube.googleapis.com/v/<youtube ID>">
    <p itemprop="description">Youtube iframe embed with microdata example</p>
    <div data-ry-video="<youtube ID>"></div>
</div>

TODO

  • destroy players
  • Create cover alternative to video