Allow full screen for YouTube embedded videos

Always allow full screen for YouTube embedded videos that don't allow it by default

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name     Allow full screen for YouTube embedded videos
// @description     Always allow full screen for YouTube embedded videos that don't allow it by default
// @version  1
// @match    https://*/*
// @exclude  *://youtube.com/embed/*
// @exclude  *://*.youtube.com/embed/*
// @namespace https://greasyfork.org/users/4654
// ==/UserScript==

/*jshint esversion:6 */

;(function AddFullScreenToEmbeddedVideos() {
  "use strict";
  let utils = {
    $$: (selector, _parent = document.body) => 
      _parent.matches(selector) ? [_parent] : [..._parent.querySelectorAll(selector)],  // return as a JS Array instead of a NodeList

    concurrent: (job) =>
      (window.requestIdleCallback || window.requestAnimationFrame || window.setTimeout)(job),

    isElement: (any) => typeof any === "object" && any.nodeType === Node.ELEMENT_NODE
  };

  let YT_IFRAME_SELECTORS = [
      "youtube.com", "youtube-nocookie.com"
    ]
    .map((url) => `iframe[src*="${url}/embed/"]:not(allowfullscreen)`)
    .join(",");

  let selectVideoIFrames = (parent) => utils.$$(YT_IFRAME_SELECTORS, parent);
  
  let addFullScreen = (videoFrame) => {
    console.debug('adding fullscreen to iframe', videoFrame);
    videoFrame.allowFullscreen = true;
    videoFrame.src = videoFrame.src; // to force a reload of the iframe
  };


  let onDomMutation = (mutationList) =>
    mutationList.forEach((mutation) =>
      [...mutation.addedNodes].filter(utils.isElement)
                         .flatMap(selectVideoIFrames)
                         .forEach((iframe) => utils.concurrent(addFullScreen(iframe)))
    );

  let start = () => {
    selectVideoIFrames().forEach(addFullScreen);

    var observer = new MutationObserver(onDomMutation);
    observer.observe(document.body, {subtree: true, childList: true});
  }

  window.addEventListener("load", start);
  console.debug("Loaded script:", GM.info.script.name);

})()