Advertisement
Guest User

Replace Youtube player with Invidious embeds

a guest
Oct 14th, 2023
5,208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.45 KB | None | 0 0
  1. // ==UserScript==
  2. // @name Invidious Embed
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.0
  5. // @description Replace YouTube video player with Invidious embed
  6. // @author You
  7. // @match https://www.youtube.com/*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. 'use strict';
  13.  
  14. // Define the provider URL for easy user modifications
  15. const provider = 'https://invidious.fdn.fr';
  16.  
  17. function embedInvidious() {
  18. const maxRetries = 10;
  19. let retryCount = 0;
  20.  
  21. const createIframe = () => {
  22. // Remove any existing iframe
  23. const existingIframe = document.getElementById('invidious-embed');
  24. if (existingIframe) {
  25. existingIframe.remove();
  26. }
  27.  
  28. // Extract video ID from the URL
  29. const videoID = window.location.search.split('v=')[1]?.split('&')[0];
  30.  
  31. // Only proceed if a video ID is found
  32. if (!videoID) return;
  33.  
  34. // Create an iframe element
  35. const iframe = document.createElement('iframe');
  36. // Use the provider constant in the iframe src attribute
  37. iframe.src = `${provider}/embed/${videoID}?quality=hd720&autoplay=1`;
  38. iframe.frameBorder = 0;
  39. iframe.style.position = 'absolute';
  40. iframe.style.maxWidth = '100vw';
  41. iframe.style.zIndex = 1000;
  42. iframe.id = 'invidious-embed';
  43.  
  44. // Find the YouTube video player element
  45. const player = document.querySelector('.html5-video-player') || document.getElementById('movie_player');
  46.  
  47. if (player) {
  48. // Obtain the position and size of the original player
  49. const rect = player.getBoundingClientRect();
  50.  
  51. // Check if width and height are available; if not, retry after 1 second
  52. if (rect.width === 0 || rect.height === 0) {
  53. if (retryCount < maxRetries) {
  54. retryCount++;
  55. setTimeout(createIframe, 1000);
  56. }
  57. return;
  58. }
  59.  
  60. iframe.style.top = `${rect.top}px`;
  61. iframe.style.left = `${rect.left}px`;
  62. iframe.width = rect.width;
  63. iframe.height = rect.height;
  64.  
  65. // Hide the YouTube video player
  66. player.style.opacity = '0';
  67.  
  68. // Append the Invidious iframe to the body
  69. document.body.appendChild(iframe);
  70. }
  71. };
  72.  
  73. // Call createIframe once initially
  74. createIframe();
  75. }
  76.  
  77. // Initial embedding
  78. embedInvidious();
  79.  
  80. // Create a MutationObserver instance
  81. const observer = new MutationObserver((mutations, observer) => {
  82. for(const mutation of mutations) {
  83. if (mutation.type === 'attributes') {
  84. embedInvidious();
  85. }
  86. }
  87. });
  88.  
  89. // Options for the observer (which mutations to observe)
  90. const config = { attributes: true, childList: false, subtree: false };
  91.  
  92. // Target for the observer
  93. const targetNode = document.body;
  94.  
  95. // Start observing the target node
  96. observer.observe(targetNode, config);
  97.  
  98. // Pause all videos every 100ms
  99. setInterval(() => {
  100. document.querySelectorAll('video').forEach(video => {
  101. video.pause();
  102. });
  103. }, 50);
  104. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement