Embedded YouTube fix

this monitors and auto-refreshes crashed embedded YouTube players in the background for a seamless viewing experience

이 스크립트를 설치하려면 Tampermonkey, Greasemonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey와 같은 확장 프로그램을 설치해야 합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Userscripts와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 유저 스크립트 관리자 확장 프로그램이 필요합니다.

(이미 유저 스크립트 관리자가 설치되어 있습니다. 설치를 진행합니다!)

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

(이미 유저 스타일 관리자가 설치되어 있습니다. 설치를 진행합니다!)

// ==UserScript==
// @name         Embedded YouTube fix
// @description  this monitors and auto-refreshes crashed embedded YouTube players in the background for a seamless viewing experience
// @namespace    https://htsign.hateblo.jp
// @version      0.5.5
// @author       htsign
// @match        *://*/*
// @match        https://www.youtube.com/embed/*
// @grant        none
// @license      MIT
// ==/UserScript==

(() => {
  'use strict';

  const ID = 'embedded-youtube-fix';
  /** @type {RegExp[]} */
  const BROKEN_PAGES = [];

  const iframes = document.getElementsByTagName('iframe');

  // parent
  if (window.self === window.top) {
    /**
     * tracks visible iframes and the timestamp of the last message sent
     *
     * @type {WeakMap<HTMLIFrameElement, number>}
     */
    const visibles = new WeakMap();

    /**
     * @param {HTMLIFrameElement} target
     */
    const postId = target => target.contentWindow?.postMessage(ID, 'https://www.youtube.com');

    const observer = new IntersectionObserver(entries => {
      for (const { target, isIntersecting } of entries) {
        if (isIntersecting) {
          const current = performance.now();

          if (!visibles.has(target) || current - visibles.get(target) > 1000) {
            postId(target);
          }
          visibles.set(target, current);
        }
        else {
          visibles.delete(target);
        }
      }
    });

    window.addEventListener('message', ({ data, source, origin }) => {
      if (origin !== 'https://www.youtube.com') return;
      if (data !== ID) return;

      const iframe = Array.prototype.find.call(iframes, el => el.contentWindow === source);
      if (iframe == null) return;

      if (!iframe.hasAttribute('referrerpolicy')) {
        iframe.setAttribute('referrerpolicy', 'strict-origin-when-cross-origin');
        return;
      }

      const url = new URL(iframe.src);

      if (
        ['playlist', 'videoseries'].some(path => url.pathname.endsWith(`/${path}`))
        && url.searchParams.has('list')
        && url.searchParams.has('feature')
      ) {
        url.searchParams.delete('feature');
      }

      if (url.searchParams.get('feature') === 'oembed') {
        url.searchParams.delete('feature');
      }
      url.searchParams.set(ID, url.searchParams.get(ID) ^ 1);

      if (BROKEN_PAGES.some(pageRegex => pageRegex.test(url.href))) {
        // fallback for cases where copying elements breaks the original site's scripts
        iframe.src = url.toString();
      }
      else {
        // avoid multiple back button presses
        const newIframe = iframe.cloneNode();
        newIframe.src = url.toString();

        iframe.replaceWith(newIframe);
        observer.observe(newIframe);
      }
    });

    const loop = () => {
      for (const iframe of iframes) {
        observer.observe(iframe);

        if (visibles.has(iframe)) {
          postId(iframe);
        }
      }

      setTimeout(loop, 500);
    };
    requestIdleCallback(loop);
  }

  // inside of iframe
  else if (location.href.startsWith('https://www.youtube.com/embed/')) {
    window.addEventListener('message', ({ data, source, origin }) => {
      if (data !== ID) return;

      const error = document.querySelector('.ytp-error');
      const subreason = error?.querySelector('.ytp-error-content-wrap-subreason');

      if (subreason && getComputedStyle(subreason).display !== 'none') {
        source.postMessage(ID, origin);
      }
    });
  }
})();