UNPKG

youtube-iframe-api-module

Version:

A lightweight TypeScript and JavaScript wrapper for the YouTube IFrame API, making it easy to use YouTube videos in npm-based projects and modern browser environments.

34 lines (33 loc) 1.05 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.loadScript = void 0; const src = "https://www.youtube.com/iframe_api"; const isScriptPresent = () => { const isAdded = !!document.querySelector(`script[src="${src}"]`); return isAdded; }; const addScript = () => { var _a; const tag = document.createElement('script'); tag.src = src; const firstScriptTag = document.getElementsByTagName('script')[0]; (_a = firstScriptTag === null || firstScriptTag === void 0 ? void 0 : firstScriptTag.parentNode) === null || _a === void 0 ? void 0 : _a.insertBefore(tag, firstScriptTag); }; const arrCallbacks = []; const defineCallback = () => { window.onYouTubeIframeAPIReady = () => { arrCallbacks.forEach(callback => callback()); }; }; const loadScript = (callback) => { if (!!window.YT) { callback(); return; } arrCallbacks.push(callback); if (!isScriptPresent()) { defineCallback(); addScript(); } }; exports.loadScript = loadScript;