YouTube /embed/ forwarder

Forwards YouTube links to the youtube.com/embed/* page, so there's just the video in your window and nothing else.

  1. // ==UserScript==
  2. // @name YouTube /embed/ forwarder
  3. // @description Forwards YouTube links to the youtube.com/embed/* page, so there's just the video in your window and nothing else.
  4. // @namespace https://greasyfork.org/en/users/1148791-vuccala
  5. // @author Vuccala
  6. // @icon https://archive.org/download/yt_icon/yt.png
  7. // @match *://*.youtube.com/*
  8. // @match *://*.youtu.be/*
  9. // @run-at document-start
  10. // @version 0.4
  11. // @grant none
  12. // @license MIT
  13.  
  14. // ==/UserScript==
  15.  
  16. (function() {
  17. var embedBaseUrl = 'https://youtube.com/embed/';
  18.  
  19. function getId(u) {
  20. var idMatch = /(?:[?&]v=|\/(?:embed\/|v\/|shorts\/))([^&?/]+)/.exec(u);
  21. return idMatch ? idMatch[1] : '';
  22. }
  23.  
  24. function updateLinks(links) {
  25. links.forEach(function(link) {
  26. var linkHref = link.getAttribute('href');
  27. var videoIdMatch = /(?:[?&]v=|\/(?:embed\/|v\/|shorts\/))([^&?/]+)/.exec(linkHref);
  28.  
  29. if (videoIdMatch) {
  30. var videoId = videoIdMatch[1];
  31. var embedUrl = embedBaseUrl + videoId;
  32. link.href = embedUrl;
  33. }
  34. });
  35. }
  36.  
  37. function observeDOM() {
  38. var targetNode = document.body;
  39.  
  40. var observer = new MutationObserver(function(mutationsList) {
  41. for (var mutation of mutationsList) {
  42. if (mutation.type === 'childList') {
  43. var newLinks = mutation.addedNodes[0].querySelectorAll('a[href*="/watch?v="], a[href*="/shorts/"], a[href*="/watch?app=desktop&v="]');
  44. updateLinks(newLinks);
  45. }
  46. }
  47. });
  48.  
  49. var observerConfig = { childList: true, subtree: true };
  50. observer.observe(targetNode, observerConfig);
  51. }
  52.  
  53. var url = window.location.href;
  54.  
  55. if (url.includes('/watch?v=') || url.includes('/shorts/') || url.includes('/watch?app=desktop&v=')) {
  56. var videoId = getId(url);
  57. var newURL = embedBaseUrl + videoId;
  58.  
  59. // Change the location to the new URL
  60. if (newURL !== url) {
  61. window.location.href = newURL;
  62. }
  63. } else {
  64. observeDOM();
  65. }
  66. })();