Redirect YT video to embedded video

Redirects any opened YT video to the YT embedded video to make the video take the whole screen.

Verzia zo dňa 21.11.2021. Pozri najnovšiu verziu.

// ==UserScript==
// @name         Redirect YT video to embedded video
// @namespace    YTRedir
// @version      0.7
// @description  Redirects any opened YT video to the YT embedded video to make the video take the whole screen.
// @author       hacker09
// @include      /^(https?:\/\/)(www\.)?(m\.)?(youtube)(\.com)(\/.*)?/
// @icon         https://www.youtube.com/s/desktop/03f86491/img/favicon.ico
// @run-at       document-end
// @grant        none
// ==/UserScript==

(function() {
  'use strict';

  function Run() { //Creates a new function
    if ((location.pathname !== '/') && (location.href.match('/watch') !== null) && (location.href.match(/&list=|&t=1s|embed/) === null)) //As long as it's not the YT index page, the URL is a video, it's not a playlist/embedded video, and doesn't have '&t=1s' in the end of the URL
    { //Starts the if condition
      const NoFeature = location.href.replace('&feature=emb_title', ''); //Remove the feature URL parameters
      const YTID = 'https://www.youtube.com/embed/' + location.search.replace('?v=', '') + '?autoplay=1'; //Store embedded URL
      location = NoFeature.replace(location.href, YTID); //Redirect the YT Link
    } //Finishes the if condition
  } //Finishes the Run function

  window.ontransitionrun = function() { //When the video is changed
    Run(); //Calls the Run function
  }; //Finishes the transitionend event listener

  if (location.href.match('embed') !== null) //As long as the URL is an embedded video
  { //Starts the if condition
    window.onload = function() { //When the page loads
      document.querySelector(".ytp-title-text > a").outerHTML = document.querySelector(".ytp-title-text > a").outerHTML; //Remove the event listeners of the element
      document.querySelector(".ytp-title-text > a").href += '&t=1s'; //Add url parameter to start video on the first second of the video, so that the script won't redirect the URL
      document.querySelector(".ytp-title-text > a").target = '_self'; //Open the video in the same tab
      document.querySelector(".ytp-button.yt-uix-sessionlink").outerHTML = document.querySelector(".ytp-button.yt-uix-sessionlink").outerHTML; //Remove the event listeners of the element
      document.querySelector(".ytp-button.yt-uix-sessionlink").href += '&t=1s'; //Add url parameter to start video on the first second of the video, so that the script won't redirect the URL
      document.querySelector(".ytp-button.yt-uix-sessionlink").target = '_self'; //Open the video in the same tab
    }; //Finishes the onload event listener
  } //Finishes the if condition
})();