Allow full screen for YouTube embedded videos

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

  1. // ==UserScript==
  2. // @name Allow full screen for YouTube embedded videos
  3. // @description Always allow full screen for YouTube embedded videos that don't allow it by default
  4. // @version 1
  5. // @match https://*/*
  6. // @exclude *://youtube.com/embed/*
  7. // @exclude *://*.youtube.com/embed/*
  8. // @namespace https://greasyfork.org/users/4654
  9. // ==/UserScript==
  10.  
  11. /*jshint esversion:6 */
  12.  
  13. ;(function AddFullScreenToEmbeddedVideos() {
  14. "use strict";
  15. let utils = {
  16. $$: (selector, _parent = document.body) =>
  17. _parent.matches(selector) ? [_parent] : [..._parent.querySelectorAll(selector)], // return as a JS Array instead of a NodeList
  18.  
  19. concurrent: (job) =>
  20. (window.requestIdleCallback || window.requestAnimationFrame || window.setTimeout)(job),
  21.  
  22. isElement: (any) => typeof any === "object" && any.nodeType === Node.ELEMENT_NODE
  23. };
  24.  
  25. let YT_IFRAME_SELECTORS = [
  26. "youtube.com", "youtube-nocookie.com"
  27. ]
  28. .map((url) => `iframe[src*="${url}/embed/"]:not(allowfullscreen)`)
  29. .join(",");
  30.  
  31. let selectVideoIFrames = (parent) => utils.$$(YT_IFRAME_SELECTORS, parent);
  32. let addFullScreen = (videoFrame) => {
  33. console.debug('adding fullscreen to iframe', videoFrame);
  34. videoFrame.allowFullscreen = true;
  35. videoFrame.src = videoFrame.src; // to force a reload of the iframe
  36. };
  37.  
  38.  
  39. let onDomMutation = (mutationList) =>
  40. mutationList.forEach((mutation) =>
  41. [...mutation.addedNodes].filter(utils.isElement)
  42. .flatMap(selectVideoIFrames)
  43. .forEach((iframe) => utils.concurrent(addFullScreen(iframe)))
  44. );
  45.  
  46. let start = () => {
  47. selectVideoIFrames().forEach(addFullScreen);
  48.  
  49. var observer = new MutationObserver(onDomMutation);
  50. observer.observe(document.body, {subtree: true, childList: true});
  51. }
  52.  
  53. window.addEventListener("load", start);
  54. console.debug("Loaded script:", GM.info.script.name);
  55.  
  56. })()