YouTube Link to Embedded Video

Opens YouTube links in an iframe when the link is clicked

安装此脚本?
作者推荐脚本

您可能也喜欢Youtube - Subtitle

安装此脚本
  1. // ==UserScript==
  2. // @name YouTube Link to Embedded Video
  3. // @namespace https://greasyfork.org/en/users/670188-hacker09?sort=daily_installs
  4. // @version 3
  5. // @description Opens YouTube links in an iframe when the link is clicked
  6. // @author hacker09
  7. // @match *://*/*
  8. // @exclude https://www.youtube.com/*
  9. // @run-at document-end
  10. // @icon https://www.youtube.com/s/desktop/03f86491/img/favicon.ico
  11. // @grant none
  12. // ==/UserScript==
  13.  
  14. new MutationObserver(function() { //Starts a new MutationObserver function
  15. 'use strict';
  16. document.querySelectorAll('[href*="youtube.com/watch?v="], [href*="youtu.be/watch?v="]').forEach(link => { //ForEach YT link
  17. link.addEventListener('click', function(event) { //When the YT link is clicked
  18. if (event.target.closest('a') && event.button !== 2 && !event.ctrlKey && !event.metaKey) { //If the CTRL key isn't being holded
  19. event.preventDefault(); //Prevent it from opening
  20. window.originalLink = this.outerHTML; //Save link HTML
  21. 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
  22. document.querySelector('.closeButton').addEventListener('click', function() { //When the close button is clicked
  23. document.querySelector('.originalLinkPosition').outerHTML = originalLink; //Restore original link HTML
  24. document.querySelector('.YTScript').remove(); //Remove embedded YT video
  25. }); //Finishes the click event listener
  26. this.remove(); //Remove current link HTML
  27. } //Finishes the if condition
  28. }); //Finishes the click event listener
  29. }); //Finishes the ForEach loop
  30. }).observe(document, { childList: true, subtree: true }); //Run script on page change