YouTube Link to Embedded Video

Opens YouTube links in an iframe when the link is clicked

Εγκατάσταση αυτού του κώδικαΒοήθεια
Κώδικας προτεινόμενος από τον δημιιουργό

Μπορεί, επίσης, να σας αρέσει ο κώδικας Youtube - Subtitle.

Εγκατάσταση αυτού του κώδικα

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

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

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==UserScript==
// @name         YouTube Link to Embedded Video
// @namespace    https://greatest.deepsurf.us/en/users/670188-hacker09?sort=daily_installs
// @version      3
// @description  Opens YouTube links in an iframe when the link is clicked
// @author       hacker09
// @match        *://*/*
// @exclude      https://www.youtube.com/*
// @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="position: absolute; bottom: -11px; right: -7px; width: 20px; height: 28px; background-color: red; z-index: 10001; 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