YouTube Link to Embedded Video

Opens YouTube links in an iframe when the link is clicked

目前為 2024-05-17 提交的版本,檢視 最新版本

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

You will need to install an extension such as Tampermonkey to install this script.

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

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

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         YouTube Link to Embedded Video
// @namespace    https://greasyfork.org/en/users/670188-hacker09?sort=daily_installs
// @version      1
// @description  Opens YouTube links in an iframe when the link is clicked
// @author       hacker09
// @match        *://*/*
// @run-at       document-end
// @icon         https://www.youtube.com/s/desktop/03f86491/img/favicon.ico
// @grant        none
// ==/UserScript==

new MutationObserver(function() { //Starts a new MutationObserver function
  'use strict';
  document.querySelectorAll('[href*="youtube.com/watch?v="], [href*="youtu.be/watch?v="]').forEach(link => { //ForEach YT link
    link.addEventListener('click', function(event) { //When the YT link is clicked
      if (event.target.closest('a') && event.button !== 2 && !event.ctrlKey && !event.metaKey) { //If the CTRL key isn't being holded
        event.preventDefault(); //Prevent it from opening
        window.originalLink = this.outerHTML; //Save link HTML
        this.outerHTML = `<div class='originalLinkPosition'><div class="YTScript"><div style="resize: both;overflow: hidden;width: 555px;height: 373px;position: relative;"><div class="closeButton" style="position: absolute;right: 0px;cursor: pointer;z-index: 10000;">❌</div><iframe style="z-index: 9999;position: absolute;width: 100%;height: 100%;" src="https://www.youtube.com/embed/${this.href.split('&')[0].split('v=')[1]}?autoplay=1" allow="picture-in-picture;" allowfullscreen=""></iframe><div style="left: 96%;top: 96%;width: 120px;height: 30%;z-index: 9999;border-right: 80px solid #D71E1E;position: absolute;transform: rotate(-135deg);"></div></div></div>`; //Add buttons and iframe
        document.querySelector('.closeButton').addEventListener('click', function() { //When the close button is clicked
          document.querySelector('.originalLinkPosition').outerHTML = originalLink; //Restore original link HTML
          document.querySelector('.YTScript').remove(); //Remove embedded YT video
        }); //Finishes the click event listener
        this.remove(); //Remove current link HTML
      } //Finishes the if condition
    }); //Finishes the click event listener
  });  //Finishes the ForEach loop
}).observe(document, { childList: true, subtree: true }); //Run script on page change