Redirect YT video to embedded video

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

目前為 2021-11-21 提交的版本,檢視 最新版本

// ==UserScript==
// @name         Redirect YT video to embedded video
// @namespace    YTRedir
// @version      0.8
// @description  Redirects any opened YT video to the YT embedded video to make the video take the whole screen.
// @author       hacker09
// @include      https://m.youtube.com/*
// @include      https://www.youtube.com/*
// @icon         https://www.youtube.com/s/desktop/03f86491/img/favicon.ico
// @run-at       document-start
// @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 NoFeatureTitle = location.href.replace('&feature=emb_title', ''); //Remove the feature title URL parameters
      const NoFeatureLogo = NoFeatureTitle.replace('&feature=emb_logo', ''); //Remove the feature logo URL parameters
      const YTID = 'https://www.youtube.com/embed/' + location.search.replace('?v=', '') + '?autoplay=1'; //Store embedded URL
      location = NoFeatureLogo.replace(location.href, YTID); //Redirect the YT Link
    } //Finishes the if condition
  } //Finishes the Run 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
    Run(); //Calls the Run function
  } //Finishes the if condition

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

  window.onload = function() { //When the page loads
    if (location.href.match('embed') !== null) //As long as the URL is an embedded video
    { //Starts the if condition
      document.querySelector("a.ytp-title-link").outerHTML = document.querySelector("a.ytp-title-link").outerHTML; //Remove the event listeners of the element
      document.querySelector("a.ytp-title-link").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("a.ytp-title-link").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 if condition
  }; //Finishes the onload event listener
})();