npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

youtube-iframe-ctrl

v1.0.2

Published

Simple YouTube iFrame controller with no additional dependencies and no YouTube iFrame API script.

Downloads

49

Readme

YouTube iFrame Controller

Simple YouTube iFrame controller with no additional dependencies and no YouTube iFrame API script.

Demo with examples: youtube-iframe-ctrl.mihau.co


Toss a Coin to Your Developer

If you this package helped you and you would like to spare me some change - you can do it via buymeacoffee.com or buycoffee.to.


The official YouTube documentation requires you to add an external script to your page and create an iFrame with JavaScript to control the player inside the iFrame. This can be complicated if you only want to perform simple actions. Most packages available on NPM are just wrappers for the official YouTube script (downloading it under the hood and exposing the same API). This module does not require the official YouTube iFrame API script or any other dependencies. It contains just a few lines of code and provides all the functionalities of the official API.

Table of Contents

  • Installation
  • Usage
  • Methods
  • Properties
  • Events
  • Quick note about autoplay

Installation

To install the YouTubeIFrameCtrl you can use npm:

npm install youtube-iframe-ctrl

Usage

Here's an example of how to use the YouTubeIFrameCtrl:

Add iframe to your page / html.


IMPORTANT NOTE:

You must add enablejsapi=1 query parameter to YouTube url - otherwise iFrame controller will not work.


<iframe
  id="youtube-iframe"
  src="https://www.youtube-nocookie.com/embed/jNQXAC9IVRw?enablejsapi=1"
  title="YouTube video player"
  frameborder="0"
  allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share"
  referrerpolicy="strict-origin-when-cross-origin"
  allowfullscreen
>
</iframe>
import YouTubeIFrameCtrl from 'youtube-iframe-ctrl';

// Assuming you have an iframe element with id 'youtube-iframe'
const iframeElement = document.getElementById('youtube-iframe');
const youTubeIFrameCtrl = new YouTubeIFrameCtrl(iframe);

async function play() {
  // Mute video
  await youTubeIFrameCtrl.mute()
  // Play video
  await youTubeIFrameCtrl.play()
}

play()

Methods

async play(): Promise<void>

Plays the video. Under the hood it uses command() method. IMPORTANT! If you would like autoplay a video it must be muted first! More info at the end of readme.

Example:

youTubeIFrameCtrl.play()

async pause(): Promise<void>

Pauses the video. Under the hood it uses command() method.

Example:

youTubeIFrameCtrl.pause()

async stop(): Promise<void>

Stops the video. Under the hood it uses command() method.

Example:

youTubeIFrameCtrl.stop()

async mute(): Promise<void>

Mutes the video. Under the hood it uses command() method.

Example:

youTubeIFrameCtrl.mute()

async unMute(): Promise<void>

Unmutes the video. Under the hood it uses command() method.

Example:

youTubeIFrameCtrl.unmute()

async command(command: string, args?: any[]): Promise<void>

Sends a command to the YouTube player. command argument can be a name of any command accepted by YouTube iFrame player API, below you'll find a list of all know commands (it's based on my foundings after studing some YouTube player JS source files):

| Command | Arguments | Info | |---------|-----------|------| | play | - | Play video (you can use play method of YouTube iFrame Controller instead) | | pause | - | Pause video (you can use pause method of YouTube iFrame Controller instead) | | stop | - | Stop video (you can use stop method of YouTube iFrame Controller instead) | | mute | - | Mute video (you can use mute method of YouTube iFrame Controller instead) | | unMute | - | Unmute video (you can use unMute method of YouTube iFrame Controller instead) | | seekTo | seconds: number, allowSeekAhead: boolean | Seek to a specified time in seconds | | setVolume | volume: number | Set the volume (0-100) |

Properties

playerState: string

Returns the current player state as a string.

Events

YouTube iFtame Controller adds some custom events to the iFrame element.

ytstatechange

Dispatched when the player state changes. The event detail contains the new player state.

Example

iframeElement.addEventListener('ytstatechange', (event) => {
  console.log('Player state changed to:', event.detail);
});

ytmessage

Dispatched when a message is received from the YouTube player. The event detail contains the message data.

iframeElement.addEventListener('ytmessage', (event) => {
  console.log('Message sent from youtube player:', event.detail);
});

Quick note about autoplay

YouTube does not allow unmuted videos to autoplay without user interaction (likely using UserActivation to check if user interacted with the page, though I haven't confirmed it). If you want to play a YouTube video on page load, it must be muted first.