Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name Invidious Embed
- // @namespace http://tampermonkey.net/
- // @version 1.0
- // @description Replace YouTube video player with Invidious embed
- // @author You
- // @match https://www.youtube.com/*
- // @grant none
- // ==/UserScript==
- (function() {
- 'use strict';
- // Define the provider URL for easy user modifications
- const provider = 'https://invidious.fdn.fr';
- function embedInvidious() {
- const maxRetries = 10;
- let retryCount = 0;
- const createIframe = () => {
- // Remove any existing iframe
- const existingIframe = document.getElementById('invidious-embed');
- if (existingIframe) {
- existingIframe.remove();
- }
- // Extract video ID from the URL
- const videoID = window.location.search.split('v=')[1]?.split('&')[0];
- // Only proceed if a video ID is found
- if (!videoID) return;
- // Create an iframe element
- const iframe = document.createElement('iframe');
- // Use the provider constant in the iframe src attribute
- iframe.src = `${provider}/embed/${videoID}?quality=hd720&autoplay=1`;
- iframe.frameBorder = 0;
- iframe.style.position = 'absolute';
- iframe.style.maxWidth = '100vw';
- iframe.style.zIndex = 1000;
- iframe.id = 'invidious-embed';
- // Find the YouTube video player element
- const player = document.querySelector('.html5-video-player') || document.getElementById('movie_player');
- if (player) {
- // Obtain the position and size of the original player
- const rect = player.getBoundingClientRect();
- // Check if width and height are available; if not, retry after 1 second
- if (rect.width === 0 || rect.height === 0) {
- if (retryCount < maxRetries) {
- retryCount++;
- setTimeout(createIframe, 1000);
- }
- return;
- }
- iframe.style.top = `${rect.top}px`;
- iframe.style.left = `${rect.left}px`;
- iframe.width = rect.width;
- iframe.height = rect.height;
- // Hide the YouTube video player
- player.style.opacity = '0';
- // Append the Invidious iframe to the body
- document.body.appendChild(iframe);
- }
- };
- // Call createIframe once initially
- createIframe();
- }
- // Initial embedding
- embedInvidious();
- // Create a MutationObserver instance
- const observer = new MutationObserver((mutations, observer) => {
- for(const mutation of mutations) {
- if (mutation.type === 'attributes') {
- embedInvidious();
- }
- }
- });
- // Options for the observer (which mutations to observe)
- const config = { attributes: true, childList: false, subtree: false };
- // Target for the observer
- const targetNode = document.body;
- // Start observing the target node
- observer.observe(targetNode, config);
- // Pause all videos every 100ms
- setInterval(() => {
- document.querySelectorAll('video').forEach(video => {
- video.pause();
- });
- }, 50);
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement