Allow full screen for YouTube embedded videos

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

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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);

})()